| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
(eval-when-compile (require 'cl)) |
|---|
| 90 |
|
|---|
| 91 |
(defgroup refill nil |
|---|
| 92 |
"Refilling paragraphs on changes." |
|---|
| 93 |
:group 'fill) |
|---|
| 94 |
|
|---|
| 95 |
(defvar refill-ignorable-overlay nil |
|---|
| 96 |
"Portion of the most recently filled paragraph not needing filling. |
|---|
| 97 |
This is used to optimize refilling.") |
|---|
| 98 |
(make-variable-buffer-local 'refill-ignorable-overlay) |
|---|
| 99 |
|
|---|
| 100 |
(defun refill-adjust-ignorable-overlay (overlay afterp beg end &optional len) |
|---|
| 101 |
"Adjust OVERLAY to not include the about-to-be-modified region." |
|---|
| 102 |
(when (not afterp) |
|---|
| 103 |
(save-excursion |
|---|
| 104 |
(goto-char beg) |
|---|
| 105 |
(forward-line -1) |
|---|
| 106 |
(if (<= (point) (overlay-start overlay)) |
|---|
| 107 |
|
|---|
| 108 |
(move-overlay overlay (point-min) (point-min)) |
|---|
| 109 |
|
|---|
| 110 |
(move-overlay overlay (overlay-start overlay) (point)))))) |
|---|
| 111 |
|
|---|
| 112 |
(defun refill-fill-paragraph-at (pos &optional arg) |
|---|
| 113 |
"Like `fill-paragraph' at POS, but don't delete whitespace at paragraph end." |
|---|
| 114 |
(save-excursion |
|---|
| 115 |
(goto-char pos) |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
(forward-paragraph) |
|---|
| 120 |
(skip-syntax-backward "-") |
|---|
| 121 |
(let ((end (point)) |
|---|
| 122 |
(beg (progn (backward-paragraph) (point))) |
|---|
| 123 |
(obeg (overlay-start refill-ignorable-overlay)) |
|---|
| 124 |
(oend (overlay-end refill-ignorable-overlay))) |
|---|
| 125 |
(unless (> beg pos) |
|---|
| 126 |
(goto-char pos) |
|---|
| 127 |
(if (and (>= beg obeg) (< beg oend)) |
|---|
| 128 |
|
|---|
| 129 |
(let ( |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
(fill-prefix |
|---|
| 134 |
(if (and adaptive-fill-mode |
|---|
| 135 |
(or (null fill-prefix) (string= fill-prefix ""))) |
|---|
| 136 |
(fill-context-prefix beg end) |
|---|
| 137 |
fill-prefix)) |
|---|
| 138 |
|
|---|
| 139 |
(adaptive-fill-mode nil)) |
|---|
| 140 |
(save-restriction |
|---|
| 141 |
(if use-hard-newlines |
|---|
| 142 |
(fill-region oend end arg) |
|---|
| 143 |
(fill-region-as-paragraph oend end arg))) |
|---|
| 144 |
(move-overlay refill-ignorable-overlay obeg (point))) |
|---|
| 145 |
|
|---|
| 146 |
(save-restriction |
|---|
| 147 |
(if use-hard-newlines |
|---|
| 148 |
(fill-region beg end arg) |
|---|
| 149 |
(fill-region-as-paragraph beg end arg))) |
|---|
| 150 |
(move-overlay refill-ignorable-overlay beg (point))))))) |
|---|
| 151 |
|
|---|
| 152 |
(defun refill-fill-paragraph (arg) |
|---|
| 153 |
"Like `fill-paragraph' but don't delete whitespace at paragraph end." |
|---|
| 154 |
(refill-fill-paragraph-at (point) arg)) |
|---|
| 155 |
|
|---|
| 156 |
(defvar refill-doit nil |
|---|
| 157 |
"Non-nil tells `refill-post-command-function' to do its processing. |
|---|
| 158 |
Set by `refill-after-change-function' in `after-change-functions' and |
|---|
| 159 |
unset by `refill-post-command-function' in `post-command-hook', and |
|---|
| 160 |
sometimes `refill-pre-command-function' in `pre-command-hook'. This |
|---|
| 161 |
ensures refilling is only done once per command that causes a change, |
|---|
| 162 |
regardless of the number of after-change calls from commands doing |
|---|
| 163 |
complex processing.") |
|---|
| 164 |
(make-variable-buffer-local 'refill-doit) |
|---|
| 165 |
|
|---|
| 166 |
(defun refill-after-change-function (beg end len) |
|---|
| 167 |
"Function for `after-change-functions' which just sets `refill-doit'." |
|---|
| 168 |
(unless undo-in-progress |
|---|
| 169 |
(setq refill-doit end))) |
|---|
| 170 |
|
|---|
| 171 |
(defun refill-post-command-function () |
|---|
| 172 |
"Post-command function to do refilling (conditionally)." |
|---|
| 173 |
(when refill-doit |
|---|
| 174 |
|
|---|
| 175 |
(case this-command |
|---|
| 176 |
(self-insert-command |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
(when (aref auto-fill-chars (char-before)) |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
(refill-fill-paragraph-at refill-doit) |
|---|
| 185 |
(setq refill-doit nil))) |
|---|
| 186 |
((quoted-insert fill-paragraph fill-region) nil) |
|---|
| 187 |
((newline newline-and-indent open-line indent-new-comment-line |
|---|
| 188 |
reindent-then-newline-and-indent) |
|---|
| 189 |
|
|---|
| 190 |
(save-excursion |
|---|
| 191 |
(beginning-of-line) |
|---|
| 192 |
(skip-chars-backward "\n") |
|---|
| 193 |
(save-restriction |
|---|
| 194 |
(narrow-to-region (point-min) (point)) |
|---|
| 195 |
(refill-fill-paragraph-at refill-doit))) |
|---|
| 196 |
(widen) |
|---|
| 197 |
(save-excursion |
|---|
| 198 |
(skip-chars-forward "\n") |
|---|
| 199 |
(save-restriction |
|---|
| 200 |
(narrow-to-region (line-beginning-position) (point-max)) |
|---|
| 201 |
(refill-fill-paragraph-at refill-doit)))) |
|---|
| 202 |
(t |
|---|
| 203 |
(refill-fill-paragraph-at refill-doit))) |
|---|
| 204 |
(setq refill-doit nil))) |
|---|
| 205 |
|
|---|
| 206 |
(defun refill-pre-command-function () |
|---|
| 207 |
"Pre-command function to do refilling (conditionally)." |
|---|
| 208 |
(when (and refill-doit (not (eq this-command 'self-insert-command))) |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
(refill-fill-paragraph-at refill-doit) |
|---|
| 213 |
(setq refill-doit nil))) |
|---|
| 214 |
|
|---|
| 215 |
(defvar refill-saved-state nil) |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
(define-minor-mode refill-mode |
|---|
| 219 |
"Toggle Refill minor mode. |
|---|
| 220 |
With prefix arg, turn Refill mode on if arg is positive, otherwise turn it off. |
|---|
| 221 |
|
|---|
| 222 |
When Refill mode is on, the current paragraph will be formatted when |
|---|
| 223 |
changes are made within it. Self-inserting characters only cause |
|---|
| 224 |
refilling if they would cause auto-filling." |
|---|
| 225 |
:group 'refill |
|---|
| 226 |
:lighter " Refill" |
|---|
| 227 |
:keymap '(("\177" . backward-delete-char-untabify)) |
|---|
| 228 |
|
|---|
| 229 |
(when refill-ignorable-overlay |
|---|
| 230 |
(delete-overlay refill-ignorable-overlay) |
|---|
| 231 |
(kill-local-variable 'refill-ignorable-overlay)) |
|---|
| 232 |
(when (local-variable-p 'refill-saved-state) |
|---|
| 233 |
(dolist (x refill-saved-state) |
|---|
| 234 |
(set (make-local-variable (car x)) (cdr x))) |
|---|
| 235 |
(kill-local-variable 'refill-saved-state)) |
|---|
| 236 |
(if refill-mode |
|---|
| 237 |
(progn |
|---|
| 238 |
(add-hook 'after-change-functions 'refill-after-change-function nil t) |
|---|
| 239 |
(add-hook 'post-command-hook 'refill-post-command-function nil t) |
|---|
| 240 |
(add-hook 'pre-command-hook 'refill-pre-command-function nil t) |
|---|
| 241 |
(set (make-local-variable 'refill-saved-state) |
|---|
| 242 |
(mapcar (lambda (s) (cons s (symbol-value s))) |
|---|
| 243 |
'(fill-paragraph-function auto-fill-function))) |
|---|
| 244 |
|
|---|
| 245 |
(set (make-local-variable 'fill-paragraph-function) |
|---|
| 246 |
'refill-fill-paragraph) |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
(set (make-local-variable 'backward-delete-char-untabify-method) |
|---|
| 250 |
'hungry) |
|---|
| 251 |
(setq refill-ignorable-overlay (make-overlay 1 1 nil nil t)) |
|---|
| 252 |
(overlay-put refill-ignorable-overlay 'modification-hooks |
|---|
| 253 |
'(refill-adjust-ignorable-overlay)) |
|---|
| 254 |
(overlay-put refill-ignorable-overlay 'insert-behind-hooks |
|---|
| 255 |
'(refill-adjust-ignorable-overlay)) |
|---|
| 256 |
(auto-fill-mode 0)) |
|---|
| 257 |
(remove-hook 'after-change-functions 'refill-after-change-function t) |
|---|
| 258 |
(remove-hook 'post-command-hook 'refill-post-command-function t) |
|---|
| 259 |
(kill-local-variable 'backward-delete-char-untabify-method))) |
|---|
| 260 |
|
|---|
| 261 |
(provide 'refill) |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|