PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/textmodes/refill.el

https://gitlab.com/RobertCochran/emacs
Emacs Lisp | 262 lines | 129 code | 31 blank | 102 comment | 2 complexity | e0aeea06119440a6c3ecfd9a3f2fb457 MD5 | raw file
  1. ;;; refill.el --- `auto-fill' by refilling paragraphs on changes
  2. ;; Copyright (C) 2000-2019 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 <https://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. ;; Unused.
  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. (pcase 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. ((or 'quoted-insert 'fill-paragraph 'fill-region) nil)
  159. ((or 'newline 'newline-and-indent 'open-line 'indent-new-comment-line
  160. 'default-indent-new-line '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. (_
  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. Refill mode is a buffer-local minor mode. When enabled, the
  190. current paragraph is refilled as you edit. Self-inserting
  191. characters only cause refilling if they would cause
  192. auto-filling.
  193. For true \"word wrap\" behavior, use `visual-line-mode' instead."
  194. ;; Not global, so no effect.
  195. ;;; :group 'refill
  196. :lighter " Refill"
  197. :keymap '(("\177" . backward-delete-char-untabify))
  198. ;; Remove old state if necessary
  199. (when refill-ignorable-overlay
  200. (delete-overlay refill-ignorable-overlay)
  201. (kill-local-variable 'refill-ignorable-overlay))
  202. (when (local-variable-p 'refill-saved-state)
  203. (dolist (x refill-saved-state)
  204. (set (make-local-variable (car x)) (cdr x)))
  205. (kill-local-variable 'refill-saved-state))
  206. (if refill-mode
  207. (progn
  208. (add-hook 'after-change-functions 'refill-after-change-function nil t)
  209. (add-hook 'post-command-hook 'refill-post-command-function nil t)
  210. (add-hook 'pre-command-hook 'refill-pre-command-function nil t)
  211. (set (make-local-variable 'refill-saved-state)
  212. (mapcar (lambda (s) (cons s (symbol-value s)))
  213. '(fill-paragraph-function auto-fill-function)))
  214. ;; This provides the test for recursive paragraph filling.
  215. (set (make-local-variable 'fill-paragraph-function)
  216. 'refill-fill-paragraph)
  217. ;; When using justification, doing DEL on 2 spaces should remove
  218. ;; both, otherwise, the subsequent refill will undo the DEL.
  219. (set (make-local-variable 'backward-delete-char-untabify-method)
  220. 'hungry)
  221. (setq refill-ignorable-overlay (make-overlay 1 1 nil nil t))
  222. (overlay-put refill-ignorable-overlay 'modification-hooks
  223. '(refill-adjust-ignorable-overlay))
  224. (overlay-put refill-ignorable-overlay 'insert-behind-hooks
  225. '(refill-adjust-ignorable-overlay))
  226. (auto-fill-mode 0))
  227. (remove-hook 'after-change-functions 'refill-after-change-function t)
  228. (remove-hook 'post-command-hook 'refill-post-command-function t)
  229. (kill-local-variable 'backward-delete-char-untabify-method)))
  230. (provide 'refill)
  231. ;;; refill.el ends here