1 ;;; adaptive-wrap.el --- Smart line-wrapping with wrap-prefix
3 ;; Copyright (C) 2011 Stefan Monnier
5 ;; Author: Stephen Berman <stephen.berman@gmx.net>
6 ;; Stefan Monnier <monnier@iro.umontreal.ca>
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; This package provides the `adaptive-wrap-prefix-mode' minor mode which sets
25 ;; the wrap-prefix property on the fly so that single-long-line paragraphs get
26 ;; word-wrapped in a way similar to what you'd get with M-q using
27 ;; adaptive-fill-mode, but without actually changing the buffer's text.
31 (defun adaptive-wrap-prefix-function (beg end)
32 "Indent the region between BEG and END with adaptive filling."
34 (while (< (point) end)
35 (let ((blp (line-beginning-position)))
36 (put-text-property (point)
37 (progn (search-forward "\n" end 'move) (point))
39 (fill-context-prefix blp (point))))))
42 (define-minor-mode adaptive-wrap-prefix-mode
43 "Wrap the buffer text with adaptive filling."
45 (if adaptive-wrap-prefix-mode
46 (jit-lock-register #'adaptive-wrap-prefix-function)
47 (jit-lock-unregister #'adaptive-wrap-prefix-function)
48 (with-silent-modifications
51 (remove-text-properties (point-min) (point-max) '(wrap-prefix nil))))))
54 (provide 'adaptive-wrap)
55 ;;; adaptive-wrap.el ends here