]> code.delx.au - gnu-emacs-elpa/blob - packages/adaptive-wrap/adaptive-wrap.el
* ampc.el: Sync to version 0.1.3.
[gnu-emacs-elpa] / packages / adaptive-wrap / adaptive-wrap.el
1 ;;; adaptive-wrap.el --- Smart line-wrapping with wrap-prefix
2
3 ;; Copyright (C) 2011 Stefan Monnier
4
5 ;; Author: Stephen Berman <stephen.berman@gmx.net>
6 ;; Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Version: 0.1
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
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.
28
29 ;;; Code:
30
31 (defcustom adaptive-wrap-extra-indent 0
32 "Number of extra spaces to indent in `adaptive-wrap-prefix-mode'.
33
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.
37
38 Examples:
39
40 actual indent = 2
41 extra indent = -1
42
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.
47
48 actual indent = 2
49 extra indent = 2
50
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."
55 :type 'integer
56 :group 'visual-line)
57 (make-local-variable 'adaptive-wrap-extra-indent)
58
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))
65 ?\ )))
66 (cond
67 ((= 0 adaptive-wrap-extra-indent)
68 fcp)
69 ((< 0 adaptive-wrap-extra-indent)
70 (concat fcp
71 (make-string adaptive-wrap-extra-indent fill-char)))
72 ((< 0 (+ adaptive-wrap-extra-indent fcp-len))
73 (substring fcp
74 0
75 (+ adaptive-wrap-extra-indent fcp-len)))
76 (t
77 ""))))
78
79 (defun adaptive-wrap-prefix-function (beg end)
80 "Indent the region between BEG and END with adaptive filling."
81 (goto-char beg)
82 (while (< (point) end)
83 (let ((lbp (line-beginning-position)))
84 (put-text-property (point)
85 (progn (search-forward "\n" end 'move) (point))
86 'wrap-prefix
87 (adaptive-wrap-fill-context-prefix lbp (point))))))
88
89 ;;;###autoload
90 (define-minor-mode adaptive-wrap-prefix-mode
91 "Wrap the buffer text with adaptive filling."
92 :lighter ""
93 :group 'visual-line
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
98 (save-restriction
99 (widen)
100 (remove-text-properties (point-min) (point-max) '(wrap-prefix nil))))))
101
102 (provide 'adaptive-wrap)
103 ;;; adaptive-wrap.el ends here