home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / jove / part06 / paragraph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-02  |  13.6 KB  |  526 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986 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.  
  10. /* Thanks to Brian Harvey for this paragraph boundery finding algorithm.
  11.    It's really quite hairy figuring it out.  This deals with paragraphs that
  12.    are seperated by blank lines, lines beginning with a Period (assumed to
  13.    be an nroff command), lines beginning with BackSlash (assumed to be Tex
  14.    commands).  Also handles paragraphs that are separated by lines of
  15.    different indent; and it deals with outdented paragraphs, too.  It's
  16.    really quite nice.  Here's Brian's algorithm.
  17.  
  18.    Definitions:
  19.    
  20.    THIS means the line containing the cursor.
  21.    PREV means the line above THIS.
  22.    NEXT means the line below THIS.
  23.    
  24.    BLANK means empty, empty except for spaces and tabs, starts with a period
  25.    or a backslash, or nonexistent (because the edge of the buffer is
  26.    reached).  ((BH 12/24/85 A line starting with backslash is blank only if
  27.    the following line also starts with backslash.  This is so that \noindent
  28.    is part of a paragraph, but long strings of TeX commands don't get
  29.    rearranged.  It still isn't perfect but it's better.))
  30.  
  31.    BSBLANK means BLANK or starts with a backslash.  (BH 12/24/85)
  32.    
  33.    HEAD means the first (nonblank) line of the paragraph containing THIS.
  34.    BODY means all other (nonblank) lines of the paragraph.
  35.    TAIL means the last (nb) line of the paragraph.  (TAIL is part of BODY.)
  36.    
  37.    HEAD INDENT means the indentation of HEAD.  M-J should preserve this.
  38.    BODY INDENT means the indentation of BODY.  Ditto.
  39.    
  40.    Subprocedures:
  41.    
  42.    TAILRULE(BODYLINE)
  43.    If BODYLINE is BLANK, the paragraph has only one line, and there is no
  44.    BODY and therefore no TAIL.  Return.  Otherwise, starting from BODYLINE,
  45.    move down until you find a line that either is BSBLANK or has a different
  46.    indentation from BODYLINE.  The line above that different line is TAIL.
  47.    Return.
  48.    
  49.    Rules:
  50.    
  51.    1.  If THIS is BLANK, which command are you doing?  If M-J or M-[, then go
  52.    up to the first non-BLANK line and start over.  (If there is no non-BLANK
  53.    line before THIS, ring the bell.)  If M-], then the first non-BLANK line
  54.    below THIS is HEAD, and the second consecutive non-BSBLANK line (if any) is
  55.    the beginning of BODY.  (If there is no non-BLANK line after THIS, ring
  56.    the bell.)  Do TAILRULE(beginning-of-BODY).  Go to rule A.
  57.  
  58.    2.  If PREV is BLANK or THIS is BSBLANK, then THIS is HEAD, and NEXT (if
  59.    not BSBLANK) is in BODY.  Do TAILRULE(NEXT).  Go to rule A.
  60.  
  61.    3.  If NEXT is BSBLANK, then THIS is TAIL, therefore part of BODY.  Go to
  62.    rule 5 to find HEAD.
  63.    
  64.    4.  If either NEXT or PREV has the same indentation as THIS, then THIS is
  65.    part of BODY.  Do TAILRULE(THIS).  Go to rule 5 to find HEAD.  Otherwise,
  66.    go to rule 6.
  67.    
  68.    5.  Go up until you find a line that is either BSBLANK or has a different
  69.    indentation from THIS.  If that line is BLANK, the line below it is HEAD.
  70.    If that line is non-BLANK, then call that new line THIS for what follows.
  71.    If (the new) PREV has the same indent as THIS, then (the new) NEXT is
  72.    HEAD.  If PREV has a different indent from THIS, then THIS is HEAD.  Go to
  73.    rule A.
  74.    
  75.    6.  If you got here, then both NEXT and PREV are nonblank and are
  76.    differently indented from THIS.  This is a tricky case and there is no
  77.    guarantee that you're going to win.  The most straightforward thing to do
  78.    is assume that we are not using hanging indentation.  In that case:
  79.    whichever of PREV and THIS is indented further is HEAD.  Do
  80.    TAILRULE(HEAD+1).  Go to rule A.
  81.    
  82.    6+.  A more complicated variant would be this: if THIS is indented further
  83.    than PREV, we are using regular indentation and rule 6 applies.  If PREV
  84.    is indented further than THIS, look at both NEXT and the line after NEXT.
  85.    If those two lines are indented equally, and more than THIS, then we are
  86.    using hanging indent, THIS is HEAD, and NEXT is the first line of BODY.
  87.    Do TAILRULE(NEXT).  Otherwise, rule 6 applies.
  88.    
  89.    A.  You now know where HEAD and TAIL are.  The indentation of HEAD is HEAD
  90.    INDENT; the indentation of TAIL is BODY INDENT.
  91.    
  92.    B.  If you are trying to M-J, you are now ready to do it.
  93.    
  94.    C.  If you are trying to M-], leave point after the newline that ends
  95.    TAIL.  In other words, leave the cursor at the beginning of the line
  96.    after TAIL.  It is not possible for this to leave point where it started
  97.    unless it was already at the end of the buffer.
  98.    
  99.    D.  If you are trying to M-[, if the line before HEAD is not BLANK, then
  100.    leave point just before HEAD.  That is, leave the cursor at the beginning
  101.    of HEAD.  If the line before HEAD is BLANK, then leave the cursor at the
  102.    beginning of that line.  If the cursor didn't move, go up to the first
  103.    earlier non-BLANK line and start over.
  104.  
  105.  
  106.    End of Algorithm.  I implemented rule 6+ because it seemed nicer.  */
  107.  
  108. int    RMargin = 78,
  109.     LMargin = 0;
  110. Line    *para_head,
  111.     *para_tail;
  112. int    head_indent,
  113.     body_indent;
  114. static int    use_lmargin;
  115.  
  116. /* some defines for paragraph boundery checking */
  117. #define I_EMPTY        -1    /* line "looks" empty (spaces and tabs) */
  118. #define I_PERIOD    -2    /* line begins with "." or "\" */
  119. #define I_BUFEDGE    -3    /* line is nonexistent (edge of buffer) */
  120.  
  121. static int    bslash;        /* Nonzero if get_indent finds line starting
  122.                    with backslash */
  123.  
  124. i_bsblank(lp)
  125. Line    *lp;
  126. {
  127.     if (i_blank(lp))
  128.         return 1;
  129.     return bslash;
  130. }
  131.  
  132. i_blank(lp)
  133. Line    *lp;
  134. {
  135.     return (get_indent(lp) < 0);
  136. }
  137.  
  138. static 
  139. get_indent(lp)
  140. register Line    *lp;
  141. {
  142.     Bufpos    save;
  143.     register int    indent;
  144.  
  145.     bslash = 0;
  146.     if (lp == 0)
  147.         return I_BUFEDGE;
  148.     DOTsave(&save);
  149.     SetLine(lp);
  150.     if (blnkp(linebuf))
  151.         indent = I_EMPTY;
  152.      else if (linebuf[0] == '.')
  153.         indent = I_PERIOD;
  154.     else if (linebuf[0] == '\\') {
  155.         /* BH 12/24/85.  Backslash is BLANK only if next line
  156.            also starts with Backslash. */
  157.         bslash++;
  158.         SetLine(lp->l_next);
  159.         if (linebuf[0] == '\\')
  160.             indent = I_PERIOD;
  161.         else
  162.             indent = 0;
  163.     } else {
  164.         ToIndent();
  165.         indent = calc_pos(linebuf, curchar);
  166.     }
  167.     SetDot(&save);
  168.  
  169.     return indent;
  170. }
  171.  
  172. static Line *
  173. tailrule(lp)
  174. register Line    *lp;
  175. {
  176.     int    i;
  177.  
  178.     i = get_indent(lp);
  179.     if (i < 0)
  180.         return lp;    /* one line paragraph */
  181.     do {
  182.         if ((get_indent(lp->l_next) != i) || bslash)
  183.             /* BH line with backslash is head of next para */
  184.             break;
  185.     } while ((lp = lp->l_next) != 0);
  186.     if (lp == 0)
  187.         complain((char *) 0);
  188.     return lp;
  189. }
  190.  
  191. /* Finds the beginning, end and indent of the current paragraph, and sets
  192.    the above global variables.  HOW says how to behave when we're between
  193.    paragraphs.  That is, it's either FORWARD or BACKWARD depending on which
  194.    way we're favoring. */
  195.  
  196. find_para(how)
  197. {
  198.     Line    *this,
  199.         *prev,
  200.         *next,
  201.         *head = 0,
  202.         *body = 0,
  203.         *tail = 0;
  204.     int    this_indent;
  205.     Bufpos    orig;        /* remember where we were when we started */
  206.  
  207.     exp = 1;
  208.     DOTsave(&orig);
  209. strt:
  210.     this = curline;
  211.     prev = curline->l_prev;
  212.     next = curline->l_next;
  213.     this_indent = get_indent(this);
  214.  
  215.     if (i_blank(this)) {        /* rule 1 */
  216.         if (how == BACKWARD) {
  217.             while (i_blank(curline))
  218.                 if (firstp(curline))
  219.                     complain((char *) 0);
  220.                 else
  221.                     line_move(BACKWARD, NO);
  222.             goto strt;
  223.         } else {
  224.             while (i_blank(curline))
  225.                 if (lastp(curline))
  226.                     complain((char *) 0);
  227.                 else
  228.                     line_move(FORWARD, NO);
  229.             head = curline;
  230.             next = curline->l_next;
  231.             if (!i_bsblank(next))
  232.                 body = next;
  233.             else
  234.                 body = head;
  235.         }
  236.     } else if (i_bsblank(this) || i_blank(prev)) {    /* rule 2 */
  237.         head = this;
  238.         if (!i_bsblank(next))
  239.             body = next;
  240.     } else if (i_bsblank(next)) {    /* rule 3 */
  241.         tail = this;
  242.         body = this;
  243.     } else if ((get_indent(next) == this_indent) ||    /* rule 4 */
  244.            (get_indent(prev) == this_indent))
  245.         body = this;
  246.     else {        /* rule 6+ */
  247.         if (get_indent(prev) > this_indent) {
  248.             /* hanging indent maybe? */
  249.             if ((next != 0) &&
  250.                 (get_indent(next) == get_indent(next->l_next))) {
  251.                 head = this;
  252.                 body = next;
  253.             }
  254.         }
  255.         /* Now we handle hanging indent else and the other
  256.            case of this_indent > get_indent(prev).  That is,
  257.            if we didn't resolve HEAD in the above if, then
  258.            we are not a hanging indent. */
  259.         if (head == 0) {    /* still don't know */
  260.             if (this_indent > get_indent(prev))
  261.                 head = this;
  262.             else
  263.                 head = prev;
  264.             body = head->l_next;
  265.         }
  266.     }
  267.     /* rule 5 -- find the missing parts */
  268.     if (head == 0) {    /* haven't found head of paragraph so do so now */
  269.         Line    *lp;
  270.         int    i;
  271.  
  272.         lp = this;
  273.         do {
  274.             i = get_indent(lp->l_prev);
  275.             if (i < 0)    /* is blank */
  276.                 head = lp;
  277.             else if (i != this_indent || bslash) {
  278.                 Line    *this = lp->l_prev;
  279.  
  280.                 if (get_indent(this->l_prev) == i)
  281.                     head = this->l_next;
  282.                 else
  283.                     head = this;
  284.             }
  285.         } while (head == 0 && (lp = lp->l_prev) != 0);
  286.         if (lp == 0)
  287.             complain((char *) 0);
  288.     }
  289.     if (body == 0)        /* this must be a one line paragraph */
  290.         body = head;
  291.     if (tail == 0)
  292.         tail = tailrule(body);
  293.     if (tail == 0 || head == 0 || body == 0)
  294.         complain("BUG! tail(%d),head(%d),body(%d)!", tail, head, body);
  295.     para_head = head;
  296.     para_tail = tail;
  297.     head_indent = get_indent(head);
  298.     body_indent = get_indent(body);
  299.  
  300.     SetDot(&orig);
  301. }
  302.  
  303. Justify()
  304. {
  305.     use_lmargin = (exp_p != NO);
  306.     find_para(BACKWARD);
  307.     DoJustify(para_head, 0, para_tail, length(para_tail), NO,
  308.           use_lmargin ? LMargin : body_indent);
  309. }
  310.  
  311. Line *
  312. max_line(l1, l2)
  313. Line    *l1,
  314.     *l2;
  315. {
  316.     if (inorder(l1, 0, l2, 0))
  317.         return l2;
  318.     return l1;
  319. }
  320.  
  321. Line *
  322. min_line(l1, l2)
  323. Line    *l1,
  324.     *l2;
  325. {
  326.     if (inorder(l1, 0, l2, 0))
  327.         return l1;
  328.     return l2;
  329. }
  330.  
  331. RegJustify()
  332. {
  333.     Mark    *mp = CurMark(),
  334.         *tailmark;
  335.     Line    *l1 = curline,
  336.         *l2 = mp->m_line;
  337.     int    c1 = curchar,
  338.         c2 = mp->m_char;
  339.     Line    *rl1,
  340.         *rl2;
  341.  
  342.     use_lmargin = (exp_p != NO);
  343.     (void) fixorder(&l1, &c1, &l2, &c2);
  344.     do {
  345.         DotTo(l1, c1);
  346.         find_para(FORWARD);
  347.         rl1 = max_line(l1, para_head);
  348.         rl2 = min_line(l2, para_tail);
  349.         tailmark = MakeMark(para_tail, 0, FLOATER);
  350.         DoJustify(rl1, (rl1 == l1) ? c1 : 0, rl2,
  351.               (rl2 == l2) ? c2 : length(rl2),
  352.               NO, use_lmargin ? LMargin : body_indent);
  353.         l1 = tailmark->m_line->l_next;
  354.         DelMark(tailmark);
  355.         c1 = 0;
  356.     } while (l1 != 0 && l2 != rl2);
  357. }
  358.  
  359. do_rfill()
  360. {
  361.     Mark    *mp = CurMark();
  362.     Line    *l1 = curline,
  363.         *l2 = mp->m_line;
  364.     int    c1 = curchar,
  365.         c2 = mp->m_char;
  366.  
  367.     use_lmargin = (exp_p != NO);
  368.     (void) fixorder(&l1, &c1, &l2, &c2);
  369.     DoJustify(l1, c1, l2, c2, NO, use_lmargin ? LMargin : 0);
  370. }
  371.  
  372. do_space()
  373. {
  374.     int    c1 = curchar,
  375.         c2 = c1,
  376.         diff,
  377.         nspace;
  378.     char    ch;
  379.  
  380.     while (c1 > 0 && ((ch = linebuf[c1 - 1]) == ' ' || ch == '\t'))
  381.         c1--;
  382.     while ((ch = linebuf[c2]) == ' ' || ch == '\t')
  383.         c2++;
  384.     diff = (c2 - c1);
  385.     curchar = c2;
  386.  
  387.     if (diff == 0)
  388.         return;
  389.     if (c1 > 0) {
  390.         int    topunct = c1 - 1;
  391.  
  392.         nspace = 1;
  393.         if (diff >= 2) {
  394.             while (index("\")]", linebuf[topunct])) {
  395.                 if (topunct == 0)
  396.                     break;
  397.                 topunct--;
  398.             }
  399.             if (index("?!.:", linebuf[topunct]))
  400.                 nspace = 2;
  401.         }
  402.     } else
  403.         nspace = 0;
  404.  
  405.     if (diff > nspace)
  406.         DoTimes(DelPChar(), (diff - nspace));
  407.     else if (diff < nspace)
  408.         DoTimes(Insert(' '), (nspace - diff));
  409. }
  410.  
  411. DoJustify(l1, c1, l2, c2, scrunch, indent)
  412. Line    *l1,
  413.     *l2;
  414. {
  415.     int    okay_char = -1;
  416.     char    *cp;
  417.     Mark    *savedot = MakeMark(curline, curchar, FLOATER),
  418.         *endmark;
  419.  
  420.     exp = 1;
  421.     (void) fixorder(&l1, &c1, &l2, &c2);    /* l1/c1 will be before l2/c2 */
  422.     DotTo(l1, c1);
  423.     if (get_indent(l1) >= c1) {
  424.         if (use_lmargin) {
  425.             n_indent(indent + (head_indent - body_indent));
  426.             use_lmargin = 0;    /* turn this off now */
  427.         }
  428.         ToIndent();
  429.     }
  430.     endmark = MakeMark(l2, c2, FLOATER);
  431.  
  432.     for (;;) {
  433.         cp = StrIndex(1, linebuf, curchar, ' ');
  434.         if (cp == 0)
  435.             Eol();
  436.         else
  437.             curchar = (cp - linebuf);
  438.         if (curline == endmark->m_line && curchar >= endmark->m_char)
  439.             goto outahere;
  440.         if (eolp()) {
  441.             ins_str("  ", NO);
  442.             DelNChar();    /* delete line separator */
  443.             curchar -= 2;    /* back over the spaces */
  444.         }
  445.         /* at this point we are ALWAYS sitting right after
  446.            a word - that is, just before some spaces or the
  447.            end of the line */
  448.         if (calc_pos(linebuf, curchar) <= RMargin) {
  449.             okay_char = curchar;
  450.             do_space();
  451.             continue;
  452.         }
  453.  
  454.         /* if we get here, we have done all we can for
  455.            this line - now we split the line, or just move
  456.            to the next one */
  457.         if (okay_char > 0)
  458.             curchar = okay_char;            
  459.         if (curline == endmark->m_line && curchar >= endmark->m_char)
  460.             goto outahere;
  461.         /* can't fit in small margin, so we do the best we can */
  462.         if (eolp()) {
  463.             line_move(FORWARD, NO);
  464.             n_indent(indent);
  465.         } else {
  466.             /* insert a line break - line WAS too long */
  467.             DelWtSpace();
  468.             LineInsert(1);
  469.             if (scrunch && TwoBlank()) {
  470.                 Eol();
  471.                 DelNChar();
  472.             }
  473.             n_indent(indent);
  474.         }
  475.     }
  476. outahere:
  477.     ToMark(savedot);    /* Back to where we were */
  478.     DelMark(endmark);    /* Free up marks */
  479.     DelMark(savedot);
  480.     this_cmd = last_cmd = 0; /* So everything is under control */
  481.     f_mess("");
  482. }
  483.  
  484. extern Line    *para_head,
  485.         *para_tail;
  486.  
  487. DoPara(dir)
  488. {
  489.     register int    num = exp,
  490.             first_time = TRUE;    
  491.  
  492.     while (--num >= 0) {
  493. tryagain:    find_para(dir);        /* find paragraph bounderies */
  494.         if ((dir == BACKWARD) &&
  495.             ((!first_time) || ((para_head == curline) && bolp()))) {
  496.                 if (bobp())
  497.                     complain((char *) 0);
  498.             BackChar();
  499.             first_time = !first_time;
  500.             goto tryagain;
  501.         }
  502.         SetLine((dir == BACKWARD) ? para_head : para_tail);
  503.         if (dir == BACKWARD && !firstp(curline) &&
  504.             i_blank(curline->l_prev))
  505.             line_move(BACKWARD, NO);
  506.         else if (dir == FORWARD) {
  507.             if (lastp(curline)) {
  508.                 Eol();
  509.                 break;
  510.             }
  511.             /* otherwise */
  512.             line_move(FORWARD, NO);
  513.         }
  514.     }
  515. }
  516.  
  517. BackPara()
  518. {
  519.     DoPara(BACKWARD);
  520. }
  521.  
  522. ForPara()
  523. {
  524.     DoPara(FORWARD);
  525. }
  526.