(Progress, Defining Faces):
[emacs.git] / lisp / longlines.el
blob00a2782d0a38ab96391b6a1221296d8a452cbd5e
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
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 (make-local-variable 'buffer-substring-filters)
112 (set (make-local-variable 'isearch-search-fun-function)
113 'longlinges-search-function)
114 (add-to-list 'buffer-substring-filters 'longlines-encode-string)
115 (when longlines-wrap-follows-window-size
116 (set (make-local-variable 'fill-column)
117 (- (window-width) window-min-width))
118 (add-hook 'window-configuration-change-hook
119 'longlines-window-change-function nil t))
120 (let ((buffer-undo-list t)
121 (inhibit-read-only t)
122 (mod (buffer-modified-p)))
123 ;; Turning off undo is OK since (spaces + newlines) is
124 ;; conserved, except for a corner case in
125 ;; longlines-wrap-lines that we'll never encounter from here
126 (longlines-decode-region (point-min) (point-max))
127 (longlines-wrap-region (point-min) (point-max))
128 (set-buffer-modified-p mod))
129 (when (and longlines-show-hard-newlines
130 (not longlines-showing))
131 (longlines-show-hard-newlines))
132 (when longlines-auto-wrap
133 (auto-fill-mode 0)
134 (add-hook 'after-change-functions
135 'longlines-after-change-function nil t)
136 (add-hook 'post-command-hook
137 'longlines-post-command-function nil t)))
138 ;; Turn off longlines mode
139 (setq buffer-file-format (delete 'longlines buffer-file-format))
140 (if longlines-showing
141 (longlines-unshow-hard-newlines))
142 (let ((buffer-undo-list t)
143 (inhibit-read-only t))
144 (longlines-encode-region (point-min) (point-max)))
145 (remove-hook 'change-major-mode-hook 'longlines-mode-off t)
146 (remove-hook 'before-kill-functions 'longlines-encode-region t)
147 (remove-hook 'after-change-functions 'longlines-after-change-function t)
148 (remove-hook 'post-command-hook 'longlines-post-command-function t)
149 (remove-hook 'window-configuration-change-hook
150 'longlines-window-change-function t)
151 (when longlines-wrap-follows-window-size
152 (kill-local-variable 'fill-column))
153 (kill-local-variable 'isearch-search-fun-function)
154 (kill-local-variable 'require-final-newline)
155 (kill-local-variable 'buffer-substring-filters)
156 (kill-local-variable 'use-hard-newlines)))
158 (defun longlines-mode-off ()
159 "Turn off longlines mode.
160 This function exists to be called by `change-major-mode-hook' when the
161 major mode changes."
162 (longlines-mode 0))
164 ;; Showing the effect of hard newlines in the buffer
166 (defun longlines-show-hard-newlines (&optional arg)
167 "Make hard newlines visible by adding a face.
168 With optional argument ARG, make the hard newlines invisible again."
169 (interactive "P")
170 (let ((buffer-undo-list t)
171 (mod (buffer-modified-p)))
172 (if arg
173 (longlines-unshow-hard-newlines)
174 (setq longlines-showing t)
175 (longlines-show-region (point-min) (point-max)))
176 (set-buffer-modified-p mod)))
178 (defun longlines-show-region (beg end)
179 "Make hard newlines between BEG and END visible."
180 (let* ((pmin (min beg end))
181 (pmax (max beg end))
182 (pos (text-property-not-all pmin pmax 'hard nil)))
183 (while pos
184 (put-text-property pos (1+ pos) 'display
185 (copy-sequence longlines-show-effect))
186 (setq pos (text-property-not-all (1+ pos) pmax 'hard nil)))))
188 (defun longlines-unshow-hard-newlines ()
189 "Make hard newlines invisible again."
190 (interactive)
191 (setq longlines-showing nil)
192 (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil)))
193 (while pos
194 (remove-text-properties pos (1+ pos) '(display))
195 (setq pos (text-property-not-all (1+ pos) (point-max) 'hard nil)))))
197 ;; Wrapping the paragraphs.
199 (defun longlines-wrap-region (beg end)
200 "Wrap each successive line, starting with the line before BEG.
201 Stop when we reach lines after END that don't need wrapping, or the
202 end of the buffer."
203 (setq longlines-wrap-point (point))
204 (goto-char beg)
205 (forward-line -1)
206 ;; Two successful longlines-wrap-line's in a row mean successive
207 ;; lines don't need wrapping.
208 (while (null (and (longlines-wrap-line)
209 (or (eobp)
210 (and (>= (point) end)
211 (longlines-wrap-line))))))
212 (goto-char longlines-wrap-point))
214 (defun longlines-wrap-line ()
215 "If the current line needs to be wrapped, wrap it and return nil.
216 If wrapping is performed, point remains on the line. If the line does
217 not need to be wrapped, move point to the next line and return t."
218 (if (longlines-set-breakpoint)
219 (progn (backward-char 1)
220 (delete-char 1)
221 (insert-char ?\n 1)
222 nil)
223 (if (longlines-merge-lines-p)
224 (progn (end-of-line)
225 (delete-char 1)
226 ;; After certain commands (e.g. kill-line), there may be two
227 ;; successive soft newlines in the buffer. In this case, we
228 ;; replace these two newlines by a single space. Unfortunately,
229 ;; this breaks the conservation of (spaces + newlines), so we
230 ;; have to fiddle with longlines-wrap-point.
231 (if (or (bolp) (eolp))
232 (if (> longlines-wrap-point (point))
233 (setq longlines-wrap-point
234 (1- longlines-wrap-point)))
235 (insert-char ? 1))
236 nil)
237 (forward-line 1)
238 t)))
240 (defun longlines-set-breakpoint ()
241 "Place point where we should break the current line, and return t.
242 If the line should not be broken, return nil; point remains on the
243 line."
244 (move-to-column fill-column)
245 (if (and (re-search-forward "[^ ]" (line-end-position) 1)
246 (> (current-column) fill-column))
247 ;; This line is too long. Can we break it?
248 (or (longlines-find-break-backward)
249 (progn (move-to-column fill-column)
250 (longlines-find-break-forward)))))
252 (defun longlines-find-break-backward ()
253 "Move point backward to the first available breakpoint and return t.
254 If no breakpoint is found, return nil."
255 (and (search-backward " " (line-beginning-position) 1)
256 (save-excursion
257 (skip-chars-backward " " (line-beginning-position))
258 (null (bolp)))
259 (progn (forward-char 1)
260 (if (and fill-nobreak-predicate
261 (run-hook-with-args-until-success
262 'fill-nobreak-predicate))
263 (progn (skip-chars-backward " " (line-beginning-position))
264 (longlines-find-break-backward))
265 t))))
267 (defun longlines-find-break-forward ()
268 "Move point forward to the first available breakpoint and return t.
269 If no break point is found, return nil."
270 (and (search-forward " " (line-end-position) 1)
271 (progn (skip-chars-forward " " (line-end-position))
272 (null (eolp)))
273 (if (and fill-nobreak-predicate
274 (run-hook-with-args-until-success
275 'fill-nobreak-predicate))
276 (longlines-find-break-forward)
277 t)))
279 (defun longlines-merge-lines-p ()
280 "Return t if part of the next line can fit onto the current line.
281 Otherwise, return nil. Text cannot be moved across hard newlines."
282 (save-excursion
283 (end-of-line)
284 (and (null (eobp))
285 (null (get-text-property (point) 'hard))
286 (let ((space (- fill-column (current-column))))
287 (forward-line 1)
288 (if (eq (char-after) ? )
289 t ; We can always merge some spaces
290 (<= (if (search-forward " " (line-end-position) 1)
291 (current-column)
292 (1+ (current-column)))
293 space))))))
295 (defun longlines-decode-region (beg end)
296 "Turn all newlines between BEG and END into hard newlines."
297 (save-excursion
298 (goto-char (min beg end))
299 (while (search-forward "\n" (max beg end) t)
300 (set-hard-newline-properties
301 (match-beginning 0) (match-end 0)))))
303 (defun longlines-encode-region (beg end &optional buffer)
304 "Replace each soft newline between BEG and END with exactly one space.
305 Hard newlines are left intact. The optional argument BUFFER exists for
306 compatibility with `format-alist', and is ignored."
307 (save-excursion
308 (let ((mod (buffer-modified-p)))
309 (goto-char (min beg end))
310 (while (search-forward "\n" (max (max beg end)) t)
311 (unless (get-text-property (match-beginning 0) 'hard)
312 (replace-match " ")))
313 (set-buffer-modified-p mod)
314 end)))
316 (defun longlines-encode-string (string)
317 "Return a copy of STRING with each soft newline replaced by a space.
318 Hard newlines are left intact."
319 (let* ((str (copy-sequence string))
320 (pos (string-match "\n" str)))
321 (while pos
322 (if (null (get-text-property pos 'hard str))
323 (aset str pos ? ))
324 (setq pos (string-match "\n" str (1+ pos))))
325 str))
327 ;; Auto wrap
329 (defun longlines-auto-wrap (&optional arg)
330 "Turn on automatic line wrapping, and wrap the entire buffer.
331 With optional argument ARG, turn off line wrapping."
332 (interactive "P")
333 (remove-hook 'after-change-functions 'longlines-after-change-function t)
334 (remove-hook 'post-command-hook 'longlines-post-command-function t)
335 (if arg
336 (progn (setq longlines-auto-wrap nil)
337 (message "Auto wrap disabled."))
338 (setq longlines-auto-wrap t)
339 (add-hook 'after-change-functions
340 'longlines-after-change-function nil t)
341 (add-hook 'post-command-hook
342 'longlines-post-command-function nil t)
343 (let ((mod (buffer-modified-p)))
344 (longlines-wrap-region (point-min) (point-max))
345 (set-buffer-modified-p mod))
346 (message "Auto wrap enabled.")))
348 (defun longlines-after-change-function (beg end len)
349 "Update `longlines-wrap-beg' and `longlines-wrap-end'.
350 This is called by `after-change-functions' to keep track of the region
351 that has changed."
352 (unless undo-in-progress
353 (setq longlines-wrap-beg
354 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg))
355 (setq longlines-wrap-end
356 (if longlines-wrap-end (max longlines-wrap-end end) end))))
358 (defun longlines-post-command-function ()
359 "Perform line wrapping on the parts of the buffer that have changed.
360 This is called by `post-command-hook' after each command."
361 (when longlines-wrap-beg
362 (cond ((or (eq this-command 'yank)
363 (eq this-command 'yank-pop))
364 (longlines-decode-region (point) (mark t))
365 (if longlines-showing
366 (longlines-show-region (point) (mark t))))
367 ((and (eq this-command 'newline) longlines-showing)
368 (save-excursion
369 (if (search-backward "\n" nil t)
370 (longlines-show-region
371 (match-beginning 0) (match-end 0))))))
372 (unless (or (eq this-command 'fill-paragraph)
373 (eq this-command 'fill-region))
374 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end))
375 (setq longlines-wrap-beg nil)
376 (setq longlines-wrap-end nil)))
378 (defun longlines-window-change-function ()
379 "Re-wrap the buffer if the window width has changed.
380 This is called by `window-size-change-functions'."
381 (when (/= fill-column (- (window-width) window-min-width))
382 (setq fill-column (- (window-width) window-min-width))
383 (let ((mod (buffer-modified-p)))
384 (longlines-wrap-region (point-min) (point-max))
385 (set-buffer-modified-p mod))))
387 ;; Isearch
389 (defun longlinges-search-function ()
390 (cond
391 (isearch-word
392 (if isearch-forward 'word-search-forward 'word-search-backward))
393 (isearch-regexp
394 (if isearch-forward 're-search-forward 're-search-backward))
396 (if isearch-forward
397 'longlines-search-forward
398 'longlines-search-backward))))
400 (defun longlines-search-forward (string &optional bound noerror count)
401 (let ((search-spaces-regexp "[ \n]+"))
402 (re-search-forward (regexp-quote string) bound noerror count)))
404 (defun longlines-search-backward (string &optional bound noerror count)
405 (let ((search-spaces-regexp "[ \n]+"))
406 (re-search-backward (regexp-quote string) bound noerror count)))
408 ;; Loading and saving
410 (add-to-list
411 'format-alist
412 (list 'longlines "Automatically wrap long lines." nil
413 'longlines-decode-region 'longlines-encode-region t nil))
415 (provide 'longlines)
417 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624
418 ;;; longlines.el ends here