Various cleanups.
[emacs.git] / lisp / longlines.el
blobb11d4e6fabae92ff41cc29be337ce909fd9be8f6
1 ;;; longlines.el --- automatically wrap long lines
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006 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: Chong Yidong <cyd@stupidchicken.com>
9 ;; Keywords: convenience, wp
11 ;; This file is 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 2, or (at your option)
16 ;; 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; 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.
28 ;;; Commentary:
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.
38 ;; Special thanks to Rod Smith for many useful bug reports.
40 ;;; Code:
42 (defgroup longlines nil
43 "Automatic wrapping of long lines when loading files."
44 :group 'fill)
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)
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)
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)
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)
76 ;; Internal variables
78 (defvar longlines-wrap-beg nil)
79 (defvar longlines-wrap-end nil)
80 (defvar longlines-wrap-point nil)
81 (defvar longlines-showing nil)
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)
88 ;; Mode
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.
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.
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 (add-hook 'before-revert-hook 'longlines-before-revert-hook nil t)
112 (make-local-variable 'buffer-substring-filters)
113 (set (make-local-variable 'isearch-search-fun-function)
114 'longlines-search-function)
115 (add-to-list 'buffer-substring-filters 'longlines-encode-string)
116 (when longlines-wrap-follows-window-size
117 (set (make-local-variable 'fill-column)
118 (- (window-width) window-min-width))
119 (add-hook 'window-configuration-change-hook
120 'longlines-window-change-function nil t))
121 (let ((buffer-undo-list t)
122 (inhibit-read-only t)
123 (after-change-functions nil)
124 (mod (buffer-modified-p)))
125 ;; Turning off undo is OK since (spaces + newlines) is
126 ;; conserved, except for a corner case in
127 ;; longlines-wrap-lines that we'll never encounter from here
128 (save-restriction
129 (widen)
130 (longlines-decode-buffer)
131 (longlines-wrap-region (point-min) (point-max)))
132 (set-buffer-modified-p mod))
133 (when (and longlines-show-hard-newlines
134 (not longlines-showing))
135 (longlines-show-hard-newlines))
137 ;; Hacks to make longlines play nice with various modes.
138 (cond ((eq major-mode 'mail-mode)
139 (add-hook 'mail-setup-hook 'longlines-decode-buffer nil t)
140 (or mail-citation-hook
141 (add-hook 'mail-citation-hook 'mail-indent-citation nil t))
142 (add-hook 'mail-citation-hook 'longlines-decode-region nil t))
143 ((eq major-mode 'message-mode)
144 (add-hook 'message-setup-hook 'longlines-decode-buffer nil t)
145 (make-local-variable 'message-indent-citation-function)
146 (if (not (listp message-indent-citation-function))
147 (setq message-indent-citation-function
148 (list message-indent-citation-function)))
149 (add-to-list 'message-indent-citation-function
150 'longlines-decode-region t)))
152 (when longlines-auto-wrap
153 (auto-fill-mode 0)
154 (add-hook 'after-change-functions
155 'longlines-after-change-function nil t)
156 (add-hook 'post-command-hook
157 'longlines-post-command-function nil t)))
158 ;; Turn off longlines mode
159 (setq buffer-file-format (delete 'longlines buffer-file-format))
160 (if longlines-showing
161 (longlines-unshow-hard-newlines))
162 (let ((buffer-undo-list t)
163 (after-change-functions nil)
164 (inhibit-read-only t))
165 (save-restriction
166 (widen)
167 (longlines-encode-region (point-min) (point-max))))
168 (remove-hook 'change-major-mode-hook 'longlines-mode-off t)
169 (remove-hook 'after-change-functions 'longlines-after-change-function t)
170 (remove-hook 'post-command-hook 'longlines-post-command-function t)
171 (remove-hook 'before-revert-hook 'longlines-before-revert-hook t)
172 (remove-hook 'window-configuration-change-hook
173 'longlines-window-change-function t)
174 (when longlines-wrap-follows-window-size
175 (kill-local-variable 'fill-column))
176 (kill-local-variable 'isearch-search-fun-function)
177 (kill-local-variable 'require-final-newline)
178 (kill-local-variable 'buffer-substring-filters)
179 (kill-local-variable 'use-hard-newlines)))
181 (defun longlines-mode-off ()
182 "Turn off longlines mode.
183 This function exists to be called by `change-major-mode-hook' when the
184 major mode changes."
185 (longlines-mode 0))
187 ;; Showing the effect of hard newlines in the buffer
189 (defun longlines-show-hard-newlines (&optional arg)
190 "Make hard newlines visible by adding a face.
191 With optional argument ARG, make the hard newlines invisible again."
192 (interactive "P")
193 (let ((buffer-undo-list t)
194 (mod (buffer-modified-p)))
195 (if arg
196 (longlines-unshow-hard-newlines)
197 (setq longlines-showing t)
198 (longlines-show-region (point-min) (point-max)))
199 (set-buffer-modified-p mod)))
201 (defun longlines-show-region (beg end)
202 "Make hard newlines between BEG and END visible."
203 (let* ((pmin (min beg end))
204 (pmax (max beg end))
205 (pos (text-property-not-all pmin pmax 'hard nil)))
206 (while pos
207 (put-text-property pos (1+ pos) 'display
208 (copy-sequence longlines-show-effect))
209 (setq pos (text-property-not-all (1+ pos) pmax 'hard nil)))))
211 (defun longlines-unshow-hard-newlines ()
212 "Make hard newlines invisible again."
213 (interactive)
214 (setq longlines-showing nil)
215 (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil)))
216 (while pos
217 (remove-text-properties pos (1+ pos) '(display))
218 (setq pos (text-property-not-all (1+ pos) (point-max) 'hard nil)))))
220 ;; Wrapping the paragraphs.
222 (defun longlines-wrap-region (beg end)
223 "Wrap each successive line, starting with the line before BEG.
224 Stop when we reach lines after END that don't need wrapping, or the
225 end of the buffer."
226 (setq longlines-wrap-point (point))
227 (goto-char beg)
228 (forward-line -1)
229 ;; Two successful longlines-wrap-line's in a row mean successive
230 ;; lines don't need wrapping.
231 (while (null (and (longlines-wrap-line)
232 (or (eobp)
233 (and (>= (point) end)
234 (longlines-wrap-line))))))
235 (goto-char longlines-wrap-point))
237 (defun longlines-wrap-line ()
238 "If the current line needs to be wrapped, wrap it and return nil.
239 If wrapping is performed, point remains on the line. If the line does
240 not need to be wrapped, move point to the next line and return t."
241 (if (longlines-set-breakpoint)
242 (progn (insert-before-markers ?\n)
243 (backward-char 1)
244 (delete-char -1)
245 (forward-char 1)
246 nil)
247 (if (longlines-merge-lines-p)
248 (progn (end-of-line)
249 ;; After certain commands (e.g. kill-line), there may be two
250 ;; successive soft newlines in the buffer. In this case, we
251 ;; replace these two newlines by a single space. Unfortunately,
252 ;; this breaks the conservation of (spaces + newlines), so we
253 ;; have to fiddle with longlines-wrap-point.
254 (if (or (prog1 (bolp) (forward-char 1)) (eolp))
255 (progn
256 (delete-char -1)
257 (if (> longlines-wrap-point (point))
258 (setq longlines-wrap-point
259 (1- longlines-wrap-point))))
260 (insert-before-markers-and-inherit ?\ )
261 (backward-char 1)
262 (delete-char -1)
263 (forward-char 1))
264 nil)
265 (forward-line 1)
266 t)))
268 (defun longlines-set-breakpoint ()
269 "Place point where we should break the current line, and return t.
270 If the line should not be broken, return nil; point remains on the
271 line."
272 (move-to-column fill-column)
273 (if (and (re-search-forward "[^ ]" (line-end-position) 1)
274 (> (current-column) fill-column))
275 ;; This line is too long. Can we break it?
276 (or (longlines-find-break-backward)
277 (progn (move-to-column fill-column)
278 (longlines-find-break-forward)))))
280 (defun longlines-find-break-backward ()
281 "Move point backward to the first available breakpoint and return t.
282 If no breakpoint is found, return nil."
283 (and (search-backward " " (line-beginning-position) 1)
284 (save-excursion
285 (skip-chars-backward " " (line-beginning-position))
286 (null (bolp)))
287 (progn (forward-char 1)
288 (if (and fill-nobreak-predicate
289 (run-hook-with-args-until-success
290 'fill-nobreak-predicate))
291 (progn (skip-chars-backward " " (line-beginning-position))
292 (longlines-find-break-backward))
293 t))))
295 (defun longlines-find-break-forward ()
296 "Move point forward to the first available breakpoint and return t.
297 If no break point is found, return nil."
298 (and (search-forward " " (line-end-position) 1)
299 (progn (skip-chars-forward " " (line-end-position))
300 (null (eolp)))
301 (if (and fill-nobreak-predicate
302 (run-hook-with-args-until-success
303 'fill-nobreak-predicate))
304 (longlines-find-break-forward)
305 t)))
307 (defun longlines-merge-lines-p ()
308 "Return t if part of the next line can fit onto the current line.
309 Otherwise, return nil. Text cannot be moved across hard newlines."
310 (save-excursion
311 (end-of-line)
312 (and (null (eobp))
313 (null (get-text-property (point) 'hard))
314 (let ((space (- fill-column (current-column))))
315 (forward-line 1)
316 (if (eq (char-after) ? )
317 t ; We can always merge some spaces
318 (<= (if (search-forward " " (line-end-position) 1)
319 (current-column)
320 (1+ (current-column)))
321 space))))))
323 (defun longlines-decode-region (&optional beg end)
324 "Turn all newlines between BEG and END into hard newlines.
325 If BEG and END are nil, the point and mark are used."
326 (if (null beg) (setq beg (point)))
327 (if (null end) (setq end (mark t)))
328 (save-excursion
329 (let ((reg-max (max beg end)))
330 (goto-char (min beg end))
331 (while (search-forward "\n" reg-max t)
332 (set-hard-newline-properties
333 (match-beginning 0) (match-end 0))))))
335 (defun longlines-decode-buffer ()
336 "Turn all newlines in the buffer into hard newlines."
337 (longlines-decode-region (point-min) (point-max)))
339 (defun longlines-encode-region (beg end &optional buffer)
340 "Replace each soft newline between BEG and END with exactly one space.
341 Hard newlines are left intact. The optional argument BUFFER exists for
342 compatibility with `format-alist', and is ignored."
343 (save-excursion
344 (let ((reg-max (max beg end))
345 (mod (buffer-modified-p)))
346 (goto-char (min beg end))
347 (while (search-forward "\n" reg-max t)
348 (unless (get-text-property (match-beginning 0) 'hard)
349 (replace-match " ")))
350 (set-buffer-modified-p mod)
351 end)))
353 (defun longlines-encode-string (string)
354 "Return a copy of STRING with each soft newline replaced by a space.
355 Hard newlines are left intact."
356 (let* ((str (copy-sequence string))
357 (pos (string-match "\n" str)))
358 (while pos
359 (if (null (get-text-property pos 'hard str))
360 (aset str pos ? ))
361 (setq pos (string-match "\n" str (1+ pos))))
362 str))
364 ;; Auto wrap
366 (defun longlines-auto-wrap (&optional arg)
367 "Turn on automatic line wrapping, and wrap the entire buffer.
368 With optional argument ARG, turn off line wrapping."
369 (interactive "P")
370 (remove-hook 'after-change-functions 'longlines-after-change-function t)
371 (remove-hook 'post-command-hook 'longlines-post-command-function t)
372 (if arg
373 (progn (setq longlines-auto-wrap nil)
374 (message "Auto wrap disabled."))
375 (setq longlines-auto-wrap t)
376 (add-hook 'after-change-functions
377 'longlines-after-change-function nil t)
378 (add-hook 'post-command-hook
379 'longlines-post-command-function nil t)
380 (let ((mod (buffer-modified-p)))
381 (longlines-wrap-region (point-min) (point-max))
382 (set-buffer-modified-p mod))
383 (message "Auto wrap enabled.")))
385 (defun longlines-after-change-function (beg end len)
386 "Update `longlines-wrap-beg' and `longlines-wrap-end'.
387 This is called by `after-change-functions' to keep track of the region
388 that has changed."
389 (unless undo-in-progress
390 (setq longlines-wrap-beg
391 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg))
392 (setq longlines-wrap-end
393 (if longlines-wrap-end (max longlines-wrap-end end) end))))
395 (defun longlines-post-command-function ()
396 "Perform line wrapping on the parts of the buffer that have changed.
397 This is called by `post-command-hook' after each command."
398 (when longlines-wrap-beg
399 (cond ((or (eq this-command 'yank)
400 (eq this-command 'yank-pop))
401 (longlines-decode-region (point) (mark t))
402 (if longlines-showing
403 (longlines-show-region (point) (mark t))))
404 ((and (eq this-command 'newline) longlines-showing)
405 (save-excursion
406 (if (search-backward "\n" nil t)
407 (longlines-show-region
408 (match-beginning 0) (match-end 0))))))
409 (unless (or (eq this-command 'fill-paragraph)
410 (eq this-command 'fill-region))
411 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end))
412 (setq longlines-wrap-beg nil)
413 (setq longlines-wrap-end nil)))
415 (defun longlines-window-change-function ()
416 "Re-wrap the buffer if the window width has changed.
417 This is called by `window-size-change-functions'."
418 (when (/= fill-column (- (window-width) window-min-width))
419 (setq fill-column (- (window-width) window-min-width))
420 (let ((mod (buffer-modified-p)))
421 (longlines-wrap-region (point-min) (point-max))
422 (set-buffer-modified-p mod))))
424 ;; Isearch
426 (defun longlines-search-function ()
427 (cond
428 (isearch-word
429 (if isearch-forward 'word-search-forward 'word-search-backward))
430 (isearch-regexp
431 (if isearch-forward 're-search-forward 're-search-backward))
433 (if isearch-forward
434 'longlines-search-forward
435 'longlines-search-backward))))
437 (defun longlines-search-forward (string &optional bound noerror count)
438 (let ((search-spaces-regexp "[ \n]+"))
439 (re-search-forward (regexp-quote string) bound noerror count)))
441 (defun longlines-search-backward (string &optional bound noerror count)
442 (let ((search-spaces-regexp "[ \n]+"))
443 (re-search-backward (regexp-quote string) bound noerror count)))
445 ;; Loading and saving
447 (defun longlines-before-revert-hook ()
448 (add-hook 'after-revert-hook 'longlines-after-revert-hook nil t)
449 (longlines-mode 0))
451 (defun longlines-after-revert-hook ()
452 (remove-hook 'after-revert-hook 'longlines-after-revert-hook t)
453 (longlines-mode 1))
455 (add-to-list
456 'format-alist
457 (list 'longlines "Automatically wrap long lines." nil nil
458 'longlines-encode-region t nil))
460 (provide 'longlines)
462 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624
463 ;;; longlines.el ends here