home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / paraform.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  7.4 KB  |  203 lines

  1. ;From: tom@ssd.harris.com (Tom Horsley)
  2. ;Newsgroups: comp.emacs
  3. ;Subject: Formatting with hanging indents
  4. ;Message-ID: <TOM.89Sep8072759@hcx2.ssd.harris.com>
  5. ;Date: 8 Sep 89 11:27:59 GMT
  6. ;Organization: Harris Computer Systems Division
  7. ;Lines: 194
  8. ;
  9. ;Someone was asking about formatting paragraphs with hanging indents. This is
  10. ;a package I wrote which I now use to do all my paragraph filling.  It uses
  11. ;several empirical rules to recognize a "paragraph" which seem to work well
  12. ;in practice, at least for the sorts of things I am always typing.
  13. ;
  14. ;For instance:
  15. ;
  16. ;   /* This is a sample C comment to be formatted by paraform
  17. ;    * this version is
  18. ;    * very ragged, but it will come out nicely filled
  19. ;    * once formatted.
  20. ;    */
  21. ;
  22. ;1) This is another sort of
  23. ;   paragraph, which can be filled by paraform with no mode
  24. ;   changes or variable settings required
  25. ;   since filling
  26. ;   the C comment.
  27. ;
  28. ;becomes:
  29. ;
  30. ;   /* This is a sample C comment to be formatted by paraform this version is
  31. ;    * very ragged, but it will come out nicely filled once formatted.
  32. ;    */
  33. ;
  34. ;1) This is another sort of paragraph, which can be filled by paraform with
  35. ;   no mode changes or variable settings required since filling the C
  36. ;   comment.
  37. ;
  38. ;The requirement for paraform to figure out what goes on is that in both of
  39. ;the above examples, the cursor was sitting on the 'T' in the first word
  40. ;(This) in each paragraph when it was filled.
  41. ;
  42. ;I replaced the standard fill paragraph key binding with this, but people who
  43. ;normally fill paragraphs with the cursor sitting at the end may not want to
  44. ;do that, paraform only works with the cursor on the first character of the
  45. ;paragraph, also it cannot work at all if you want paragraphs with the first
  46. ;line indented.
  47. ;
  48. ;-------------------cut here for paraform.el------------------------------
  49. ;; This is a paragraph formatting command  that combines many elements
  50. ;; of the standard paragraph formatting routines in a  more convenient
  51. ;; to use  functionality than the  standard fill-paragraph  command of
  52. ;; emacs -   this is  NOT  a functional  replacement  -    it  behaves
  53. ;; differently.
  54. ;;
  55. ;; The  primary  improvement is in the   area of formatting  text with
  56. ;; hanging indents. You MUST put the  cursor on the first character of
  57. ;; the paragraph. If  there is text to the  left of the cursor, it  is
  58. ;; assumed to be the paragraph label. It  searches down from the first
  59. ;; character for a line  that does not  look  like it  belongs  to the
  60. ;; paragraph.  The criteria are:
  61. ;;
  62. ;;   1) The end of the buffer is hit.
  63. ;;   2) The line is not long enough.
  64. ;;   3) The line has white space in the cursor column.
  65. ;;   4) The prefix is not identical to  the prefix of  the second line
  66. ;;      (for lines beyond the second line).
  67. ;;
  68. ;; The area defined  by this  match criteria is  formatted.  The first
  69. ;; line  remains prefixed  by  the  first  line prefix, any additional
  70. ;; lines are prefixed by the second line prefix.
  71. ;;
  72. ;; With a prefix arg it will right justify the text.
  73.  
  74. (defun paraform (prf)
  75. "Format  a paragraph  starting at point  the cursor is on.  Anything to
  76. the left of the cursor  is interpreted as  a prefix for line 1  of the
  77. paragraph.  The paragraph ends at the first  line following the cursor
  78. line  which contains  white space  in the cursor  column,  or does not
  79. match  the   previous lines. Any prefix   on  the  second line  of the
  80. paragraph is used as a fill prefix  for all remaining lines.  A prefix
  81. argument right justifies  the   paragraph.  The variable   fill-column
  82. determines where the right margin is."
  83.    (interactive "P")
  84.    (let
  85.       (
  86.          (start-pos (point))
  87.          (start-col (current-column))
  88.          (orig-fill-column fill-column)
  89.          (orig-fill-prefix fill-prefix)
  90.          (first-line-prefix
  91.             (buffer-substring
  92.                (save-excursion (beginning-of-line 1) (point))
  93.                (point)
  94.             )
  95.          )
  96.          (second-line-prefix nil)
  97.          (delete-final-newline nil)
  98.          start-marker
  99.          end-marker
  100.       )
  101.       ; Make sure this file has a final newline (otherwise while
  102.       ; loop below might infinite loop).
  103.       (if (/= (char-after (1- (point-max))) ?\n)
  104.          (save-excursion
  105.             (goto-char (point-max))
  106.             (insert "\n")
  107.             (setq delete-final-newline t)
  108.          )
  109.       )
  110.       ; Insert a blank line above this so it looks like start
  111.       ; of paragraph. Record the newline position in start-marker.
  112.       (save-excursion
  113.          (beginning-of-line 1)
  114.          (insert "\n")
  115.          (backward-char 1)
  116.          (setq start-marker (point-marker))
  117.       )
  118.       ; Delete the first line prefix
  119.       (delete-region (save-excursion (beginning-of-line 1) (point)) (point))
  120.       ; This loop moves to first line after paragraph
  121.       (while
  122.          (and (equal (forward-line 1) 0)
  123.               (equal (move-to-column start-col) start-col)
  124.               (looking-at "[^ \t\n]")
  125.               (or (not second-line-prefix)
  126.                   (equal second-line-prefix
  127.                      (buffer-substring
  128.                         (save-excursion (beginning-of-line 1) (point))
  129.                         (point)
  130.                      )
  131.                   )
  132.               )
  133.          )
  134.          ; remember second line prefix
  135.          (if second-line-prefix
  136.             nil
  137.             (setq second-line-prefix
  138.                (buffer-substring
  139.                   (save-excursion
  140.                      (beginning-of-line 1)
  141.                      (point)
  142.                   )
  143.                   (point)
  144.                )
  145.             )
  146.          )
  147.          ; delete prefix of each line in paragraph
  148.          (delete-region
  149.             (save-excursion (beginning-of-line 1) (point))
  150.             (point)
  151.          )
  152.       )
  153.       ; get a marker to blank line at end of paragraph
  154.       (beginning-of-line 1)
  155.       (insert "\n")
  156.       (backward-char 1)
  157.       (setq end-marker (point-marker))
  158.       ; go fill the paragraph
  159.       (goto-char (marker-position start-marker))
  160.       (forward-line 1)
  161.       (setq fill-column (- fill-column start-col))
  162.       (setq fill-prefix nil)
  163.       (fill-paragraph prf)
  164.       (setq fill-column orig-fill-column)
  165.       (setq fill-prefix orig-fill-prefix)
  166.       ; if only 1 line, use same prefix for all.
  167.       (if second-line-prefix
  168.          nil
  169.          (setq second-line-prefix first-line-prefix)
  170.       )
  171.       ; Insert first line prefix at first line and
  172.       ; second line prefix in all other lines.
  173.       (goto-char (marker-position start-marker))
  174.       (set-marker start-marker nil)
  175.       (delete-char 1)
  176.       (insert first-line-prefix)
  177.       (while
  178.          (and (equal (forward-line 1) 0)
  179.               (< (point) (marker-position end-marker))
  180.          )
  181.          (insert second-line-prefix)
  182.       )
  183.       (set-marker end-marker nil)
  184.       (delete-char 1)
  185.       ; Leave buffer terminated the way we found it.
  186.       (if delete-final-newline
  187.          (save-excursion
  188.             (goto-char (point-max))
  189.             (backward-delete-char 1 nil)
  190.          )
  191.       )
  192.       ; go back to the beginning of the paragraph
  193.       (goto-char start-pos)
  194.    )
  195. )
  196. ;-------------------cut here, end of paraform.el------------------------------
  197. ;--
  198. ;=====================================================================
  199. ;    usenet: tahorsley@ssd.harris.com  USMail: Tom Horsley
  200. ;compuserve: 76505,364                         511 Kingbird Circle
  201. ;     genie: T.HORSLEY                         Delray Beach, FL  33444
  202. ;======================== Aging: Just say no! ========================
  203.