1 ;;; longlines.el --- automatically wrap long lines
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007 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 3, or (at your option)
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.
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.
42 (defgroup longlines nil
43 "Automatic wrapping of long lines when loading files."
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."
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.
60 If the value is an integer, that specifies the distance from the
61 right edge of the window at which wrapping occurs. For any other
62 non-nil value, wrapping occurs 2 characters from the right edge."
66 (defcustom longlines-show-hard-newlines nil
67 "Non-nil means each hard newline is marked on the screen.
68 \(The variable `longlines-show-effect' controls what they look like.)
69 You can also enable the display temporarily, using the command
70 `longlines-show-hard-newlines'."
74 (defcustom longlines-show-effect
(propertize "|\n" 'face
'escape-glyph
)
75 "A string to display when showing hard newlines.
76 This is used when `longlines-show-hard-newlines' is on."
82 (defvar longlines-wrap-beg nil
)
83 (defvar longlines-wrap-end nil
)
84 (defvar longlines-wrap-point nil
)
85 (defvar longlines-showing nil
)
86 (defvar longlines-decoded nil
)
88 (make-variable-buffer-local 'longlines-wrap-beg
)
89 (make-variable-buffer-local 'longlines-wrap-end
)
90 (make-variable-buffer-local 'longlines-wrap-point
)
91 (make-variable-buffer-local 'longlines-showing
)
92 (make-variable-buffer-local 'longlines-decoded
)
96 (defvar message-indent-citation-function
)
99 (define-minor-mode longlines-mode
100 "Toggle Long Lines mode.
101 In Long Lines mode, long lines are wrapped if they extend beyond
102 `fill-column'. The soft newlines used for line wrapping will not
103 show up when the text is yanked or saved to disk.
105 If the variable `longlines-auto-wrap' is non-nil, lines are automatically
106 wrapped whenever the buffer is changed. You can always call
107 `fill-paragraph' to fill individual paragraphs.
109 If the variable `longlines-show-hard-newlines' is non-nil, hard newlines
110 are indicated with a symbol."
111 :group
'longlines
:lighter
" ll"
113 ;; Turn on longlines mode
115 (use-hard-newlines 1 'never
)
116 (set (make-local-variable 'require-final-newline
) nil
)
117 (add-to-list 'buffer-file-format
'longlines
)
118 (add-hook 'change-major-mode-hook
'longlines-mode-off nil t
)
119 (add-hook 'before-revert-hook
'longlines-before-revert-hook nil t
)
120 (make-local-variable 'buffer-substring-filters
)
121 (make-local-variable 'longlines-auto-wrap
)
122 (set (make-local-variable 'isearch-search-fun-function
)
123 'longlines-search-function
)
124 (add-to-list 'buffer-substring-filters
'longlines-encode-string
)
125 (when longlines-wrap-follows-window-size
126 (let ((dw (if (and (integerp longlines-wrap-follows-window-size
)
127 (>= longlines-wrap-follows-window-size
0)
128 (< longlines-wrap-follows-window-size
130 longlines-wrap-follows-window-size
132 (set (make-local-variable 'fill-column
)
133 (- (window-width) dw
)))
134 (add-hook 'window-configuration-change-hook
135 'longlines-window-change-function nil t
))
136 (let ((buffer-undo-list t
)
137 (inhibit-read-only t
)
138 (after-change-functions nil
)
139 (mod (buffer-modified-p)))
140 ;; Turning off undo is OK since (spaces + newlines) is
141 ;; conserved, except for a corner case in
142 ;; longlines-wrap-lines that we'll never encounter from here
145 (unless longlines-decoded
146 (longlines-decode-buffer)
147 (setq longlines-decoded t
))
148 (longlines-wrap-region (point-min) (point-max)))
149 (set-buffer-modified-p mod
))
150 (when (and longlines-show-hard-newlines
151 (not longlines-showing
))
152 (longlines-show-hard-newlines))
154 ;; Hacks to make longlines play nice with various modes.
155 (cond ((eq major-mode
'mail-mode
)
156 (add-hook 'mail-setup-hook
'longlines-decode-buffer nil t
)
157 (or mail-citation-hook
158 (add-hook 'mail-citation-hook
'mail-indent-citation nil t
))
159 (add-hook 'mail-citation-hook
'longlines-decode-region nil t
))
160 ((eq major-mode
'message-mode
)
161 (add-hook 'message-setup-hook
'longlines-decode-buffer nil t
)
162 (make-local-variable 'message-indent-citation-function
)
163 (if (not (listp message-indent-citation-function
))
164 (setq message-indent-citation-function
165 (list message-indent-citation-function
)))
166 (add-to-list 'message-indent-citation-function
167 'longlines-decode-region t
)))
169 (add-hook 'after-change-functions
'longlines-after-change-function nil t
)
170 (add-hook 'post-command-hook
'longlines-post-command-function nil t
)
171 (when longlines-auto-wrap
173 ;; Turn off longlines mode
174 (setq buffer-file-format
(delete 'longlines buffer-file-format
))
175 (if longlines-showing
176 (longlines-unshow-hard-newlines))
177 (let ((buffer-undo-list t
)
178 (after-change-functions nil
)
179 (inhibit-read-only t
))
180 (if longlines-decoded
183 (longlines-encode-region (point-min) (point-max))
184 (setq longlines-decoded nil
))))
185 (remove-hook 'change-major-mode-hook
'longlines-mode-off t
)
186 (remove-hook 'after-change-functions
'longlines-after-change-function t
)
187 (remove-hook 'post-command-hook
'longlines-post-command-function t
)
188 (remove-hook 'before-revert-hook
'longlines-before-revert-hook t
)
189 (remove-hook 'window-configuration-change-hook
190 'longlines-window-change-function t
)
191 (when longlines-wrap-follows-window-size
192 (kill-local-variable 'fill-column
))
193 (kill-local-variable 'isearch-search-fun-function
)
194 (kill-local-variable 'require-final-newline
)
195 (kill-local-variable 'buffer-substring-filters
)
196 (kill-local-variable 'use-hard-newlines
)))
198 (defun longlines-mode-off ()
199 "Turn off longlines mode.
200 This function exists to be called by `change-major-mode-hook' when the
204 ;; Showing the effect of hard newlines in the buffer
206 (defun longlines-show-hard-newlines (&optional arg
)
207 "Make hard newlines visible by adding a face.
208 With optional argument ARG, make the hard newlines invisible again."
211 (longlines-unshow-hard-newlines)
212 (setq longlines-showing t
)
213 (longlines-show-region (point-min) (point-max))))
215 (defun longlines-show-region (beg end
)
216 "Make hard newlines between BEG and END visible."
217 (let* ((pmin (min beg end
))
219 (pos (text-property-not-all pmin pmax
'hard nil
))
220 (mod (buffer-modified-p))
222 (inhibit-read-only t
)
223 (inhibit-modification-hooks t
))
225 (put-text-property pos
(1+ pos
) 'display
226 (copy-sequence longlines-show-effect
))
227 (setq pos
(text-property-not-all (1+ pos
) pmax
'hard nil
)))
228 (restore-buffer-modified-p mod
)))
230 (defun longlines-unshow-hard-newlines ()
231 "Make hard newlines invisible again."
233 (setq longlines-showing nil
)
234 (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil
))
235 (mod (buffer-modified-p))
237 (inhibit-read-only t
)
238 (inhibit-modification-hooks t
))
240 (remove-text-properties pos
(1+ pos
) '(display))
241 (setq pos
(text-property-not-all (1+ pos
) (point-max) 'hard nil
)))
242 (restore-buffer-modified-p mod
)))
244 ;; Wrapping the paragraphs.
246 (defun longlines-wrap-region (beg end
)
247 "Wrap each successive line, starting with the line before BEG.
248 Stop when we reach lines after END that don't need wrapping, or the
250 (let ((mod (buffer-modified-p)))
251 (setq longlines-wrap-point
(point))
254 ;; Two successful longlines-wrap-line's in a row mean successive
255 ;; lines don't need wrapping.
256 (while (null (and (longlines-wrap-line)
258 (and (>= (point) end
)
259 (longlines-wrap-line))))))
260 (goto-char longlines-wrap-point
)
261 (set-buffer-modified-p mod
)))
263 (defun longlines-wrap-line ()
264 "If the current line needs to be wrapped, wrap it and return nil.
265 If wrapping is performed, point remains on the line. If the line does
266 not need to be wrapped, move point to the next line and return t."
267 (if (longlines-set-breakpoint)
268 (progn (insert-before-markers ?
\n)
273 (if (longlines-merge-lines-p)
275 ;; After certain commands (e.g. kill-line), there may be two
276 ;; successive soft newlines in the buffer. In this case, we
277 ;; replace these two newlines by a single space. Unfortunately,
278 ;; this breaks the conservation of (spaces + newlines), so we
279 ;; have to fiddle with longlines-wrap-point.
280 (if (or (prog1 (bolp) (forward-char 1)) (eolp))
283 (if (> longlines-wrap-point
(point))
284 (setq longlines-wrap-point
285 (1- longlines-wrap-point
))))
286 (insert-before-markers-and-inherit ?\s
)
294 (defun longlines-set-breakpoint ()
295 "Place point where we should break the current line, and return t.
296 If the line should not be broken, return nil; point remains on the
298 (move-to-column fill-column
)
299 (if (and (re-search-forward "[^ ]" (line-end-position) 1)
300 (> (current-column) fill-column
))
301 ;; This line is too long. Can we break it?
302 (or (longlines-find-break-backward)
303 (progn (move-to-column fill-column
)
304 (longlines-find-break-forward)))))
306 (defun longlines-find-break-backward ()
307 "Move point backward to the first available breakpoint and return t.
308 If no breakpoint is found, return nil."
309 (and (search-backward " " (line-beginning-position) 1)
311 (skip-chars-backward " " (line-beginning-position))
313 (progn (forward-char 1)
314 (if (and fill-nobreak-predicate
315 (run-hook-with-args-until-success
316 'fill-nobreak-predicate
))
317 (progn (skip-chars-backward " " (line-beginning-position))
318 (longlines-find-break-backward))
321 (defun longlines-find-break-forward ()
322 "Move point forward to the first available breakpoint and return t.
323 If no break point is found, return nil."
324 (and (search-forward " " (line-end-position) 1)
325 (progn (skip-chars-forward " " (line-end-position))
327 (if (and fill-nobreak-predicate
328 (run-hook-with-args-until-success
329 'fill-nobreak-predicate
))
330 (longlines-find-break-forward)
333 (defun longlines-merge-lines-p ()
334 "Return t if part of the next line can fit onto the current line.
335 Otherwise, return nil. Text cannot be moved across hard newlines."
339 (null (get-text-property (point) 'hard
))
340 (let ((space (- fill-column
(current-column))))
342 (if (eq (char-after) ?
)
343 t
; We can always merge some spaces
344 (<= (if (search-forward " " (line-end-position) 1)
346 (1+ (current-column)))
349 (defun longlines-decode-region (&optional beg end
)
350 "Turn all newlines between BEG and END into hard newlines.
351 If BEG and END are nil, the point and mark are used."
352 (if (null beg
) (setq beg
(point)))
353 (if (null end
) (setq end
(mark t
)))
355 (let ((reg-max (max beg end
)))
356 (goto-char (min beg end
))
357 (while (search-forward "\n" reg-max t
)
358 (set-hard-newline-properties
359 (match-beginning 0) (match-end 0))))))
361 (defun longlines-decode-buffer ()
362 "Turn all newlines in the buffer into hard newlines."
363 (longlines-decode-region (point-min) (point-max)))
365 (defun longlines-encode-region (beg end
&optional buffer
)
366 "Replace each soft newline between BEG and END with exactly one space.
367 Hard newlines are left intact. The optional argument BUFFER exists for
368 compatibility with `format-alist', and is ignored."
370 (let ((reg-max (max beg end
))
371 (mod (buffer-modified-p)))
372 (goto-char (min beg end
))
373 (while (search-forward "\n" reg-max t
)
374 (unless (get-text-property (match-beginning 0) 'hard
)
375 (replace-match " ")))
376 (set-buffer-modified-p mod
)
379 (defun longlines-encode-string (string)
380 "Return a copy of STRING with each soft newline replaced by a space.
381 Hard newlines are left intact."
382 (let* ((str (copy-sequence string
))
383 (pos (string-match "\n" str
)))
385 (if (null (get-text-property pos
'hard str
))
387 (setq pos
(string-match "\n" str
(1+ pos
))))
392 (defun longlines-auto-wrap (&optional arg
)
393 "Toggle automatic line wrapping.
394 With optional argument ARG, turn on line wrapping if and only if ARG is positive.
395 If automatic line wrapping is turned on, wrap the entire buffer."
398 (> (prefix-numeric-value arg
) 0)
399 (not longlines-auto-wrap
)))
402 (setq longlines-auto-wrap t
)
403 (longlines-wrap-region (point-min) (point-max))
404 (message "Auto wrap enabled."))
405 (setq longlines-auto-wrap nil
)
406 (message "Auto wrap disabled.")))
408 (defun longlines-after-change-function (beg end len
)
409 "Update `longlines-wrap-beg' and `longlines-wrap-end'.
410 This is called by `after-change-functions' to keep track of the region
412 (when (and longlines-auto-wrap
(not undo-in-progress
))
413 (setq longlines-wrap-beg
414 (if longlines-wrap-beg
(min longlines-wrap-beg beg
) beg
))
415 (setq longlines-wrap-end
416 (if longlines-wrap-end
(max longlines-wrap-end end
) end
))))
418 (defun longlines-post-command-function ()
419 "Perform line wrapping on the parts of the buffer that have changed.
420 This is called by `post-command-hook' after each command."
421 (when (and longlines-auto-wrap longlines-wrap-beg
)
422 (if (or (eq this-command
'yank
)
423 (eq this-command
'yank-pop
))
424 (longlines-decode-region (point) (mark t
)))
425 (if longlines-showing
426 (longlines-show-region longlines-wrap-beg longlines-wrap-end
))
427 (unless (or (eq this-command
'fill-paragraph
)
428 (eq this-command
'fill-region
))
429 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end
))
430 (setq longlines-wrap-beg nil
)
431 (setq longlines-wrap-end nil
)))
433 (defun longlines-window-change-function ()
434 "Re-wrap the buffer if the window width has changed.
435 This is called by `window-configuration-change-hook'."
436 (let ((dw (if (and (integerp longlines-wrap-follows-window-size
)
437 (>= longlines-wrap-follows-window-size
0)
438 (< longlines-wrap-follows-window-size
(window-width)))
439 longlines-wrap-follows-window-size
441 (when (/= fill-column
(- (window-width) dw
))
442 (setq fill-column
(- (window-width) dw
))
443 (longlines-wrap-region (point-min) (point-max)))))
447 (defun longlines-search-function ()
450 (if isearch-forward
'word-search-forward
'word-search-backward
))
452 (if isearch-forward
're-search-forward
're-search-backward
))
455 'longlines-search-forward
456 'longlines-search-backward
))))
458 (defun longlines-search-forward (string &optional bound noerror count
)
459 (let ((search-spaces-regexp "[ \n]+"))
460 (re-search-forward (regexp-quote string
) bound noerror count
)))
462 (defun longlines-search-backward (string &optional bound noerror count
)
463 (let ((search-spaces-regexp "[ \n]+"))
464 (re-search-backward (regexp-quote string
) bound noerror count
)))
466 ;; Loading and saving
468 (defun longlines-before-revert-hook ()
469 (add-hook 'after-revert-hook
'longlines-after-revert-hook nil t
)
472 (defun longlines-after-revert-hook ()
473 (remove-hook 'after-revert-hook
'longlines-after-revert-hook t
)
478 (list 'longlines
"Automatically wrap long lines." nil nil
479 'longlines-encode-region t nil
))
483 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624
484 ;;; longlines.el ends here