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 (defcustom adaptive-wrap-extra-indent 0
32 "Number of extra spaces to indent in `adaptive-wrap-prefix-mode'.
34 `adaptive-wrap-prefix-mode' indents the visual lines to
35 the level of the actual line plus `adaptive-wrap-extra-indent'.
36 A negative value will do a relative de-indent.
43 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
44 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
45 enim ad minim veniam, quis nostrud exercitation ullamco laboris
46 nisi ut aliquip ex ea commodo consequat.
51 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
52 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
53 enim ad minim veniam, quis nostrud exercitation ullamco laboris
54 nisi ut aliquip ex ea commodo consequat."
57 (make-local-variable 'adaptive-wrap-extra-indent)
59 (defun adaptive-wrap-fill-context-prefix (beg en)
60 "Like `fill-context-prefix', but with length adjusted by `adaptive-wrap-extra-indent'."
61 (let* ((fcp (fill-context-prefix beg en))
62 (fcp-len (string-width fcp))
63 (fill-char (if (< 0 fcp-len)
64 (string-to-char (substring fcp -1))
67 ((= 0 adaptive-wrap-extra-indent)
69 ((< 0 adaptive-wrap-extra-indent)
71 (make-string adaptive-wrap-extra-indent fill-char)))
72 ((< 0 (+ adaptive-wrap-extra-indent fcp-len))
75 (+ adaptive-wrap-extra-indent fcp-len)))
79 (defun adaptive-wrap-prefix-function (beg end)
80 "Indent the region between BEG and END with adaptive filling."
82 (while (< (point) end)
83 (let ((lbp (line-beginning-position)))
84 (put-text-property (point)
85 (progn (search-forward "\n" end 'move) (point))
87 (adaptive-wrap-fill-context-prefix lbp (point))))))
90 (define-minor-mode adaptive-wrap-prefix-mode
91 "Wrap the buffer text with adaptive filling."
94 (if adaptive-wrap-prefix-mode
95 (jit-lock-register #'adaptive-wrap-prefix-function)
96 (jit-lock-unregister #'adaptive-wrap-prefix-function)
97 (with-silent-modifications
100 (remove-text-properties (point-min) (point-max) '(wrap-prefix nil))))))
102 (provide 'adaptive-wrap)
103 ;;; adaptive-wrap.el ends here