]> code.delx.au - gnu-emacs-elpa/blob - packages/adaptive-wrap-prefix/adaptive-wrap-prefix.el
c0e312299302c4ef64b37f7fe1a19e9d5f9c0127
[gnu-emacs-elpa] / packages / adaptive-wrap-prefix / adaptive-wrap-prefix.el
1 ;;; adaptive-wrap-prefix.el --- Perform 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 (defun adaptive-wrap-prefix-function (beg end)
32 "Indent the region between BEG and END with adaptive filling."
33 (goto-char beg)
34 (while (< (point) end)
35 (let ((blp (line-beginning-position)))
36 (put-text-property (point)
37 (progn (search-forward "\n" end 'move) (point))
38 'wrap-prefix
39 (fill-context-prefix blp (point))))))
40
41 ;;;###autoload
42 (define-minor-mode adaptive-wrap-prefix-mode
43 "Wrap the buffer text with adaptive filling."
44 :lighter ""
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
49 (save-restriction
50 (widen)
51 (remove-text-properties (point-min) (point-max) '(wrap-prefix nil))))))
52
53
54 (provide 'adaptive-wrap-prefix)
55 ;;; adaptive-wrap-prefix.el ends here