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