1 ;;; coolj.el --- automatically wrap long lines -*- lexical-binding: t; coding: utf-8 -*-
3 ;; Copyright (C) 2000, 2001, 2004-2009 Free Software Foundation, Inc.
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
11 ;; This file is not part of GNU Emacs.
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.
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.
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/>.
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.
33 ;; No minor-mode is provided, the caller is expected to call
34 ;; `coolj-wrap-region' to wrap the region of interest.
39 "Wrapping of long lines with prefix."
42 (defcustom coolj-wrap-follows-window-size t
43 "Non-nil means wrap text to the window size.
44 Otherwise respect `fill-column'."
48 (defcustom coolj-line-prefix-regexp "^\\(>+ \\)*"
49 "Regular expression that matches line prefixes."
53 (defvar-local coolj-wrap-point nil)
55 (defun coolj-determine-prefix ()
56 "Determine the prefix for the current line."
59 (if (re-search-forward coolj-line-prefix-regexp nil t)
60 (buffer-substring (match-beginning 0) (match-end 0))
63 (defun coolj-wrap-buffer ()
64 "Wrap the current buffer."
65 (coolj-wrap-region (point-min) (point-max)))
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
71 (setq fill-column (if coolj-wrap-follows-window-size
74 (let ((mod (buffer-modified-p)))
75 (setq coolj-wrap-point (point))
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)
83 (coolj-wrap-line))))))
84 (goto-char coolj-wrap-point)
85 (set-buffer-modified-p mod)))
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)
94 (insert-before-markers ?\n)
98 (insert-before-markers prefix)
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
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)))))
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)
121 (skip-chars-backward " " end-of-prefix)
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))
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))
137 (if (and fill-nobreak-predicate
138 (run-hook-with-args-until-success
139 'fill-nobreak-predicate))
140 (coolj-find-break-forward)
145 ;;; coolj.el ends here