(rmail-forward-separator-regex): Fix typo in docstring.
[bpt/emacs.git] / lisp / longlines.el
1 ;;; longlines.el --- automatically wrap long lines
2
3 ;; Copyright (C) 2000, 2001, 2004, 2005 by 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: Chong Yidong <cyd@stupidchicken.com>
9 ;; Keywords: convenience
10
11 ;; This file is 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 2, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; Some text editors save text files with long lines, and they
31 ;; automatically break these lines at whitespace, without actually
32 ;; inserting any newline characters. When doing `M-q' in Emacs, you
33 ;; are inserting newline characters. Longlines mode provides a file
34 ;; format which wraps the long lines when reading a file and unwraps
35 ;; the lines when saving the file. It can also wrap and unwrap
36 ;; automatically as editing takes place.
37
38 ;; Special thanks to Rod Smith for many useful bug reports.
39
40 ;;; Code:
41
42 (defgroup longlines nil
43 "Automatic wrapping of long lines when loading files."
44 :group 'fill)
45
46 (defcustom longlines-auto-wrap t
47 "*Non-nil means long lines are automatically wrapped after each command.
48 Otherwise, you can perform filling using `fill-paragraph' or
49 `auto-fill-mode'. In any case, the soft newlines will be removed
50 when the file is saved to disk."
51 :group 'longlines
52 :type 'boolean)
53
54 (defcustom longlines-wrap-follows-window-size nil
55 "*Non-nil means wrapping and filling happen at the edge of the window.
56 Otherwise, `fill-column' is used, regardless of the window size. This
57 does not work well when the buffer is displayed in multiple windows
58 with differing widths."
59 :group 'longlines
60 :type 'boolean)
61
62 (defcustom longlines-show-hard-newlines nil
63 "*Non-nil means each hard newline is marked with a symbol.
64 You can also enable the display temporarily, using the command
65 `longlines-show-hard-newlines'"
66 :group 'longlines
67 :type 'boolean)
68
69 (defcustom longlines-show-effect (propertize "|\n" 'face 'escape-glyph)
70 "*A string to display when showing hard newlines.
71 This is used when `longlines-show-hard-newlines' is on."
72 :group 'longlines
73 :type 'string)
74
75 ;; Internal variables
76
77 (defvar longlines-wrap-beg nil)
78 (defvar longlines-wrap-end nil)
79 (defvar longlines-wrap-point nil)
80 (defvar longlines-showing nil)
81
82 (make-variable-buffer-local 'longlines-wrap-beg)
83 (make-variable-buffer-local 'longlines-wrap-end)
84 (make-variable-buffer-local 'longlines-wrap-point)
85 (make-variable-buffer-local 'longlines-showing)
86
87 ;; Mode
88
89 ;;;###autoload
90 (define-minor-mode longlines-mode
91 "Toggle Long Lines mode.
92 In Long Lines mode, long lines are wrapped if they extend beyond
93 `fill-column'. The soft newlines used for line wrapping will not
94 show up when the text is yanked or saved to disk.
95
96 If `longlines-auto-wrap' is non-nil, lines are automatically
97 wrapped whenever the buffer is changed. You can always call
98 `fill-paragraph' to fill individual paragraphs.
99
100 If `longlines-show-hard-newlines' is non-nil, hard newlines will
101 be marked by a symbol."
102 :group 'longlines :lighter " ll"
103 (if longlines-mode
104 ;; Turn on longlines mode
105 (progn
106 (use-hard-newlines 1 'never)
107 (set (make-local-variable 'require-final-newline) nil)
108 (add-to-list 'buffer-file-format 'longlines)
109 (add-hook 'change-major-mode-hook 'longlines-mode-off nil t)
110 (make-local-variable 'buffer-substring-filters)
111 (add-to-list 'buffer-substring-filters 'longlines-encode-string)
112 (when longlines-wrap-follows-window-size
113 (set (make-local-variable 'fill-column)
114 (- (window-width) window-min-width))
115 (add-hook 'window-configuration-change-hook
116 'longlines-window-change-function nil t))
117 (let ((buffer-undo-list t)
118 (mod (buffer-modified-p)))
119 ;; Turning off undo is OK since (spaces + newlines) is
120 ;; conserved, except for a corner case in
121 ;; longlines-wrap-lines that we'll never encounter from here
122 (longlines-decode-region (point-min) (point-max))
123 (longlines-wrap-region (point-min) (point-max))
124 (set-buffer-modified-p mod))
125 (when (and longlines-show-hard-newlines
126 (not longlines-showing))
127 (longlines-show-hard-newlines))
128 (when longlines-auto-wrap
129 (auto-fill-mode 0)
130 (add-hook 'after-change-functions
131 'longlines-after-change-function nil t)
132 (add-hook 'post-command-hook
133 'longlines-post-command-function nil t)))
134 ;; Turn off longlines mode
135 (setq buffer-file-format (delete 'longlines buffer-file-format))
136 (if longlines-showing
137 (longlines-unshow-hard-newlines))
138 (let ((buffer-undo-list t))
139 (longlines-encode-region (point-min) (point-max)))
140 (remove-hook 'change-major-mode-hook 'longlines-mode-off t)
141 (remove-hook 'before-kill-functions 'longlines-encode-region t)
142 (remove-hook 'after-change-functions 'longlines-after-change-function t)
143 (remove-hook 'post-command-hook 'longlines-post-command-function t)
144 (remove-hook 'window-configuration-change-hook
145 'longlines-window-change-function t)
146 (kill-local-variable 'fill-column)))
147
148 (defun longlines-mode-off ()
149 "Turn off longlines mode.
150 This function exists to be called by `change-major-mode-hook' when the
151 major mode changes."
152 (longlines-mode 0))
153
154 ;; Showing the effect of hard newlines in the buffer
155
156 (defface longlines-visible-face
157 '((t (:background "red")))
158 "Face used to make hard newlines visible in `longlines-mode'."
159 :group 'longlines)
160
161 (defun longlines-show-hard-newlines (&optional arg)
162 "Make hard newlines visible by adding a face.
163 With optional argument ARG, make the hard newlines invisible again."
164 (interactive "P")
165 (let ((buffer-undo-list t)
166 (mod (buffer-modified-p)))
167 (if arg
168 (longlines-unshow-hard-newlines)
169 (setq longlines-showing t)
170 (longlines-show-region (point-min) (point-max)))
171 (set-buffer-modified-p mod)))
172
173 (defun longlines-show-region (beg end)
174 "Make hard newlines between BEG and END visible."
175 (let* ((pmin (min beg end))
176 (pmax (max beg end))
177 (pos (text-property-any pmin pmax 'hard t)))
178 (while pos
179 (put-text-property pos (1+ pos) 'display
180 (copy-sequence longlines-show-effect))
181 (setq pos (text-property-any (1+ pos) pmax 'hard t)))))
182
183 (defun longlines-unshow-hard-newlines ()
184 "Make hard newlines invisible again."
185 (interactive)
186 (setq longlines-showing nil)
187 (let ((pos (text-property-any (point-min) (point-max) 'hard t)))
188 (while pos
189 (remove-text-properties pos (1+ pos) '(display))
190 (setq pos (text-property-any (1+ pos) (point-max) 'hard t)))))
191
192 ;; Wrapping the paragraphs.
193
194 (defun longlines-wrap-region (beg end)
195 "Wrap each successive line, starting with the line before BEG.
196 Stop when we reach lines after END that don't need wrapping, or the
197 end of the buffer."
198 (setq longlines-wrap-point (point))
199 (goto-char beg)
200 (forward-line -1)
201 ;; Two successful longlines-wrap-line's in a row mean successive
202 ;; lines don't need wrapping.
203 (while (null (and (longlines-wrap-line)
204 (or (eobp)
205 (and (>= (point) end)
206 (longlines-wrap-line))))))
207 (goto-char longlines-wrap-point))
208
209 (defun longlines-wrap-line ()
210 "If the current line needs to be wrapped, wrap it and return nil.
211 If wrapping is performed, point remains on the line. If the line does
212 not need to be wrapped, move point to the next line and return t."
213 (if (longlines-set-breakpoint)
214 (progn (backward-char 1)
215 (delete-char 1)
216 (insert-char ?\n 1)
217 nil)
218 (if (longlines-merge-lines-p)
219 (progn (end-of-line)
220 (delete-char 1)
221 ;; After certain commands (e.g. kill-line), there may be two
222 ;; successive soft newlines in the buffer. In this case, we
223 ;; replace these two newlines by a single space. Unfortunately,
224 ;; this breaks the conservation of (spaces + newlines), so we
225 ;; have to fiddle with longlines-wrap-point.
226 (if (or (bolp) (eolp))
227 (if (> longlines-wrap-point (point))
228 (setq longlines-wrap-point
229 (1- longlines-wrap-point)))
230 (insert-char ? 1))
231 nil)
232 (forward-line 1)
233 t)))
234
235 (defun longlines-set-breakpoint ()
236 "Place point where we should break the current line, and return t.
237 If the line should not be broken, return nil; point remains on the
238 line."
239 (move-to-column fill-column)
240 (if (and (re-search-forward "[^ ]" (line-end-position) 1)
241 (> (current-column) fill-column))
242 ;; This line is too long. Can we break it?
243 (or (longlines-find-break-backward)
244 (progn (move-to-column fill-column)
245 (longlines-find-break-forward)))))
246
247 (defun longlines-find-break-backward ()
248 "Move point backward to the first available breakpoint and return t.
249 If no breakpoint is found, return nil."
250 (and (search-backward " " (line-beginning-position) 1)
251 (save-excursion
252 (skip-chars-backward " " (line-beginning-position))
253 (null (bolp)))
254 (progn (forward-char 1)
255 (if (and fill-nobreak-predicate
256 (run-hook-with-args-until-success
257 'fill-nobreak-predicate))
258 (progn (skip-chars-backward " " (line-beginning-position))
259 (longlines-find-break-backward))
260 t))))
261
262 (defun longlines-find-break-forward ()
263 "Move point forward to the first available breakpoint and return t.
264 If no break point is found, return nil."
265 (and (search-forward " " (line-end-position) 1)
266 (progn (skip-chars-forward " " (line-end-position))
267 (null (eolp)))
268 (if (and fill-nobreak-predicate
269 (run-hook-with-args-until-success
270 'fill-nobreak-predicate))
271 (longlines-find-break-forward)
272 t)))
273
274 (defun longlines-merge-lines-p ()
275 "Return t if part of the next line can fit onto the current line.
276 Otherwise, return nil. Text cannot be moved across hard newlines."
277 (save-excursion
278 (end-of-line)
279 (and (null (eobp))
280 (null (get-text-property (point) 'hard))
281 (let ((space (- fill-column (current-column))))
282 (forward-line 1)
283 (if (eq (char-after) ? )
284 t ; We can always merge some spaces
285 (<= (if (search-forward " " (line-end-position) 1)
286 (current-column)
287 (1+ (current-column)))
288 space))))))
289
290 (defun longlines-decode-region (beg end)
291 "Turn all newlines between BEG and END into hard newlines."
292 (save-excursion
293 (goto-char (min beg end))
294 (while (search-forward "\n" (max beg end) t)
295 (set-hard-newline-properties
296 (match-beginning 0) (match-end 0)))))
297
298 (defun longlines-encode-region (beg end &optional buffer)
299 "Replace each soft newline between BEG and END with exactly one space.
300 Hard newlines are left intact. The optional argument BUFFER exists for
301 compatibility with `format-alist', and is ignored."
302 (save-excursion
303 (let ((mod (buffer-modified-p)))
304 (goto-char (min beg end))
305 (while (search-forward "\n" (max (max beg end)) t)
306 (unless (get-text-property (match-beginning 0) 'hard)
307 (replace-match " ")))
308 (set-buffer-modified-p mod)
309 end)))
310
311 (defun longlines-encode-string (string)
312 "Return a copy of STRING with each soft newline replaced by a space.
313 Hard newlines are left intact."
314 (let* ((str (copy-sequence string))
315 (pos (string-match "\n" str)))
316 (while pos
317 (if (null (get-text-property pos 'hard str))
318 (aset str pos ? ))
319 (setq pos (string-match "\n" str (1+ pos))))
320 str))
321
322 ;; Auto wrap
323
324 (defun longlines-auto-wrap (&optional arg)
325 "Turn on automatic line wrapping, and wrap the entire buffer.
326 With optional argument ARG, turn off line wrapping."
327 (interactive "P")
328 (remove-hook 'after-change-functions 'longlines-after-change-function t)
329 (remove-hook 'post-command-hook 'longlines-post-command-function t)
330 (if arg
331 (progn (setq longlines-auto-wrap nil)
332 (message "Auto wrap disabled."))
333 (setq longlines-auto-wrap t)
334 (add-hook 'after-change-functions
335 'longlines-after-change-function nil t)
336 (add-hook 'post-command-hook
337 'longlines-post-command-function nil t)
338 (let ((mod (buffer-modified-p)))
339 (longlines-wrap-region (point-min) (point-max))
340 (set-buffer-modified-p mod))
341 (message "Auto wrap enabled.")))
342
343 (defun longlines-after-change-function (beg end len)
344 "Update `longlines-wrap-beg' and `longlines-wrap-end'.
345 This is called by `after-change-functions' to keep track of the region
346 that has changed."
347 (unless undo-in-progress
348 (setq longlines-wrap-beg
349 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg))
350 (setq longlines-wrap-end
351 (if longlines-wrap-end (max longlines-wrap-end end) end))))
352
353 (defun longlines-post-command-function ()
354 "Perform line wrapping on the parts of the buffer that have changed.
355 This is called by `post-command-hook' after each command."
356 (when longlines-wrap-beg
357 (cond ((or (eq this-command 'yank)
358 (eq this-command 'yank-pop))
359 (longlines-decode-region (point) (mark t))
360 (if longlines-showing
361 (longlines-show-region (point) (mark t))))
362 ((and (eq this-command 'newline) longlines-showing)
363 (save-excursion
364 (if (search-backward "\n" nil t)
365 (longlines-show-region
366 (match-beginning 0) (match-end 0))))))
367 (unless (or (eq this-command 'fill-paragraph)
368 (eq this-command 'fill-region))
369 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end))
370 (setq longlines-wrap-beg nil)
371 (setq longlines-wrap-end nil)))
372
373 (defun longlines-window-change-function ()
374 "Re-wrap the buffer if the window width has changed.
375 This is called by `window-size-change-functions'."
376 (when (/= fill-column (- (window-width) window-min-width))
377 (setq fill-column (- (window-width) window-min-width))
378 (let ((mod (buffer-modified-p)))
379 (longlines-wrap-region (point-min) (point-max))
380 (set-buffer-modified-p mod))))
381
382 ;; Loading and saving
383
384 (add-to-list
385 'format-alist
386 (list 'longlines "Automatically wrap long lines." nil
387 'longlines-decode-region 'longlines-encode-region t nil))
388
389 (provide 'longlines)
390
391 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624
392 ;;; longlines.el ends here