*** empty log message ***
[emacs.git] / lisp / longlines.el
bloba3912a26ca747ed09904369d565b0f3079823c99
1 ;;; longlines.el --- automatically wrap long lines
3 ;; Copyright (C) 2000, 2001, 2005 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 'longlinges-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 'before-kill-functions 'longlines-encode-region t)
170 (remove-hook 'after-change-functions 'longlines-after-change-function t)
171 (remove-hook 'post-command-hook 'longlines-post-command-function t)
172 (remove-hook 'before-revert-hook 'longlines-before-revert-hook t)
173 (remove-hook 'window-configuration-change-hook
174 'longlines-window-change-function t)
175 (when longlines-wrap-follows-window-size
176 (kill-local-variable 'fill-column))
177 (kill-local-variable 'isearch-search-fun-function)
178 (kill-local-variable 'require-final-newline)
179 (kill-local-variable 'buffer-substring-filters)
180 (kill-local-variable 'use-hard-newlines)))
182 (defun longlines-mode-off ()
183 "Turn off longlines mode.
184 This function exists to be called by `change-major-mode-hook' when the
185 major mode changes."
186 (longlines-mode 0))
188 ;; Showing the effect of hard newlines in the buffer
190 (defun longlines-show-hard-newlines (&optional arg)
191 "Make hard newlines visible by adding a face.
192 With optional argument ARG, make the hard newlines invisible again."
193 (interactive "P")
194 (let ((buffer-undo-list t)
195 (mod (buffer-modified-p)))
196 (if arg
197 (longlines-unshow-hard-newlines)
198 (setq longlines-showing t)
199 (longlines-show-region (point-min) (point-max)))
200 (set-buffer-modified-p mod)))
202 (defun longlines-show-region (beg end)
203 "Make hard newlines between BEG and END visible."
204 (let* ((pmin (min beg end))
205 (pmax (max beg end))
206 (pos (text-property-not-all pmin pmax 'hard nil)))
207 (while pos
208 (put-text-property pos (1+ pos) 'display
209 (copy-sequence longlines-show-effect))
210 (setq pos (text-property-not-all (1+ pos) pmax 'hard nil)))))
212 (defun longlines-unshow-hard-newlines ()
213 "Make hard newlines invisible again."
214 (interactive)
215 (setq longlines-showing nil)
216 (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil)))
217 (while pos
218 (remove-text-properties pos (1+ pos) '(display))
219 (setq pos (text-property-not-all (1+ pos) (point-max) 'hard nil)))))
221 ;; Wrapping the paragraphs.
223 (defun longlines-wrap-region (beg end)
224 "Wrap each successive line, starting with the line before BEG.
225 Stop when we reach lines after END that don't need wrapping, or the
226 end of the buffer."
227 (setq longlines-wrap-point (point))
228 (goto-char beg)
229 (forward-line -1)
230 ;; Two successful longlines-wrap-line's in a row mean successive
231 ;; lines don't need wrapping.
232 (while (null (and (longlines-wrap-line)
233 (or (eobp)
234 (and (>= (point) end)
235 (longlines-wrap-line))))))
236 (goto-char longlines-wrap-point))
238 (defun longlines-wrap-line ()
239 "If the current line needs to be wrapped, wrap it and return nil.
240 If wrapping is performed, point remains on the line. If the line does
241 not need to be wrapped, move point to the next line and return t."
242 (if (longlines-set-breakpoint)
243 (progn (insert-before-markers ?\n)
244 (backward-char 1)
245 (delete-char -1)
246 (forward-char 1)
247 nil)
248 (if (longlines-merge-lines-p)
249 (progn (end-of-line)
250 ;; After certain commands (e.g. kill-line), there may be two
251 ;; successive soft newlines in the buffer. In this case, we
252 ;; replace these two newlines by a single space. Unfortunately,
253 ;; this breaks the conservation of (spaces + newlines), so we
254 ;; have to fiddle with longlines-wrap-point.
255 (if (or (prog1 (bolp) (forward-char 1)) (eolp))
256 (progn
257 (delete-char -1)
258 (if (> longlines-wrap-point (point))
259 (setq longlines-wrap-point
260 (1- longlines-wrap-point))))
261 (insert-before-markers-and-inherit ?\ )
262 (backward-char 1)
263 (delete-char -1)
264 (forward-char 1))
265 nil)
266 (forward-line 1)
267 t)))
269 (defun longlines-set-breakpoint ()
270 "Place point where we should break the current line, and return t.
271 If the line should not be broken, return nil; point remains on the
272 line."
273 (move-to-column fill-column)
274 (if (and (re-search-forward "[^ ]" (line-end-position) 1)
275 (> (current-column) fill-column))
276 ;; This line is too long. Can we break it?
277 (or (longlines-find-break-backward)
278 (progn (move-to-column fill-column)
279 (longlines-find-break-forward)))))
281 (defun longlines-find-break-backward ()
282 "Move point backward to the first available breakpoint and return t.
283 If no breakpoint is found, return nil."
284 (and (search-backward " " (line-beginning-position) 1)
285 (save-excursion
286 (skip-chars-backward " " (line-beginning-position))
287 (null (bolp)))
288 (progn (forward-char 1)
289 (if (and fill-nobreak-predicate
290 (run-hook-with-args-until-success
291 'fill-nobreak-predicate))
292 (progn (skip-chars-backward " " (line-beginning-position))
293 (longlines-find-break-backward))
294 t))))
296 (defun longlines-find-break-forward ()
297 "Move point forward to the first available breakpoint and return t.
298 If no break point is found, return nil."
299 (and (search-forward " " (line-end-position) 1)
300 (progn (skip-chars-forward " " (line-end-position))
301 (null (eolp)))
302 (if (and fill-nobreak-predicate
303 (run-hook-with-args-until-success
304 'fill-nobreak-predicate))
305 (longlines-find-break-forward)
306 t)))
308 (defun longlines-merge-lines-p ()
309 "Return t if part of the next line can fit onto the current line.
310 Otherwise, return nil. Text cannot be moved across hard newlines."
311 (save-excursion
312 (end-of-line)
313 (and (null (eobp))
314 (null (get-text-property (point) 'hard))
315 (let ((space (- fill-column (current-column))))
316 (forward-line 1)
317 (if (eq (char-after) ? )
318 t ; We can always merge some spaces
319 (<= (if (search-forward " " (line-end-position) 1)
320 (current-column)
321 (1+ (current-column)))
322 space))))))
324 (defun longlines-decode-region (&optional beg end)
325 "Turn all newlines between BEG and END into hard newlines.
326 If BEG and END are nil, the point and mark are used."
327 (if (null beg) (setq beg (point)))
328 (if (null end) (setq end (mark t)))
329 (save-excursion
330 (goto-char (min beg end))
331 (while (search-forward "\n" (max beg end) 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 ((mod (buffer-modified-p)))
345 (goto-char (min beg end))
346 (while (search-forward "\n" (max (max beg end)) t)
347 (unless (get-text-property (match-beginning 0) 'hard)
348 (replace-match " ")))
349 (set-buffer-modified-p mod)
350 end)))
352 (defun longlines-encode-string (string)
353 "Return a copy of STRING with each soft newline replaced by a space.
354 Hard newlines are left intact."
355 (let* ((str (copy-sequence string))
356 (pos (string-match "\n" str)))
357 (while pos
358 (if (null (get-text-property pos 'hard str))
359 (aset str pos ? ))
360 (setq pos (string-match "\n" str (1+ pos))))
361 str))
363 ;; Auto wrap
365 (defun longlines-auto-wrap (&optional arg)
366 "Turn on automatic line wrapping, and wrap the entire buffer.
367 With optional argument ARG, turn off line wrapping."
368 (interactive "P")
369 (remove-hook 'after-change-functions 'longlines-after-change-function t)
370 (remove-hook 'post-command-hook 'longlines-post-command-function t)
371 (if arg
372 (progn (setq longlines-auto-wrap nil)
373 (message "Auto wrap disabled."))
374 (setq longlines-auto-wrap t)
375 (add-hook 'after-change-functions
376 'longlines-after-change-function nil t)
377 (add-hook 'post-command-hook
378 'longlines-post-command-function nil t)
379 (let ((mod (buffer-modified-p)))
380 (longlines-wrap-region (point-min) (point-max))
381 (set-buffer-modified-p mod))
382 (message "Auto wrap enabled.")))
384 (defun longlines-after-change-function (beg end len)
385 "Update `longlines-wrap-beg' and `longlines-wrap-end'.
386 This is called by `after-change-functions' to keep track of the region
387 that has changed."
388 (unless undo-in-progress
389 (setq longlines-wrap-beg
390 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg))
391 (setq longlines-wrap-end
392 (if longlines-wrap-end (max longlines-wrap-end end) end))))
394 (defun longlines-post-command-function ()
395 "Perform line wrapping on the parts of the buffer that have changed.
396 This is called by `post-command-hook' after each command."
397 (when longlines-wrap-beg
398 (cond ((or (eq this-command 'yank)
399 (eq this-command 'yank-pop))
400 (longlines-decode-region (point) (mark t))
401 (if longlines-showing
402 (longlines-show-region (point) (mark t))))
403 ((and (eq this-command 'newline) longlines-showing)
404 (save-excursion
405 (if (search-backward "\n" nil t)
406 (longlines-show-region
407 (match-beginning 0) (match-end 0))))))
408 (unless (or (eq this-command 'fill-paragraph)
409 (eq this-command 'fill-region))
410 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end))
411 (setq longlines-wrap-beg nil)
412 (setq longlines-wrap-end nil)))
414 (defun longlines-window-change-function ()
415 "Re-wrap the buffer if the window width has changed.
416 This is called by `window-size-change-functions'."
417 (when (/= fill-column (- (window-width) window-min-width))
418 (setq fill-column (- (window-width) window-min-width))
419 (let ((mod (buffer-modified-p)))
420 (longlines-wrap-region (point-min) (point-max))
421 (set-buffer-modified-p mod))))
423 ;; Isearch
425 (defun longlinges-search-function ()
426 (cond
427 (isearch-word
428 (if isearch-forward 'word-search-forward 'word-search-backward))
429 (isearch-regexp
430 (if isearch-forward 're-search-forward 're-search-backward))
432 (if isearch-forward
433 'longlines-search-forward
434 'longlines-search-backward))))
436 (defun longlines-search-forward (string &optional bound noerror count)
437 (let ((search-spaces-regexp "[ \n]+"))
438 (re-search-forward (regexp-quote string) bound noerror count)))
440 (defun longlines-search-backward (string &optional bound noerror count)
441 (let ((search-spaces-regexp "[ \n]+"))
442 (re-search-backward (regexp-quote string) bound noerror count)))
444 ;; Loading and saving
446 (defun longlines-before-revert-hook ()
447 (add-hook 'after-revert-hook 'longlines-after-revert-hook nil t)
448 (longlines-mode 0))
450 (defun longlines-after-revert-hook ()
451 (remove-hook 'after-revert-hook 'longlines-after-revert-hook t)
452 (longlines-mode 1))
454 (add-to-list
455 'format-alist
456 (list 'longlines "Automatically wrap long lines." nil nil
457 'longlines-encode-region t nil))
459 (provide 'longlines)
461 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624
462 ;;; longlines.el ends here