PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/gnus/flow-fill.el

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