Mercurial > emacs
annotate lisp/longlines.el @ 63666:e0dd6fa42c85
(Fmatch_data): Remove evaporate option.
(Fmatch_data): Do not mention evaporate option in doc string.
Add commentary explaining evaporate arg (for internal use only).
(unwind_set_match_data): Add comment on evaporate use.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Wed, 22 Jun 2005 23:18:59 +0000 |
parents | 6a1d35d4ea03 |
children | 6fb026ad601f |
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 (defgroup longlines nil | |
43 "Automatic wrapping of long lines when loading files." | |
44 :group 'fill) | |
45 | |
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) | |
53 | |
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) | |
61 | |
62 (defcustom longlines-show-hard-newlines nil | |
63557
6a1d35d4ea03
(longlines-mode, longlines-show-hard-newlines): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
63198
diff
changeset
|
63 "*Non-nil means each hard newline is marked on the screen. |
6a1d35d4ea03
(longlines-mode, longlines-show-hard-newlines): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
63198
diff
changeset
|
64 \(The variable `longlines-show-effect' controls what they look like.) |
61110 | 65 You can also enable the display temporarily, using the command |
66 `longlines-show-hard-newlines'" | |
67 :group 'longlines | |
68 :type 'boolean) | |
69 | |
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) | |
75 | |
76 ;; Internal variables | |
77 | |
78 (defvar longlines-wrap-beg nil) | |
79 (defvar longlines-wrap-end nil) | |
80 (defvar longlines-wrap-point nil) | |
81 (defvar longlines-showing nil) | |
82 | |
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) | |
87 | |
88 ;; Mode | |
89 | |
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. | |
96 | |
63557
6a1d35d4ea03
(longlines-mode, longlines-show-hard-newlines): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
63198
diff
changeset
|
97 If the variable `longlines-auto-wrap' is non-nil, lines are automatically |
61110 | 98 wrapped whenever the buffer is changed. You can always call |
99 `fill-paragraph' to fill individual paragraphs. | |
100 | |
63557
6a1d35d4ea03
(longlines-mode, longlines-show-hard-newlines): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
63198
diff
changeset
|
101 If the variable `longlines-show-hard-newlines' is non-nil, hard newlines |
6a1d35d4ea03
(longlines-mode, longlines-show-hard-newlines): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
63198
diff
changeset
|
102 are indicated with a symbol." |
61289
78b96a3ee117
(longlines-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
61125
diff
changeset
|
103 :group 'longlines :lighter " ll" |
61110 | 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 (add-to-list 'buffer-substring-filters 'longlines-encode-string) | |
113 (when longlines-wrap-follows-window-size | |
114 (set (make-local-variable 'fill-column) | |
115 (- (window-width) window-min-width)) | |
116 (add-hook 'window-configuration-change-hook | |
117 'longlines-window-change-function nil t)) | |
118 (let ((buffer-undo-list t) | |
119 (mod (buffer-modified-p))) | |
120 ;; Turning off undo is OK since (spaces + newlines) is | |
121 ;; conserved, except for a corner case in | |
122 ;; longlines-wrap-lines that we'll never encounter from here | |
123 (longlines-decode-region (point-min) (point-max)) | |
124 (longlines-wrap-region (point-min) (point-max)) | |
125 (set-buffer-modified-p mod)) | |
126 (when (and longlines-show-hard-newlines | |
127 (not longlines-showing)) | |
128 (longlines-show-hard-newlines)) | |
129 (when longlines-auto-wrap | |
130 (auto-fill-mode 0) | |
131 (add-hook 'after-change-functions | |
132 'longlines-after-change-function nil t) | |
133 (add-hook 'post-command-hook | |
134 'longlines-post-command-function nil t))) | |
135 ;; Turn off longlines mode | |
136 (setq buffer-file-format (delete 'longlines buffer-file-format)) | |
137 (if longlines-showing | |
138 (longlines-unshow-hard-newlines)) | |
139 (let ((buffer-undo-list t)) | |
140 (longlines-encode-region (point-min) (point-max))) | |
141 (remove-hook 'change-major-mode-hook 'longlines-mode-off t) | |
142 (remove-hook 'before-kill-functions 'longlines-encode-region t) | |
143 (remove-hook 'after-change-functions 'longlines-after-change-function t) | |
144 (remove-hook 'post-command-hook 'longlines-post-command-function t) | |
145 (remove-hook 'window-configuration-change-hook | |
146 'longlines-window-change-function t) | |
147 (kill-local-variable 'fill-column))) | |
148 | |
149 (defun longlines-mode-off () | |
150 "Turn off longlines mode. | |
151 This function exists to be called by `change-major-mode-hook' when the | |
152 major mode changes." | |
153 (longlines-mode 0)) | |
154 | |
155 ;; Showing the effect of hard newlines in the buffer | |
156 | |
157 (defun longlines-show-hard-newlines (&optional arg) | |
158 "Make hard newlines visible by adding a face. | |
159 With optional argument ARG, make the hard newlines invisible again." | |
160 (interactive "P") | |
161 (let ((buffer-undo-list t) | |
162 (mod (buffer-modified-p))) | |
163 (if arg | |
164 (longlines-unshow-hard-newlines) | |
165 (setq longlines-showing t) | |
166 (longlines-show-region (point-min) (point-max))) | |
167 (set-buffer-modified-p mod))) | |
168 | |
169 (defun longlines-show-region (beg end) | |
170 "Make hard newlines between BEG and END visible." | |
171 (let* ((pmin (min beg end)) | |
172 (pmax (max beg end)) | |
173 (pos (text-property-any pmin pmax 'hard t))) | |
174 (while pos | |
175 (put-text-property pos (1+ pos) 'display | |
176 (copy-sequence longlines-show-effect)) | |
177 (setq pos (text-property-any (1+ pos) pmax 'hard t))))) | |
178 | |
179 (defun longlines-unshow-hard-newlines () | |
180 "Make hard newlines invisible again." | |
181 (interactive) | |
182 (setq longlines-showing nil) | |
183 (let ((pos (text-property-any (point-min) (point-max) 'hard t))) | |
184 (while pos | |
185 (remove-text-properties pos (1+ pos) '(display)) | |
186 (setq pos (text-property-any (1+ pos) (point-max) 'hard t))))) | |
187 | |
188 ;; Wrapping the paragraphs. | |
189 | |
190 (defun longlines-wrap-region (beg end) | |
191 "Wrap each successive line, starting with the line before BEG. | |
192 Stop when we reach lines after END that don't need wrapping, or the | |
193 end of the buffer." | |
194 (setq longlines-wrap-point (point)) | |
195 (goto-char beg) | |
196 (forward-line -1) | |
197 ;; Two successful longlines-wrap-line's in a row mean successive | |
198 ;; lines don't need wrapping. | |
199 (while (null (and (longlines-wrap-line) | |
200 (or (eobp) | |
201 (and (>= (point) end) | |
202 (longlines-wrap-line)))))) | |
203 (goto-char longlines-wrap-point)) | |
204 | |
205 (defun longlines-wrap-line () | |
206 "If the current line needs to be wrapped, wrap it and return nil. | |
207 If wrapping is performed, point remains on the line. If the line does | |
208 not need to be wrapped, move point to the next line and return t." | |
209 (if (longlines-set-breakpoint) | |
210 (progn (backward-char 1) | |
211 (delete-char 1) | |
212 (insert-char ?\n 1) | |
213 nil) | |
214 (if (longlines-merge-lines-p) | |
215 (progn (end-of-line) | |
216 (delete-char 1) | |
217 ;; After certain commands (e.g. kill-line), there may be two | |
218 ;; successive soft newlines in the buffer. In this case, we | |
219 ;; replace these two newlines by a single space. Unfortunately, | |
220 ;; this breaks the conservation of (spaces + newlines), so we | |
221 ;; have to fiddle with longlines-wrap-point. | |
222 (if (or (bolp) (eolp)) | |
223 (if (> longlines-wrap-point (point)) | |
224 (setq longlines-wrap-point | |
225 (1- longlines-wrap-point))) | |
226 (insert-char ? 1)) | |
227 nil) | |
228 (forward-line 1) | |
229 t))) | |
230 | |
231 (defun longlines-set-breakpoint () | |
232 "Place point where we should break the current line, and return t. | |
233 If the line should not be broken, return nil; point remains on the | |
234 line." | |
235 (move-to-column fill-column) | |
236 (if (and (re-search-forward "[^ ]" (line-end-position) 1) | |
237 (> (current-column) fill-column)) | |
238 ;; This line is too long. Can we break it? | |
239 (or (longlines-find-break-backward) | |
240 (progn (move-to-column fill-column) | |
241 (longlines-find-break-forward))))) | |
242 | |
243 (defun longlines-find-break-backward () | |
244 "Move point backward to the first available breakpoint and return t. | |
245 If no breakpoint is found, return nil." | |
246 (and (search-backward " " (line-beginning-position) 1) | |
247 (save-excursion | |
248 (skip-chars-backward " " (line-beginning-position)) | |
249 (null (bolp))) | |
250 (progn (forward-char 1) | |
251 (if (and fill-nobreak-predicate | |
252 (run-hook-with-args-until-success | |
253 'fill-nobreak-predicate)) | |
254 (progn (skip-chars-backward " " (line-beginning-position)) | |
255 (longlines-find-break-backward)) | |
256 t)))) | |
257 | |
258 (defun longlines-find-break-forward () | |
259 "Move point forward to the first available breakpoint and return t. | |
260 If no break point is found, return nil." | |
261 (and (search-forward " " (line-end-position) 1) | |
262 (progn (skip-chars-forward " " (line-end-position)) | |
263 (null (eolp))) | |
264 (if (and fill-nobreak-predicate | |
265 (run-hook-with-args-until-success | |
266 'fill-nobreak-predicate)) | |
267 (longlines-find-break-forward) | |
268 t))) | |
269 | |
270 (defun longlines-merge-lines-p () | |
271 "Return t if part of the next line can fit onto the current line. | |
272 Otherwise, return nil. Text cannot be moved across hard newlines." | |
273 (save-excursion | |
274 (end-of-line) | |
275 (and (null (eobp)) | |
276 (null (get-text-property (point) 'hard)) | |
277 (let ((space (- fill-column (current-column)))) | |
278 (forward-line 1) | |
279 (if (eq (char-after) ? ) | |
280 t ; We can always merge some spaces | |
281 (<= (if (search-forward " " (line-end-position) 1) | |
282 (current-column) | |
283 (1+ (current-column))) | |
284 space)))))) | |
285 | |
286 (defun longlines-decode-region (beg end) | |
287 "Turn all newlines between BEG and END into hard newlines." | |
288 (save-excursion | |
289 (goto-char (min beg end)) | |
290 (while (search-forward "\n" (max beg end) t) | |
291 (set-hard-newline-properties | |
292 (match-beginning 0) (match-end 0))))) | |
293 | |
294 (defun longlines-encode-region (beg end &optional buffer) | |
295 "Replace each soft newline between BEG and END with exactly one space. | |
296 Hard newlines are left intact. The optional argument BUFFER exists for | |
297 compatibility with `format-alist', and is ignored." | |
298 (save-excursion | |
299 (let ((mod (buffer-modified-p))) | |
300 (goto-char (min beg end)) | |
301 (while (search-forward "\n" (max (max beg end)) t) | |
302 (unless (get-text-property (match-beginning 0) 'hard) | |
303 (replace-match " "))) | |
304 (set-buffer-modified-p mod) | |
305 end))) | |
306 | |
307 (defun longlines-encode-string (string) | |
308 "Return a copy of STRING with each soft newline replaced by a space. | |
309 Hard newlines are left intact." | |
310 (let* ((str (copy-sequence string)) | |
311 (pos (string-match "\n" str))) | |
312 (while pos | |
313 (if (null (get-text-property pos 'hard str)) | |
314 (aset str pos ? )) | |
315 (setq pos (string-match "\n" str (1+ pos)))) | |
316 str)) | |
317 | |
318 ;; Auto wrap | |
319 | |
320 (defun longlines-auto-wrap (&optional arg) | |
321 "Turn on automatic line wrapping, and wrap the entire buffer. | |
322 With optional argument ARG, turn off line wrapping." | |
323 (interactive "P") | |
324 (remove-hook 'after-change-functions 'longlines-after-change-function t) | |
325 (remove-hook 'post-command-hook 'longlines-post-command-function t) | |
326 (if arg | |
327 (progn (setq longlines-auto-wrap nil) | |
328 (message "Auto wrap disabled.")) | |
329 (setq longlines-auto-wrap t) | |
330 (add-hook 'after-change-functions | |
331 'longlines-after-change-function nil t) | |
332 (add-hook 'post-command-hook | |
333 'longlines-post-command-function nil t) | |
334 (let ((mod (buffer-modified-p))) | |
335 (longlines-wrap-region (point-min) (point-max)) | |
336 (set-buffer-modified-p mod)) | |
337 (message "Auto wrap enabled."))) | |
338 | |
339 (defun longlines-after-change-function (beg end len) | |
340 "Update `longlines-wrap-beg' and `longlines-wrap-end'. | |
341 This is called by `after-change-functions' to keep track of the region | |
342 that has changed." | |
343 (unless undo-in-progress | |
344 (setq longlines-wrap-beg | |
345 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg)) | |
346 (setq longlines-wrap-end | |
347 (if longlines-wrap-end (max longlines-wrap-end end) end)))) | |
348 | |
349 (defun longlines-post-command-function () | |
350 "Perform line wrapping on the parts of the buffer that have changed. | |
351 This is called by `post-command-hook' after each command." | |
352 (when longlines-wrap-beg | |
353 (cond ((or (eq this-command 'yank) | |
354 (eq this-command 'yank-pop)) | |
355 (longlines-decode-region (point) (mark t)) | |
356 (if longlines-showing | |
357 (longlines-show-region (point) (mark t)))) | |
358 ((and (eq this-command 'newline) longlines-showing) | |
359 (save-excursion | |
360 (if (search-backward "\n" nil t) | |
361 (longlines-show-region | |
362 (match-beginning 0) (match-end 0)))))) | |
363 (unless (or (eq this-command 'fill-paragraph) | |
364 (eq this-command 'fill-region)) | |
365 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end)) | |
366 (setq longlines-wrap-beg nil) | |
367 (setq longlines-wrap-end nil))) | |
368 | |
369 (defun longlines-window-change-function () | |
370 "Re-wrap the buffer if the window width has changed. | |
371 This is called by `window-size-change-functions'." | |
372 (when (/= fill-column (- (window-width) window-min-width)) | |
373 (setq fill-column (- (window-width) window-min-width)) | |
374 (let ((mod (buffer-modified-p))) | |
375 (longlines-wrap-region (point-min) (point-max)) | |
376 (set-buffer-modified-p mod)))) | |
377 | |
378 ;; Loading and saving | |
379 | |
380 (add-to-list | |
381 'format-alist | |
382 (list 'longlines "Automatically wrap long lines." nil | |
383 'longlines-decode-region 'longlines-encode-region t nil)) | |
384 | |
385 (provide 'longlines) | |
386 | |
61125
93886f61ae3e
Changes from arch/CVS synchronization
Miles Bader <miles@gnu.org>
parents:
61110
diff
changeset
|
387 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624 |
61110 | 388 ;;; longlines.el ends here |