-
Notifications
You must be signed in to change notification settings - Fork 3
/
adaptive-wrap.el
103 lines (84 loc) · 3.67 KB
/
adaptive-wrap.el
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
;;; adaptive-wrap.el --- Smart line-wrapping with wrap-prefix
;; Copyright (C) 2011 Stefan Monnier
;; Author: Stephen Berman <[email protected]>
;; Stefan Monnier <[email protected]>
;; Version: 0.1
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides the `adaptive-wrap-prefix-mode' minor mode which sets
;; the wrap-prefix property on the fly so that single-long-line paragraphs get
;; word-wrapped in a way similar to what you'd get with M-q using
;; adaptive-fill-mode, but without actually changing the buffer's text.
;;; Code:
(defcustom adaptive-wrap-extra-indent 0
"Number of extra spaces to indent in `adaptive-wrap-prefix-mode'.
`adaptive-wrap-prefix-mode' indents the visual lines to
the level of the actual line plus `adaptive-wrap-extra-indent'.
A negative value will do a relative de-indent.
Examples:
actual indent = 2
extra indent = -1
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
actual indent = 2
extra indent = 2
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat."
:type 'integer
:group 'visual-line)
(make-local-variable 'adaptive-wrap-extra-indent)
(defun adaptive-wrap-fill-context-prefix (beg en)
"Like `fill-context-prefix', but with length adjusted by `adaptive-wrap-extra-indent'."
(let* ((fcp (fill-context-prefix beg en))
(fcp-len (string-width fcp))
(fill-char (if (< 0 fcp-len)
(string-to-char (substring fcp -1))
?\ )))
(cond
((= 0 adaptive-wrap-extra-indent)
fcp)
((< 0 adaptive-wrap-extra-indent)
(concat fcp
(make-string adaptive-wrap-extra-indent fill-char)))
((< 0 (+ adaptive-wrap-extra-indent fcp-len))
(substring fcp
0
(+ adaptive-wrap-extra-indent fcp-len)))
(t
""))))
(defun adaptive-wrap-prefix-function (beg end)
"Indent the region between BEG and END with adaptive filling."
(goto-char beg)
(while (< (point) end)
(let ((lbp (line-beginning-position)))
(put-text-property (point)
(progn (search-forward "\n" end 'move) (point))
'wrap-prefix
(adaptive-wrap-fill-context-prefix lbp (point))))))
;;;###autoload
(define-minor-mode adaptive-wrap-prefix-mode
"Wrap the buffer text with adaptive filling."
:lighter ""
:group 'visual-line
(if adaptive-wrap-prefix-mode
(jit-lock-register #'adaptive-wrap-prefix-function)
(jit-lock-unregister #'adaptive-wrap-prefix-function)
(with-silent-modifications
(save-restriction
(widen)
(remove-text-properties (point-min) (point-max) '(wrap-prefix nil))))))
(provide 'adaptive-wrap)
;;; adaptive-wrap.el ends here