flow-fill.el 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. ;;; flow-fill.el --- interpret RFC2646 "flowed" text
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <jas@pdc.kth.se>
  4. ;; Keywords: mail
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This implement decoding of RFC2646 formatted text, including the
  18. ;; quoted-depth wins rules.
  19. ;; Theory of operation: search for lines ending with SPC, save quote
  20. ;; length of line, remove SPC and concatenate line with the following
  21. ;; line if quote length of following line matches current line.
  22. ;; When no further concatenations are possible, we've found a
  23. ;; paragraph and we let `fill-region' fill the long line into several
  24. ;; lines with the quote prefix as `fill-prefix'.
  25. ;; Todo: implement basic `fill-region' (Emacs and XEmacs
  26. ;; implementations differ..)
  27. ;;; History:
  28. ;; 2000-02-17 posted on ding mailing list
  29. ;; 2000-02-19 use `point-at-{b,e}ol' in XEmacs
  30. ;; 2000-03-11 no compile warnings for point-at-bol stuff
  31. ;; 2000-03-26 committed to gnus cvs
  32. ;; 2000-10-23 don't flow "-- " lines, make "quote-depth wins" rule
  33. ;; work when first line is at level 0.
  34. ;; 2002-01-12 probably incomplete encoding support
  35. ;; 2003-12-08 started working on test harness.
  36. ;;; Code:
  37. (eval-when-compile (require 'cl))
  38. (defcustom fill-flowed-display-column 'fill-column
  39. "Column beyond which format=flowed lines are wrapped, when displayed.
  40. This can be a Lisp expression or an integer."
  41. :version "22.1"
  42. :group 'mime-display
  43. :type '(choice (const :tag "Standard `fill-column'" fill-column)
  44. (const :tag "Fit Window" (- (window-width) 5))
  45. (sexp)
  46. (integer)))
  47. (defcustom fill-flowed-encode-column 66
  48. "Column beyond which format=flowed lines are wrapped, in outgoing messages.
  49. This can be a Lisp expression or an integer.
  50. RFC 2646 suggests 66 characters for readability."
  51. :version "22.1"
  52. :group 'mime-display
  53. :type '(choice (const :tag "Standard fill-column" fill-column)
  54. (const :tag "RFC 2646 default (66)" 66)
  55. (sexp)
  56. (integer)))
  57. ;;;###autoload
  58. (defun fill-flowed-encode (&optional buffer)
  59. (with-current-buffer (or buffer (current-buffer))
  60. ;; No point in doing this unless hard newlines is used.
  61. (when use-hard-newlines
  62. (let ((start (point-min)) end)
  63. ;; Go through each paragraph, filling it and adding SPC
  64. ;; as the last character on each line.
  65. (while (setq end (text-property-any start (point-max) 'hard 't))
  66. (save-restriction
  67. (narrow-to-region start end)
  68. (let ((fill-column (eval fill-flowed-encode-column)))
  69. (fill-flowed-fill-buffer))
  70. (goto-char (point-min))
  71. (while (re-search-forward "\n" nil t)
  72. (replace-match " \n" t t))
  73. (goto-char (setq start (1+ (point-max)))))))
  74. t)))
  75. (defun fill-flowed-fill-buffer ()
  76. (let ((prefix nil)
  77. (prev-prefix nil)
  78. (start (point-min)))
  79. (goto-char (point-min))
  80. (while (not (eobp))
  81. (setq prefix (and (looking-at "[> ]+")
  82. (match-string 0)))
  83. (if (equal prefix prev-prefix)
  84. (forward-line 1)
  85. (save-restriction
  86. (narrow-to-region start (point))
  87. (let ((fill-prefix prev-prefix))
  88. (fill-region (point-min) (point-max) t 'nosqueeze 'to-eop))
  89. (goto-char (point-max)))
  90. (setq prev-prefix prefix
  91. start (point))))
  92. (save-restriction
  93. (narrow-to-region start (point))
  94. (let ((fill-prefix prev-prefix))
  95. (fill-region (point-min) (point-max) t 'nosqueeze 'to-eop)))))
  96. ;;;###autoload
  97. (defun fill-flowed (&optional buffer delete-space)
  98. (with-current-buffer (or (current-buffer) buffer)
  99. (goto-char (point-min))
  100. ;; Remove space stuffing.
  101. (while (re-search-forward "^\\( \\|>+ $\\)" nil t)
  102. (delete-char -1)
  103. (forward-line 1))
  104. (goto-char (point-min))
  105. (while (re-search-forward " $" nil t)
  106. (when (save-excursion
  107. (beginning-of-line)
  108. (looking-at "^\\(>*\\)\\( ?\\)"))
  109. (let ((quote (match-string 1))
  110. sig)
  111. (if (string= quote "")
  112. (setq quote nil))
  113. (when (and quote (string= (match-string 2) ""))
  114. (save-excursion
  115. ;; insert SP after quote for pleasant reading of quoted lines
  116. (beginning-of-line)
  117. (when (> (skip-chars-forward ">") 0)
  118. (insert " "))))
  119. ;; XXX slightly buggy handling of "-- "
  120. (while (and (save-excursion
  121. (ignore-errors (backward-char 3))
  122. (setq sig (looking-at "-- "))
  123. (looking-at "[^-][^-] "))
  124. (save-excursion
  125. (unless (eobp)
  126. (forward-char 1)
  127. (looking-at (format "^\\(%s\\)\\([^>\n\r]\\)"
  128. (or quote " ?"))))))
  129. (save-excursion
  130. (replace-match (if (string= (match-string 2) " ")
  131. "" "\\2")))
  132. (backward-delete-char -1)
  133. (when delete-space
  134. (delete-char -1))
  135. (end-of-line))
  136. (unless sig
  137. (condition-case nil
  138. (let ((fill-prefix (when quote (concat quote " ")))
  139. (fill-column (eval fill-flowed-display-column))
  140. filladapt-mode
  141. adaptive-fill-mode)
  142. (fill-region (point-at-bol)
  143. (min (1+ (point-at-eol))
  144. (point-max))
  145. 'left 'nosqueeze))
  146. (error
  147. (forward-line 1)
  148. nil))))))))
  149. ;; Test vectors.
  150. (defvar show-trailing-whitespace)
  151. (defvar fill-flowed-encode-tests
  152. `(
  153. ;; The syntax of each list element is:
  154. ;; (INPUT . EXPECTED-OUTPUT)
  155. (,(concat
  156. "> Thou villainous ill-breeding spongy dizzy-eyed \n"
  157. "> reeky elf-skinned pigeon-egg! \n"
  158. ">> Thou artless swag-bellied milk-livered \n"
  159. ">> dismal-dreaming idle-headed scut!\n"
  160. ">>> Thou errant folly-fallen spleeny reeling-ripe \n"
  161. ">>> unmuzzled ratsbane!\n"
  162. ">>>> Henceforth, the coding style is to be strictly \n"
  163. ">>>> enforced, including the use of only upper case.\n"
  164. ">>>>> I've noticed a lack of adherence to the coding \n"
  165. ">>>>> styles, of late.\n"
  166. ">>>>>> Any complaints?")
  167. .
  168. ,(concat
  169. "> Thou villainous ill-breeding spongy dizzy-eyed reeky elf-skinned\n"
  170. "> pigeon-egg! \n"
  171. ">> Thou artless swag-bellied milk-livered dismal-dreaming idle-headed\n"
  172. ">> scut!\n"
  173. ">>> Thou errant folly-fallen spleeny reeling-ripe unmuzzled ratsbane!\n"
  174. ">>>> Henceforth, the coding style is to be strictly enforced,\n"
  175. ">>>> including the use of only upper case.\n"
  176. ">>>>> I've noticed a lack of adherence to the coding styles, of late.\n"
  177. ">>>>>> Any complaints?\n"
  178. ))
  179. ;; (,(concat
  180. ;; "\n"
  181. ;; "> foo\n"
  182. ;; "> \n"
  183. ;; "> \n"
  184. ;; "> bar\n")
  185. ;; .
  186. ;; ,(concat
  187. ;; "\n"
  188. ;; "> foo bar\n"))
  189. ))
  190. (defun fill-flowed-test ()
  191. (interactive "")
  192. (switch-to-buffer (get-buffer-create "*Format=Flowed test output*"))
  193. (erase-buffer)
  194. (setq show-trailing-whitespace t)
  195. (dolist (test fill-flowed-encode-tests)
  196. (let (start output)
  197. (insert "***** BEGIN TEST INPUT *****\n")
  198. (insert (car test))
  199. (insert "***** END TEST INPUT *****\n\n")
  200. (insert "***** BEGIN TEST OUTPUT *****\n")
  201. (setq start (point))
  202. (insert (car test))
  203. (save-restriction
  204. (narrow-to-region start (point))
  205. (fill-flowed))
  206. (setq output (buffer-substring start (point-max)))
  207. (insert "***** END TEST OUTPUT *****\n")
  208. (unless (string= output (cdr test))
  209. (insert "\n***** BEGIN TEST EXPECTED OUTPUT *****\n")
  210. (insert (cdr test))
  211. (insert "***** END TEST EXPECTED OUTPUT *****\n"))
  212. (insert "\n\n")))
  213. (goto-char (point-max)))
  214. (provide 'flow-fill)
  215. ;;; flow-fill.el ends here