refill.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; refill.el --- `auto-fill' by refilling paragraphs on changes
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Love <fx@gnu.org>
  4. ;; Maintainer: Miles Bader <miles@gnu.org>
  5. ;; Keywords: wp
  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. ;; Provides a mode where paragraphs are refilled after changes in them
  19. ;; (using `after-change-functions'). This gives something akin to typical
  20. ;; word processor-style filling. We restrict refilling due to
  21. ;; self-insertion to the characters which trigger auto-fill.
  22. ;; It partly satisfies a todo item in enriched.el for some value of
  23. ;; `without slowing down editing too much'. It doesn't attempt to do
  24. ;; anything (using `window-size-change-functions'?) about resizing
  25. ;; windows -- who cares?
  26. ;; This implementation is probably fragile and missing some special
  27. ;; cases -- not extensively tested. Yanking paragraph breaks, for
  28. ;; instance, won't DTRT by refilling all the relevant paragraphs.
  29. ;; You could do it a bit more efficiently (and robustly?) with just an
  30. ;; auto-fill function, but that doesn't cope with changes other than
  31. ;; through self-insertion. (Using auto-fill and after-change
  32. ;; functions together didn't seem winning.) This could probably
  33. ;; benefit from a less-general and faster `fill-paragraph-function',
  34. ;; ideally as a primitive.
  35. ;; The work is done in a local post-command hook but only if
  36. ;; `refill-doit' has been set by the after-change function. Using
  37. ;; `post-command-hook' ensures simply that refilling only happens
  38. ;; once per command.
  39. ;; [Per Abrahamsen's maniac.el does a similar thing, but operates from
  40. ;; post-command-hook. I don't understand the statement in it that
  41. ;; after-change-functions don't work for this purpose; perhaps there was
  42. ;; some Emacs bug at the time. ISTR maniac has problems with
  43. ;; whitespace at the end of paragraphs.]
  44. ;;; Todo/Bugs:
  45. ;; - When deleting the first word on a line, the space after that word tends
  46. ;; to become part of the fill-prefix, causing either wrong filling of the
  47. ;; remaining text, or causing the cursor to move unexpectedly. Ex:
  48. ;; Start with
  49. ;; I>< blabla
  50. ;;
  51. ;; and hit backspace. We end up with
  52. ;;
  53. ;; ><blabla
  54. ;; instead of
  55. ;; >< blabla
  56. ;;
  57. ;; Other example. Start with
  58. ;;
  59. ;; Foo bar blablabla asdgf
  60. ;; word>< asdfas dfasdfasd
  61. ;; asd asdfa sdfasd sdf
  62. ;;
  63. ;; and hit M-backspace. We end up with
  64. ;;
  65. ;; Foo bar blablabla asdgf
  66. ;; ><asdfas dfasdfasd asd
  67. ;; asdfa sdfasd sdf
  68. ;;; Code:
  69. (eval-when-compile (require 'cl))
  70. (defgroup refill nil
  71. "Refilling paragraphs on changes."
  72. :group 'fill)
  73. (defvar refill-ignorable-overlay nil
  74. "Portion of the most recently filled paragraph not needing filling.
  75. This is used to optimize refilling.")
  76. (make-variable-buffer-local 'refill-ignorable-overlay)
  77. (defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len)
  78. "Adjust OVERLAY to not include the about-to-be-modified region."
  79. (when (not afterp)
  80. (save-excursion
  81. (goto-char beg)
  82. (forward-line -1)
  83. (if (<= (point) (overlay-start overlay))
  84. ;; Just get OVERLAY out of the way
  85. (move-overlay overlay (point-min) (point-min))
  86. ;; Make overlay contain only the region
  87. (move-overlay overlay (overlay-start overlay) (point))))))
  88. (defun refill-fill-paragraph-at (pos &optional arg)
  89. "Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end."
  90. (save-excursion
  91. (goto-char pos)
  92. ;; FIXME: forward-paragraph seems to disregard `use-hard-newlines',
  93. ;; leading to excessive refilling and wrong choice of fill-prefix.
  94. ;; might be a bug in my paragraphs.el.
  95. (forward-paragraph)
  96. (skip-syntax-backward "-")
  97. (let ((end (point))
  98. (beg (progn (backward-paragraph) (point)))
  99. (obeg (overlay-start refill-ignorable-overlay))
  100. (oend (overlay-end refill-ignorable-overlay)))
  101. (unless (> beg pos) ;Don't fill if point is outside the paragraph.
  102. (goto-char pos)
  103. (if (and (>= beg obeg) (< beg oend))
  104. ;; Limit filling to the modified tail of the paragraph.
  105. (let ( ;; When adaptive-fill-mode is enabled, the filling
  106. ;; functions will attempt to set the fill prefix from
  107. ;; the fake paragraph bounds we pass in, so set it
  108. ;; ourselves first, using the real paragraph bounds.
  109. (fill-prefix
  110. (if (and adaptive-fill-mode
  111. (or (null fill-prefix) (string= fill-prefix "")))
  112. (fill-context-prefix beg end)
  113. fill-prefix))
  114. ;; Turn off adaptive-fill-mode temporarily
  115. (adaptive-fill-mode nil))
  116. (save-restriction
  117. (if use-hard-newlines
  118. (fill-region oend end arg)
  119. (fill-region-as-paragraph oend end arg)))
  120. (move-overlay refill-ignorable-overlay obeg (point)))
  121. ;; Fill the whole paragraph
  122. (save-restriction
  123. (if use-hard-newlines
  124. (fill-region beg end arg)
  125. (fill-region-as-paragraph beg end arg)))
  126. (move-overlay refill-ignorable-overlay beg (point)))))))
  127. (defun refill-fill-paragraph (arg)
  128. "Like `fill-paragraph' but don't delete whitespace at paragraph end."
  129. (refill-fill-paragraph-at (point) arg))
  130. (defvar refill-doit nil
  131. "Non-nil tells `refill-post-command-function' to do its processing.
  132. Set by `refill-after-change-function' in `after-change-functions' and
  133. unset by `refill-post-command-function' in `post-command-hook', and
  134. sometimes `refill-pre-command-function' in `pre-command-hook'. This
  135. ensures refilling is only done once per command that causes a change,
  136. regardless of the number of after-change calls from commands doing
  137. complex processing.")
  138. (make-variable-buffer-local 'refill-doit)
  139. (defun refill-after-change-function (beg end len)
  140. "Function for `after-change-functions' which just sets `refill-doit'."
  141. (unless undo-in-progress
  142. (setq refill-doit end)))
  143. (defun refill-post-command-function ()
  144. "Post-command function to do refilling (conditionally)."
  145. (when refill-doit ; there was a change
  146. ;; There's probably scope for more special cases here...
  147. (case this-command
  148. (self-insert-command
  149. ;; Treat self-insertion commands specially, since they don't
  150. ;; always reset `refill-doit' -- for self-insertion commands that
  151. ;; *don't* cause a refill, we want to leave it turned on so that
  152. ;; any subsequent non-modification command will cause a refill.
  153. (when (aref auto-fill-chars (char-before))
  154. ;; Respond to the same characters as auto-fill (other than
  155. ;; newline, covered below).
  156. (refill-fill-paragraph-at refill-doit)
  157. (setq refill-doit nil)))
  158. ((quoted-insert fill-paragraph fill-region) nil)
  159. ((newline newline-and-indent open-line indent-new-comment-line
  160. reindent-then-newline-and-indent)
  161. ;; Don't zap what was just inserted.
  162. (save-excursion
  163. (beginning-of-line) ; for newline-and-indent
  164. (skip-chars-backward "\n")
  165. (save-restriction
  166. (narrow-to-region (point-min) (point))
  167. (refill-fill-paragraph-at refill-doit)))
  168. (widen)
  169. (save-excursion
  170. (skip-chars-forward "\n")
  171. (save-restriction
  172. (narrow-to-region (line-beginning-position) (point-max))
  173. (refill-fill-paragraph-at refill-doit))))
  174. (t
  175. (refill-fill-paragraph-at refill-doit)))
  176. (setq refill-doit nil)))
  177. (defun refill-pre-command-function ()
  178. "Pre-command function to do refilling (conditionally)."
  179. (when (and refill-doit (not (eq this-command 'self-insert-command)))
  180. ;; A previous setting of `refill-doit' didn't result in a refill,
  181. ;; because it was a self-insert-command. Since the next command is
  182. ;; something else, do the refill now.
  183. (refill-fill-paragraph-at refill-doit)
  184. (setq refill-doit nil)))
  185. (defvar refill-saved-state nil)
  186. ;;;###autoload
  187. (define-minor-mode refill-mode
  188. "Toggle automatic refilling (Refill mode).
  189. With a prefix argument ARG, enable Refill mode if ARG is
  190. positive, and disable it otherwise. If called from Lisp, enable
  191. the mode if ARG is omitted or nil.
  192. Refill mode is a buffer-local minor mode. When enabled, the
  193. current paragraph is refilled as you edit. Self-inserting
  194. characters only cause refilling if they would cause
  195. auto-filling.
  196. For true \"word wrap\" behavior, use `visual-line-mode' instead."
  197. :group 'refill
  198. :lighter " Refill"
  199. :keymap '(("\177" . backward-delete-char-untabify))
  200. ;; Remove old state if necessary
  201. (when refill-ignorable-overlay
  202. (delete-overlay refill-ignorable-overlay)
  203. (kill-local-variable 'refill-ignorable-overlay))
  204. (when (local-variable-p 'refill-saved-state)
  205. (dolist (x refill-saved-state)
  206. (set (make-local-variable (car x)) (cdr x)))
  207. (kill-local-variable 'refill-saved-state))
  208. (if refill-mode
  209. (progn
  210. (add-hook 'after-change-functions 'refill-after-change-function nil t)
  211. (add-hook 'post-command-hook 'refill-post-command-function nil t)
  212. (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
  213. (set (make-local-variable 'refill-saved-state)
  214. (mapcar (lambda (s) (cons s (symbol-value s)))
  215. '(fill-paragraph-function auto-fill-function)))
  216. ;; This provides the test for recursive paragraph filling.
  217. (set (make-local-variable 'fill-paragraph-function)
  218. 'refill-fill-paragraph)
  219. ;; When using justification, doing DEL on 2 spaces should remove
  220. ;; both, otherwise, the subsequent refill will undo the DEL.
  221. (set (make-local-variable 'backward-delete-char-untabify-method)
  222. 'hungry)
  223. (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
  224. (overlay-put refill-ignorable-overlay 'modification-hooks
  225. '(refill-adjust-ignorable-overlay))
  226. (overlay-put refill-ignorable-overlay 'insert-behind-hooks
  227. '(refill-adjust-ignorable-overlay))
  228. (auto-fill-mode 0))
  229. (remove-hook 'after-change-functions 'refill-after-change-function t)
  230. (remove-hook 'post-command-hook 'refill-post-command-function t)
  231. (kill-local-variable 'backward-delete-char-untabify-method)))
  232. (provide 'refill)
  233. ;;; refill.el ends here