home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / mutt / adjust.mut < prev    next >
Text File  |  1988-10-11  |  4KB  |  121 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
  22.  
  23. (const
  24.   things-that-require-2-blanks "[.:?!]"
  25.   things-that-can-also-end-sentences "[])}'\"]"
  26. )
  27.  
  28. (include wspace.mut)
  29.  
  30. (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))
  42.     (forward-line 1)
  43.     (skip-whitespace) (indent (current-column))
  44.     (forward-line -1) (current-column col)
  45.     (last-break-column (blanks-needed 0))
  46.     (while TRUE
  47.     {
  48.       (if (skip-over-text)    ; looking at text
  49.     {
  50.       (if (< wrap-column (+ (current-column) blanks-needed))
  51.         {
  52.           (if (== 0 last-break-column)
  53.             {
  54.           (kill-whitespace)
  55.           (if (looking-at '$')    ;; might be at end of region
  56.           {
  57.             (last-break-column (current-column))
  58.             (blanks-needed 0)    ;; since already past wrap column
  59.             (continue)
  60.           })
  61.         }
  62.             (current-column last-break-column))
  63.           (newline)
  64.           ;(if justify
  65.           ; { (forward-line -1)(justify-line wrap-column)(forward-line 1) })
  66. ;(update)
  67.           (to-col indent)
  68.           (last-break-column (blanks-needed 0))
  69.         }
  70.         {
  71.         ;; put blanks at end of last word
  72.           (col (+ (current-column) blanks-needed))
  73.           (current-column last-break-column)
  74.           (insert-text (substr "   " 0 blanks-needed))
  75.           (last-break-column (current-column col))
  76.         ;; calculate blanks needed at end of current word
  77.           (while
  78.             { (previous-character)
  79.           (looking-at things-that-can-also-end-sentences) } ())
  80.           (blanks-needed
  81.             (if (looking-at things-that-require-2-blanks) 2 1))
  82.           (current-column last-break-column)
  83.         ;; get ready for next word
  84.           (kill-whitespace)
  85.         }
  86.       )
  87.     }
  88.     {    ;; end of line
  89.       (switch (skip-blank-lines) 1 (goto new-paragraph) 2 (done))
  90.       (forward-line -1)(end-of-line)
  91.       (delete-character)(kill-whitespace)
  92.     }
  93.       )
  94.     })
  95.   }
  96.   skip-over-text  HIDDEN    ; TRUE if skiped over text, FALSE if EoL
  97.   {
  98.     (if (looking-at '$') { FALSE (done) })
  99.     (if (looking-at "\\([\^ ^I]+\\)")
  100.       { (arg-prefix (strlen (get-matched '\1')))(next-character) TRUE }
  101.       FALSE
  102.     )
  103.   }
  104.     ;; Returns: 0 (text on next line), 1 (blank lines), 2 (if bottoms out)
  105.     ;; Point left at start of next non blank line.
  106.   skip-blank-lines HIDDEN
  107.   {
  108.     (byte blank-lines)
  109.  
  110.     (blank-lines 0)
  111.     (while TRUE
  112.     {
  113.       (if (or (not (forward-line 1)) (== 0 (-= lines-left 1))) { 2 (done) })
  114.       (if (looking-at '\ *$')
  115.         (blank-lines 1)
  116.     (break))
  117.     })
  118.     blank-lines
  119.   }
  120. )
  121.