home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / mutt / package / adjust.mut next >
Text File  |  1995-01-14  |  4KB  |  120 lines

  1. ;; adjust.mut : Format blocks of text
  2.  
  3. ;;    (adjust-lines) fills all lines of text starting at the line the point
  4. ;; is on and ending with the line n-lines-of-text -1 below the point or the
  5. ;; end of the buffer.  Empty lines signal end of paragraph and are
  6. ;; otherwise ignored.
  7. ;;    Paragraph indention is handled as follows:  The first line is
  8. ;; indented the same as first line of text and subsequent lines are
  9. ;; indented the same as the second line.
  10. ;;    Words ending in <terminal>[<quote><close>]* are followed by two
  11. ;; blanks, where <terminal> is any of ".:?!", <quote> is " or ', and
  12. ;; <close> is any of ")]}", e.g.:
  13. ;; end.  of?  sentence.'  sorts!"  of.)  things?"]
  14. ;;    If you want to convert a block of text into list of words one per
  15. ;; line, just set the right-margin to something small (like 0 or 1).
  16.  
  17. ;; To Do:
  18. ;;   Implement justify-line.
  19. ;;   Handle backspace so that overstruck text is formatted properly.
  20.  
  21. ;; C Durland 10/88    Public Domain
  22.  
  23. (const
  24.   things-that-require-2-blanks "[.:?!]"
  25.   things-that-can-also-end-sentences "[])}'\"]"
  26. )
  27.  
  28. (include wspace.mut)
  29.  
  30. (small-int lines-left)
  31.  
  32. (defun
  33.   adjust-lines (int n-lines-of-text right-margin)(bool justify)
  34.   {
  35.     (int indent last-break-column blanks-needed col wrap-column)
  36.  
  37.     (lines-left n-lines-of-text)(wrap-column (+ 1 right-margin))
  38.     (beginning-of-line)
  39.     (if (and (looking-at '\ *$') (== 2 (skip-blank-lines))) (done))
  40.   (label new-paragraph)
  41.     (skip-whitespace) (col (current-column))    ;; first line indent
  42.     (forward-line 1)
  43.     (skip-whitespace) (indent (current-column))    ;; second line indent
  44.     (forward-line -1) (current-column col)
  45.     (last-break-column (blanks-needed 0))
  46.     (while TRUE
  47.     {
  48.       (if (looking-at "[\^ ^I]+" TRUE)        ;; skip over non-whitespace
  49.     {    ;; sitting at whitespace between words
  50.       (if (< wrap-column (+ (current-column) blanks-needed)) ;; split line
  51.         {
  52.           (if (== 0 last-break-column)    ;; end of first word in line
  53.             {
  54.           (delete-whitespace)        ;; remove space between words
  55.           (if (looking-at '$')        ;; at end of line
  56.           {
  57.             (last-break-column (current-column))  ;; end of last word
  58.             (blanks-needed 0)    ;; since already past wrap column
  59.             (continue)
  60.           })
  61.         }
  62.             (current-column last-break-column))  ;; split after last word
  63.           (newline)                ;; split the line
  64.           ;(if justify
  65.           ; { (forward-line -1)(justify-line wrap-column)(forward-line 1) })
  66. ;(update)
  67.           (to-col indent)            ;; start the new line
  68.           (last-break-column (blanks-needed 0))
  69.         }
  70.         ;; else more stuff will fit on this line
  71.         {    ;; put blanks at end of last word
  72.           (col (+ (current-column) blanks-needed))
  73.           (current-column last-break-column)
  74.           (insert-text (extract-elements "   " 0 blanks-needed))
  75.           (last-break-column (current-column col))
  76.         ;; calculate blanks needed at end of current word
  77.           (while        ;; take care of cases like "[foo.]"
  78.             { (previous-character)
  79.            (and
  80.              (looking-at things-that-can-also-end-sentences)
  81.              (!= 1 (current-column)))        ;; ugh
  82.         } ())
  83.           (blanks-needed
  84.             (if (looking-at things-that-require-2-blanks) 2 1))
  85.           (current-column last-break-column)
  86.         ;; get ready for next word
  87.           (delete-whitespace)
  88.         }
  89.       )    ;; end if
  90.     }
  91.     {    ;; else at end of line
  92.       (switch (skip-blank-lines) 1 (goto new-paragraph) 2 (done))
  93.       (forward-line -1)(end-of-line)
  94.       (delete-character)(delete-whitespace)
  95.     }
  96.       )    ;; end if
  97.     })    ;; end while
  98.   }
  99.  
  100.     ;; Returns:
  101.     ;;   0 (text on next line)
  102.     ;;   1 (blank lines)
  103.     ;;   2 (if no lines left to adjust)
  104.     ;; Dot left at start of next non blank line.
  105.   skip-blank-lines HIDDEN
  106.   {
  107.     (byte blank-lines)
  108.  
  109.     (blank-lines 0)
  110.     (while TRUE
  111.     {
  112.       (if (or (not (forward-line 1)) (== 0 (-= lines-left 1))) { 2 (done) })
  113.       (if (looking-at '\ *$')
  114.         (blank-lines 1)
  115.     (break))
  116.     })
  117.     blank-lines
  118.   }
  119. )
  120.