1 ;;; coolj.el --- automatically wrap long lines -*- coding:utf-8 -*-
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007, 2008, 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 coolj-wrap-point nil)
55 (make-variable-buffer-local 'coolj-wrap-point)
57 (defun coolj-determine-prefix ()
58 "Determine the prefix for the current line."
61 (if (re-search-forward coolj-line-prefix-regexp nil t)
62 (buffer-substring (match-beginning 0) (match-end 0))
65 (defun coolj-wrap-buffer ()
66 "Wrap the current buffer."
67 (coolj-wrap-region (point-min) (point-max)))
69 (defun coolj-wrap-region (beg end)
70 "Wrap each successive line, starting with the line before BEG.
71 Stop when we reach lines after END that don't need wrapping, or the
73 (setq fill-column (if coolj-wrap-follows-window-size
76 (let ((mod (buffer-modified-p)))
77 (setq coolj-wrap-point (point))
80 ;; Two successful coolj-wrap-line's in a row mean successive
81 ;; lines don't need wrapping.
82 (while (null (and (coolj-wrap-line)
85 (coolj-wrap-line))))))
86 (goto-char coolj-wrap-point)
87 (set-buffer-modified-p mod)))
89 (defun coolj-wrap-line ()
90 "If the current line needs to be wrapped, wrap it and return nil.
91 If wrapping is performed, point remains on the line. If the line does
92 not need to be wrapped, move point to the next line and return t."
93 (let ((prefix (coolj-determine-prefix)))
94 (if (coolj-set-breakpoint prefix)
96 (insert-before-markers ?\n)
100 (insert-before-markers prefix)
105 (defun coolj-set-breakpoint (prefix)
106 "Place point where we should break the current line, and return t.
107 If the line should not be broken, return nil; point remains on the
109 (move-to-column fill-column)
110 (if (and (re-search-forward "[^ ]" (line-end-position) 1)
111 (> (current-column) fill-column))
112 ;; This line is too long. Can we break it?
113 (or (coolj-find-break-backward prefix)
114 (progn (move-to-column fill-column)
115 (coolj-find-break-forward)))))
117 (defun coolj-find-break-backward (prefix)
118 "Move point backward to the first available breakpoint and return t.
119 If no breakpoint is found, return nil."
120 (let ((end-of-prefix (+ (line-beginning-position) (length prefix))))
121 (and (search-backward " " end-of-prefix 1)
123 (skip-chars-backward " " end-of-prefix)
125 (progn (forward-char 1)
126 (if (and fill-nobreak-predicate
127 (run-hook-with-args-until-success
128 'fill-nobreak-predicate))
129 (progn (skip-chars-backward " " end-of-prefix)
130 (coolj-find-break-backward prefix))
133 (defun coolj-find-break-forward ()
134 "Move point forward to the first available breakpoint and return t.
135 If no break point is found, return nil."
136 (and (search-forward " " (line-end-position) 1)
137 (progn (skip-chars-forward " " (line-end-position))
139 (if (and fill-nobreak-predicate
140 (run-hook-with-args-until-success
141 'fill-nobreak-predicate))
142 (coolj-find-break-forward)
147 ;;; coolj.el ends here