]> git.notmuchmail.org Git - notmuch/blob - emacs/coolj.el
0385872fb655340c6d10737ed9b1250698e011f2
[notmuch] / emacs / coolj.el
1 ;;; coolj.el --- automatically wrap long lines  -*- coding:utf-8 -*-
2
3 ;; Copyright (C) 2000, 2001, 2004-2009 Free Software Foundation, Inc.
4
5 ;; Authors:    Kai Grossjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
6 ;;             Alex Schroeder <alex@gnu.org>
7 ;;             Chong Yidong <cyd@stupidchicken.com>
8 ;; Maintainer: David Edmondson <dme@dme.org>
9 ;; Keywords: convenience, wp
10
11 ;; This file is not part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;;; This is a simple derivative of some functionality from
29 ;;; `longlines.el'. The key difference is that this version will
30 ;;; insert a prefix at the head of each wrapped line. The prefix is
31 ;;; calculated from the originating long line.
32
33 ;;; No minor-mode is provided, the caller is expected to call
34 ;;; `coolj-wrap-region' to wrap the region of interest.
35
36 ;;; Code:
37
38 (defgroup coolj nil
39   "Wrapping of long lines with prefix."
40   :group 'fill)
41
42 (defcustom coolj-wrap-follows-window-size t
43   "Non-nil means wrap text to the window size.
44 Otherwise respect `fill-column'."
45   :group 'coolj
46   :type 'boolean)
47
48 (defcustom coolj-line-prefix-regexp "^\\(>+ \\)*"
49   "Regular expression that matches line prefixes."
50   :group 'coolj
51   :type 'regexp)
52
53 (defvar-local coolj-wrap-point nil)
54
55 (defun coolj-determine-prefix ()
56   "Determine the prefix for the current line."
57   (save-excursion
58     (beginning-of-line)
59     (if (re-search-forward coolj-line-prefix-regexp nil t)
60         (buffer-substring (match-beginning 0) (match-end 0))
61       "")))
62
63 (defun coolj-wrap-buffer ()
64   "Wrap the current buffer."
65   (coolj-wrap-region (point-min) (point-max)))
66
67 (defun coolj-wrap-region (beg end)
68   "Wrap each successive line, starting with the line before BEG.
69 Stop when we reach lines after END that don't need wrapping, or the
70 end of the buffer."
71   (setq fill-column (if coolj-wrap-follows-window-size
72                         (window-width)
73                       fill-column))
74   (let ((mod (buffer-modified-p)))
75     (setq coolj-wrap-point (point))
76     (goto-char beg)
77     (forward-line -1)
78     ;; Two successful coolj-wrap-line's in a row mean successive
79     ;; lines don't need wrapping.
80     (while (null (and (coolj-wrap-line)
81                       (or (eobp)
82                           (and (>= (point) end)
83                                (coolj-wrap-line))))))
84     (goto-char coolj-wrap-point)
85     (set-buffer-modified-p mod)))
86
87 (defun coolj-wrap-line ()
88   "If the current line needs to be wrapped, wrap it and return nil.
89 If wrapping is performed, point remains on the line.  If the line does
90 not need to be wrapped, move point to the next line and return t."
91   (let ((prefix (coolj-determine-prefix)))
92     (if (coolj-set-breakpoint prefix)
93         (progn
94           (insert-before-markers ?\n)
95           (backward-char 1)
96           (delete-char -1)
97           (forward-char 1)
98           (insert-before-markers prefix)
99           nil)
100       (forward-line 1)
101       t)))
102
103 (defun coolj-set-breakpoint (prefix)
104   "Place point where we should break the current line, and return t.
105 If the line should not be broken, return nil; point remains on the
106 line."
107   (move-to-column fill-column)
108   (and (re-search-forward "[^ ]" (line-end-position) 1)
109        (> (current-column) fill-column)
110        ;; This line is too long.  Can we break it?
111        (or (coolj-find-break-backward prefix)
112            (progn (move-to-column fill-column)
113                   (coolj-find-break-forward)))))
114
115 (defun coolj-find-break-backward (prefix)
116   "Move point backward to the first available breakpoint and return t.
117 If no breakpoint is found, return nil."
118   (let ((end-of-prefix (+ (line-beginning-position) (length prefix))))
119     (and (search-backward " " end-of-prefix 1)
120          (save-excursion
121            (skip-chars-backward " " end-of-prefix)
122            (null (bolp)))
123          (progn (forward-char 1)
124                 (if (and fill-nobreak-predicate
125                          (run-hook-with-args-until-success
126                           'fill-nobreak-predicate))
127                     (progn (skip-chars-backward " " end-of-prefix)
128                            (coolj-find-break-backward prefix))
129                   t)))))
130
131 (defun coolj-find-break-forward ()
132   "Move point forward to the first available breakpoint and return t.
133 If no break point is found, return nil."
134   (and (search-forward " " (line-end-position) 1)
135        (progn (skip-chars-forward " " (line-end-position))
136               (null (eolp)))
137        (if (and fill-nobreak-predicate
138                 (run-hook-with-args-until-success
139                  'fill-nobreak-predicate))
140            (coolj-find-break-forward)
141          t)))
142
143 (provide 'coolj)
144
145 ;;; coolj.el ends here