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