home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / Editors / mjovesrc.zoo / paragrap.c < prev    next >
C/C++ Source or Header  |  1992-04-04  |  14KB  |  547 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. int    RMargin = 78,
  113.     LMargin = 0;
  114. private Line    *para_head,
  115.     *para_tail;
  116. private int    head_indent,
  117.     body_indent;
  118. private bool    use_lmargin;
  119.  
  120. /* some defines for paragraph boundery checking */
  121. #define I_EMPTY        (-1)    /* line "looks" empty (spaces and tabs) */
  122. #define I_PERIOD    (-2)    /* line begins with "." or "\" */
  123. #define I_BUFEDGE    (-3)    /* line is nonexistent (edge of buffer) */
  124.  
  125. static bool    bslash;        /* Nonzero if get_indent finds line starting
  126.                    with backslash */
  127.  
  128. private int
  129. i_blank(lp)
  130. Line    *lp;
  131. {
  132.     return get_indent(lp) < 0;
  133. }
  134.  
  135. private bool
  136. i_bsblank(lp)
  137. Line    *lp;
  138. {
  139.     return i_blank(lp) || bslash;
  140. }
  141.  
  142. private int
  143. get_indent(lp)
  144. register Line    *lp;
  145. {
  146.     Bufpos    save;
  147.     register int    indent;
  148.  
  149.     bslash = NO;
  150.     if (lp == NULL)
  151.         return I_BUFEDGE;
  152.     DOTsave(&save);
  153.     SetLine(lp);
  154.     if (blnkp(linebuf))
  155.         indent = I_EMPTY;
  156.     else if (linebuf[0] == '.')
  157.         indent = I_PERIOD;
  158.     else if (linebuf[0] == '\\') {
  159.         /* BH 12/24/85.  Backslash is BLANK only if next line
  160.            also starts with Backslash. */
  161.         bslash = YES;
  162.         SetLine(lp->l_next);
  163.         if (linebuf[0] == '\\')
  164.             indent = I_PERIOD;
  165.         else
  166.             indent = 0;
  167.     } else {
  168.         ToIndent();
  169.         indent = calc_pos(linebuf, curchar);
  170.     }
  171.     SetDot(&save);
  172.  
  173.     return indent;
  174. }
  175.  
  176. private Line *
  177. tailrule(lp)
  178. register Line    *lp;
  179. {
  180.     int    i;
  181.  
  182.     i = get_indent(lp);
  183.     if (i < 0)
  184.         return lp;    /* one line paragraph */
  185.     do {
  186.         if ((get_indent(lp->l_next) != i) || bslash)
  187.             /* BH line with backslash is head of next para */
  188.             break;
  189.     } while ((lp = lp->l_next) != NULL);
  190.     if (lp == NULL)
  191.         complain((char *) NULL);
  192.     return lp;
  193. }
  194.  
  195. /* Finds the beginning, end and indent of the current paragraph, and sets
  196.    the above global variables.  HOW says how to behave when we're between
  197.    paragraphs.  That is, it's either FORWARD or BACKWARD depending on which
  198.    way we're favoring. */
  199.  
  200. private void
  201. find_para(how)
  202. int    how;
  203. {
  204.     Line    *this,
  205.         *prev,
  206.         *next,
  207.         *head = NULL,
  208.         *body = NULL,
  209.         *tail = NULL;
  210.     int    this_indent;
  211.     Bufpos    orig;        /* remember where we were when we started */
  212.  
  213.     DOTsave(&orig);
  214. strt:
  215.     this = curline;
  216.     prev = curline->l_prev;
  217.     next = curline->l_next;
  218.     this_indent = get_indent(this);
  219.  
  220.     if (i_blank(this)) {        /* rule 1 */
  221.         if (how == BACKWARD) {
  222.             while (i_blank(curline))
  223.                 if (firstp(curline))
  224.                     complain((char *)NULL);
  225.                 else
  226.                     line_move(BACKWARD, 1, NO);
  227.             goto strt;
  228.         } else {
  229.             while (i_blank(curline))
  230.                 if (lastp(curline))
  231.                     complain((char *)NULL);
  232.                 else
  233.                     line_move(FORWARD, 1, NO);
  234.             head = curline;
  235.             next = curline->l_next;
  236.             body = !i_bsblank(next)? next : head;
  237.         }
  238.     } else if (i_bsblank(this) || i_blank(prev)) {    /* rule 2 */
  239.         head = this;
  240.         if (!i_bsblank(next))
  241.             body = next;
  242.     } else if (i_bsblank(next)) {    /* rule 3 */
  243.         tail = this;
  244.         body = this;
  245.     } else if ((get_indent(next) == this_indent) ||    /* rule 4 */
  246.            (get_indent(prev) == this_indent)) {
  247.         body = this;
  248.     } else {        /* rule 6+ */
  249.         if (get_indent(prev) > this_indent) {
  250.             /* hanging indent maybe? */
  251.             if ((next != NULL) &&
  252.                 (get_indent(next) == get_indent(next->l_next))) {
  253.                 head = this;
  254.                 body = next;
  255.             }
  256.         }
  257.         /* Now we handle hanging indent else and the other
  258.            case of this_indent > get_indent(prev).  That is,
  259.            if we didn't resolve HEAD in the above if, then
  260.            we are not a hanging indent. */
  261.         if (head == NULL) {    /* still don't know */
  262.             if (this_indent > get_indent(prev))
  263.                 head = this;
  264.             else
  265.                 head = prev;
  266.             body = head->l_next;
  267.         }
  268.     }
  269.     /* rule 5 -- find the missing parts */
  270.     if (head == NULL) {    /* haven't found head of paragraph so do so now */
  271.         Line    *lp;
  272.         int    i;
  273.  
  274.         lp = this;
  275.         do {
  276.             i = get_indent(lp->l_prev);
  277.             if (i < 0)    /* is blank */
  278.                 head = lp;
  279.             else if (bslash)
  280.                 head = lp->l_prev;
  281.             else if (i != this_indent) {
  282.                 this = lp->l_prev;
  283.                 if (get_indent(this->l_prev) == i)
  284.                     head = this->l_next;
  285.                 else
  286.                     head = this;
  287.             }
  288.         } while (head == NULL && (lp = lp->l_prev) != NULL);
  289.         if (lp == NULL)
  290.             complain((char *)NULL);
  291.     }
  292.     if (body == NULL)        /* this must be a one line paragraph */
  293.         body = head;
  294.     if (tail == NULL)
  295.         tail = tailrule(body);
  296.     if (tail == NULL || head == NULL || body == NULL)
  297.         complain("BUG! tail(%d),head(%d),body(%d)!", tail, head, body);
  298.     para_head = head;
  299.     para_tail = tail;
  300.     head_indent = get_indent(head);
  301.     body_indent = get_indent(body);
  302.  
  303.     SetDot(&orig);
  304. }
  305.  
  306. void
  307. Justify()
  308. {
  309.     use_lmargin = is_an_arg();
  310.     find_para(BACKWARD);
  311.     DoJustify(para_head, 0, para_tail, length(para_tail), NO,
  312.           use_lmargin ? LMargin : body_indent);
  313. }
  314.  
  315. private Line *
  316. max_line(l1, l2)
  317. Line    *l1,
  318.     *l2;
  319. {
  320.     return inorder(l1, 0, l2, 0)? l2 : l1;
  321. }
  322.  
  323. private Line *
  324. min_line(l1, l2)
  325. Line    *l1,
  326.     *l2;
  327. {
  328.     return inorder(l1, 0, l2, 0)? l1 : l2;
  329. }
  330.  
  331. void
  332. RegJustify()
  333. {
  334.     Mark    *mp = CurMark(),
  335.         *tailmark;
  336.     Line    *l1 = curline,
  337.         *l2 = mp->m_line;
  338.     int    c1 = curchar,
  339.         c2 = mp->m_char;
  340.     Line    *rl1,
  341.         *rl2;
  342.  
  343.     use_lmargin = is_an_arg();
  344.     (void) fixorder(&l1, &c1, &l2, &c2);
  345.     do {
  346.         DotTo(l1, c1);
  347.         find_para(FORWARD);
  348.         rl1 = max_line(l1, para_head);
  349.         rl2 = min_line(l2, para_tail);
  350.         tailmark = MakeMark(para_tail, 0, M_FLOATER);
  351.         DoJustify(rl1, (rl1 == l1) ? c1 : 0, rl2,
  352.               (rl2 == l2) ? c2 : length(rl2),
  353.               NO, use_lmargin ? LMargin : body_indent);
  354.         l1 = tailmark->m_line->l_next;
  355.         DelMark(tailmark);
  356.         c1 = 0;
  357.     } while (l1 != NULL && l2 != rl2);
  358. }
  359.  
  360. void
  361. do_rfill(ulm)
  362. bool    ulm;
  363. {
  364.     Mark    *mp = CurMark();
  365.     Line    *l1 = curline,
  366.         *l2 = mp->m_line;
  367.     int    c1 = curchar,
  368.         c2 = mp->m_char;
  369.  
  370.     use_lmargin = ulm;
  371.     (void) fixorder(&l1, &c1, &l2, &c2);
  372.     DoJustify(l1, c1, l2, c2, NO, use_lmargin ? LMargin : 0);
  373. }
  374.  
  375. private void
  376. do_space()
  377. {
  378.     int    c1 = curchar,
  379.         c2 = c1,
  380.         diff,
  381.         nspace;
  382.     char    ch;
  383.  
  384.     while (c1 > 0 && ((ch = linebuf[c1 - 1]) == ' ' || ch == '\t'))
  385.         c1 -= 1;
  386.     while ((ch = linebuf[c2]) == ' ' || ch == '\t')
  387.         c2 += 1;
  388.     diff = (c2 - c1);
  389.     curchar = c2;
  390.  
  391.     if (diff == 0)
  392.         return;
  393.     if (c1 > 0) {
  394.         int    topunct = c1 - 1;
  395.  
  396.         nspace = 1;
  397.         if (diff >= 2) {
  398.             while (strchr("\")]", linebuf[topunct])) {
  399.                 if (topunct == 0)
  400.                     break;
  401.                 topunct -= 1;
  402.             }
  403.             if (strchr("?!.:", linebuf[topunct]))
  404.                 nspace = 2;
  405.         }
  406.     } else
  407.         nspace = 0;
  408.  
  409.     if (diff > nspace)
  410.         del_char(BACKWARD, (diff - nspace), NO);
  411.     else if (diff < nspace)
  412.         insert_c(' ', (nspace - diff));
  413. }
  414.  
  415. #ifdef    MSDOS
  416. /*#pragma loop_opt(off) */
  417. #endif
  418.  
  419. void
  420. DoJustify(l1, c1, l2, c2, scrunch, indent)
  421. Line    *l1,
  422.     *l2;
  423. int    c1,
  424.     c2,
  425.     indent;
  426. bool
  427.     scrunch;
  428. {
  429.     int    okay_char = -1;
  430.     char    *cp;
  431.     Mark    *savedot = MakeMark(curline, curchar, M_FLOATER),
  432.         *endmark;
  433.  
  434.     (void) fixorder(&l1, &c1, &l2, &c2);    /* l1/c1 will be before l2/c2 */
  435.     DotTo(l1, c1);
  436.     if (get_indent(l1) >= c1) {
  437.         if (use_lmargin) {
  438.             Bol();
  439.             n_indent(indent + (head_indent - body_indent));
  440.             use_lmargin = NO;    /* turn this off now */
  441.         }
  442.         ToIndent();
  443.     }
  444.     endmark = MakeMark(l2, c2, M_FLOATER);
  445.  
  446.     for (;;) {
  447.         /* The while loop succeeds at least once, when curchar ==
  448.            indent.  So we know that okay_char >= indent when we
  449.            exit the loop. */
  450.         while (calc_pos(linebuf, curchar) < RMargin) {
  451.             if (curline == endmark->m_line && curchar >= endmark->m_char)
  452.                 goto outahere;
  453.             okay_char = curchar;
  454.             if (eolp()) {
  455.                 /* delete line separator */
  456.                 del_char(FORWARD, 1, NO);
  457.                 ins_str("  ", NO);
  458.             } else {
  459.                 cp = StrIndex(FORWARD, linebuf, curchar + 1, ' ');
  460.                 if (cp == NULL)
  461.                     Eol();
  462.                 else
  463.                     curchar = (cp - linebuf);
  464.             }
  465.             do_space();
  466.         }
  467.         if (okay_char > indent)
  468.             curchar = okay_char;
  469.         if (curline == endmark->m_line && curchar >= endmark->m_char)
  470.             goto outahere;
  471.  
  472.         /* Can't fit in small margin, so if' we're at the end of
  473.            the line then we just move to the next line.  Otherwise
  474.            we divide the line where we are and start over. */
  475.         if (eolp()) {
  476.             Line    *l = curline;
  477.  
  478.             line_move(FORWARD, 1, NO);
  479.             if (l == curline)    /* didn't actuall go anywhere */
  480.                 goto outahere;
  481.         } else {
  482.             DelWtSpace();
  483.             LineInsert(1);
  484.             if (scrunch && TwoBlank()) {
  485.                 Eol();
  486.                 del_char(FORWARD, 1, NO);
  487.             }
  488.         }
  489.         n_indent(indent);
  490.     }
  491. outahere:
  492.     ToMark(savedot);    /* Back to where we were */
  493.     DelMark(endmark);    /* Free up marks */
  494.     DelMark(savedot);
  495.     this_cmd = last_cmd = 0; /* So everything is under control */
  496.     f_mess("");
  497. }
  498.  
  499. #ifdef    MSDOS
  500. /*#pragma loop_opt() */
  501. #endif
  502.  
  503. private void
  504. DoPara(dir)
  505. int    dir;
  506. {
  507.     register int    num = arg_value(),
  508.             first_time = TRUE;
  509.  
  510.     while (--num >= 0) {
  511. tryagain:
  512.         find_para(dir);        /* find paragraph bounderies */
  513.         if ((dir == BACKWARD) &&
  514.             ((!first_time) || ((para_head == curline) && bolp()))) {
  515.             if (bobp())
  516.                 complain((char *)NULL);
  517.             b_char(1);
  518.             first_time = !first_time;
  519.             goto tryagain;
  520.         }
  521.         SetLine((dir == BACKWARD) ? para_head : para_tail);
  522.         if (dir == BACKWARD && !firstp(curline) &&
  523.             i_blank(curline->l_prev))
  524.             line_move(BACKWARD, 1, NO);
  525.         else if (dir == FORWARD) {
  526.             if (lastp(curline)) {
  527.                 Eol();
  528.                 break;
  529.             }
  530.             /* otherwise */
  531.             line_move(FORWARD, 1, NO);
  532.         }
  533.     }
  534. }
  535.  
  536. void
  537. BackPara()
  538. {
  539.     DoPara(BACKWARD);
  540. }
  541.  
  542. void
  543. ForPara()
  544. {
  545.     DoPara(FORWARD);
  546. }
  547.