Mercurial > emacs
annotate lisp/longlines.el @ 67004:5a972cf172a7
(ls-lisp-format-file-size): Format file size with 9 digits, not 8, to avoid
misalignment for files larger than 100MB.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sat, 19 Nov 2005 11:38:23 +0000 |
parents | cf7bf8dadf78 |
children | e220d76071f7 |
rev | line source |
---|---|
61110 | 1 ;;; longlines.el --- automatically wrap long lines |
2 | |
64762
41bb365f41c4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64216
diff
changeset
|
3 ;; Copyright (C) 2000, 2001, 2005 Free Software Foundation, Inc. |
61110 | 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> | |
66465
9a896c723ab2
* longlines.el (longlines-mode): Bind after-change-functions to
Chong Yidong <cyd@stupidchicken.com>
parents:
66453
diff
changeset
|
9 ;; Keywords: convenience, wp |
61110 | 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 | |
64091 | 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 ;; Boston, MA 02110-1301, USA. | |
61110 | 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) | |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
112 (set (make-local-variable 'isearch-search-fun-function) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
113 'longlinges-search-function) |
61110 | 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) | |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
121 (inhibit-read-only t) |
66465
9a896c723ab2
* longlines.el (longlines-mode): Bind after-change-functions to
Chong Yidong <cyd@stupidchicken.com>
parents:
66453
diff
changeset
|
122 (after-change-functions nil) |
61110 | 123 (mod (buffer-modified-p))) |
124 ;; Turning off undo is OK since (spaces + newlines) is | |
125 ;; conserved, except for a corner case in | |
126 ;; longlines-wrap-lines that we'll never encounter from here | |
66453
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
127 (save-restriction |
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
128 (widen) |
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
129 (longlines-decode-region (point-min) (point-max))) |
61110 | 130 (longlines-wrap-region (point-min) (point-max)) |
131 (set-buffer-modified-p mod)) | |
132 (when (and longlines-show-hard-newlines | |
133 (not longlines-showing)) | |
134 (longlines-show-hard-newlines)) | |
66995
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
135 |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
136 ;; Hacks to make longlines play nice with various modes. |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
137 (cond ((eq major-mode 'mail-mode) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
138 (or mail-citation-hook |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
139 (add-hook 'mail-citation-hook 'mail-indent-citation nil t)) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
140 (add-hook 'mail-citation-hook 'longlines-decode-region nil t)) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
141 ((eq major-mode 'message-mode) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
142 (make-local-variable 'message-indent-citation-function) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
143 (if (not (listp message-indent-citation-function)) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
144 (setq message-indent-citation-function |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
145 (list message-indent-citation-function))) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
146 (add-to-list 'message-indent-citation-function |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
147 'longlines-decode-region t))) |
66996
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
148 |
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
149 (when longlines-auto-wrap |
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
150 (auto-fill-mode 0) |
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
151 (add-hook 'after-change-functions |
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
152 'longlines-after-change-function nil t) |
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
153 (add-hook 'post-command-hook |
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
154 'longlines-post-command-function nil t))) |
61110 | 155 ;; Turn off longlines mode |
156 (setq buffer-file-format (delete 'longlines buffer-file-format)) | |
157 (if longlines-showing | |
158 (longlines-unshow-hard-newlines)) | |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
159 (let ((buffer-undo-list t) |
66465
9a896c723ab2
* longlines.el (longlines-mode): Bind after-change-functions to
Chong Yidong <cyd@stupidchicken.com>
parents:
66453
diff
changeset
|
160 (after-change-functions nil) |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
161 (inhibit-read-only t)) |
66453
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
162 (save-restriction |
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
163 (widen) |
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
164 (longlines-encode-region (point-min) (point-max)))) |
61110 | 165 (remove-hook 'change-major-mode-hook 'longlines-mode-off t) |
166 (remove-hook 'before-kill-functions 'longlines-encode-region t) | |
167 (remove-hook 'after-change-functions 'longlines-after-change-function t) | |
168 (remove-hook 'post-command-hook 'longlines-post-command-function t) | |
169 (remove-hook 'window-configuration-change-hook | |
170 'longlines-window-change-function t) | |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
171 (when longlines-wrap-follows-window-size |
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
172 (kill-local-variable 'fill-column)) |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
173 (kill-local-variable 'isearch-search-fun-function) |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
174 (kill-local-variable 'require-final-newline) |
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
175 (kill-local-variable 'buffer-substring-filters) |
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
176 (kill-local-variable 'use-hard-newlines))) |
61110 | 177 |
178 (defun longlines-mode-off () | |
179 "Turn off longlines mode. | |
180 This function exists to be called by `change-major-mode-hook' when the | |
181 major mode changes." | |
182 (longlines-mode 0)) | |
183 | |
184 ;; Showing the effect of hard newlines in the buffer | |
185 | |
186 (defun longlines-show-hard-newlines (&optional arg) | |
187 "Make hard newlines visible by adding a face. | |
188 With optional argument ARG, make the hard newlines invisible again." | |
189 (interactive "P") | |
190 (let ((buffer-undo-list t) | |
191 (mod (buffer-modified-p))) | |
192 (if arg | |
193 (longlines-unshow-hard-newlines) | |
194 (setq longlines-showing t) | |
195 (longlines-show-region (point-min) (point-max))) | |
196 (set-buffer-modified-p mod))) | |
197 | |
198 (defun longlines-show-region (beg end) | |
199 "Make hard newlines between BEG and END visible." | |
200 (let* ((pmin (min beg end)) | |
201 (pmax (max beg end)) | |
64216
6c19076eecb0
(longlines-show-region, longlines-unshow-hard-newlines):
Richard M. Stallman <rms@gnu.org>
parents:
64091
diff
changeset
|
202 (pos (text-property-not-all pmin pmax 'hard nil))) |
61110 | 203 (while pos |
204 (put-text-property pos (1+ pos) 'display | |
205 (copy-sequence longlines-show-effect)) | |
64216
6c19076eecb0
(longlines-show-region, longlines-unshow-hard-newlines):
Richard M. Stallman <rms@gnu.org>
parents:
64091
diff
changeset
|
206 (setq pos (text-property-not-all (1+ pos) pmax 'hard nil))))) |
61110 | 207 |
208 (defun longlines-unshow-hard-newlines () | |
209 "Make hard newlines invisible again." | |
210 (interactive) | |
211 (setq longlines-showing nil) | |
64216
6c19076eecb0
(longlines-show-region, longlines-unshow-hard-newlines):
Richard M. Stallman <rms@gnu.org>
parents:
64091
diff
changeset
|
212 (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil))) |
61110 | 213 (while pos |
214 (remove-text-properties pos (1+ pos) '(display)) | |
64216
6c19076eecb0
(longlines-show-region, longlines-unshow-hard-newlines):
Richard M. Stallman <rms@gnu.org>
parents:
64091
diff
changeset
|
215 (setq pos (text-property-not-all (1+ pos) (point-max) 'hard nil))))) |
61110 | 216 |
217 ;; Wrapping the paragraphs. | |
218 | |
219 (defun longlines-wrap-region (beg end) | |
220 "Wrap each successive line, starting with the line before BEG. | |
221 Stop when we reach lines after END that don't need wrapping, or the | |
222 end of the buffer." | |
223 (setq longlines-wrap-point (point)) | |
224 (goto-char beg) | |
225 (forward-line -1) | |
226 ;; Two successful longlines-wrap-line's in a row mean successive | |
227 ;; lines don't need wrapping. | |
228 (while (null (and (longlines-wrap-line) | |
229 (or (eobp) | |
230 (and (>= (point) end) | |
231 (longlines-wrap-line)))))) | |
232 (goto-char longlines-wrap-point)) | |
233 | |
234 (defun longlines-wrap-line () | |
235 "If the current line needs to be wrapped, wrap it and return nil. | |
236 If wrapping is performed, point remains on the line. If the line does | |
237 not need to be wrapped, move point to the next line and return t." | |
238 (if (longlines-set-breakpoint) | |
239 (progn (backward-char 1) | |
240 (delete-char 1) | |
241 (insert-char ?\n 1) | |
242 nil) | |
243 (if (longlines-merge-lines-p) | |
244 (progn (end-of-line) | |
245 (delete-char 1) | |
246 ;; After certain commands (e.g. kill-line), there may be two | |
247 ;; successive soft newlines in the buffer. In this case, we | |
248 ;; replace these two newlines by a single space. Unfortunately, | |
249 ;; this breaks the conservation of (spaces + newlines), so we | |
250 ;; have to fiddle with longlines-wrap-point. | |
251 (if (or (bolp) (eolp)) | |
252 (if (> longlines-wrap-point (point)) | |
253 (setq longlines-wrap-point | |
254 (1- longlines-wrap-point))) | |
255 (insert-char ? 1)) | |
256 nil) | |
257 (forward-line 1) | |
258 t))) | |
259 | |
260 (defun longlines-set-breakpoint () | |
261 "Place point where we should break the current line, and return t. | |
262 If the line should not be broken, return nil; point remains on the | |
263 line." | |
264 (move-to-column fill-column) | |
265 (if (and (re-search-forward "[^ ]" (line-end-position) 1) | |
266 (> (current-column) fill-column)) | |
267 ;; This line is too long. Can we break it? | |
268 (or (longlines-find-break-backward) | |
269 (progn (move-to-column fill-column) | |
270 (longlines-find-break-forward))))) | |
271 | |
272 (defun longlines-find-break-backward () | |
273 "Move point backward to the first available breakpoint and return t. | |
274 If no breakpoint is found, return nil." | |
275 (and (search-backward " " (line-beginning-position) 1) | |
276 (save-excursion | |
277 (skip-chars-backward " " (line-beginning-position)) | |
278 (null (bolp))) | |
279 (progn (forward-char 1) | |
280 (if (and fill-nobreak-predicate | |
281 (run-hook-with-args-until-success | |
282 'fill-nobreak-predicate)) | |
283 (progn (skip-chars-backward " " (line-beginning-position)) | |
284 (longlines-find-break-backward)) | |
285 t)))) | |
286 | |
287 (defun longlines-find-break-forward () | |
288 "Move point forward to the first available breakpoint and return t. | |
289 If no break point is found, return nil." | |
290 (and (search-forward " " (line-end-position) 1) | |
291 (progn (skip-chars-forward " " (line-end-position)) | |
292 (null (eolp))) | |
293 (if (and fill-nobreak-predicate | |
294 (run-hook-with-args-until-success | |
295 'fill-nobreak-predicate)) | |
296 (longlines-find-break-forward) | |
297 t))) | |
298 | |
299 (defun longlines-merge-lines-p () | |
300 "Return t if part of the next line can fit onto the current line. | |
301 Otherwise, return nil. Text cannot be moved across hard newlines." | |
302 (save-excursion | |
303 (end-of-line) | |
304 (and (null (eobp)) | |
305 (null (get-text-property (point) 'hard)) | |
306 (let ((space (- fill-column (current-column)))) | |
307 (forward-line 1) | |
308 (if (eq (char-after) ? ) | |
309 t ; We can always merge some spaces | |
310 (<= (if (search-forward " " (line-end-position) 1) | |
311 (current-column) | |
312 (1+ (current-column))) | |
313 space)))))) | |
314 | |
66995
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
315 (defun longlines-decode-region (&optional beg end) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
316 "Turn all newlines between BEG and END into hard newlines. |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
317 If BEG and END are nil, the point and mark are used." |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
318 (if (null beg) (setq beg (point))) |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
319 (if (null end) (setq end (mark t))) |
61110 | 320 (save-excursion |
321 (goto-char (min beg end)) | |
322 (while (search-forward "\n" (max beg end) t) | |
323 (set-hard-newline-properties | |
324 (match-beginning 0) (match-end 0))))) | |
325 | |
326 (defun longlines-encode-region (beg end &optional buffer) | |
327 "Replace each soft newline between BEG and END with exactly one space. | |
328 Hard newlines are left intact. The optional argument BUFFER exists for | |
329 compatibility with `format-alist', and is ignored." | |
330 (save-excursion | |
331 (let ((mod (buffer-modified-p))) | |
332 (goto-char (min beg end)) | |
333 (while (search-forward "\n" (max (max beg end)) t) | |
334 (unless (get-text-property (match-beginning 0) 'hard) | |
335 (replace-match " "))) | |
336 (set-buffer-modified-p mod) | |
337 end))) | |
338 | |
339 (defun longlines-encode-string (string) | |
340 "Return a copy of STRING with each soft newline replaced by a space. | |
341 Hard newlines are left intact." | |
342 (let* ((str (copy-sequence string)) | |
343 (pos (string-match "\n" str))) | |
344 (while pos | |
345 (if (null (get-text-property pos 'hard str)) | |
346 (aset str pos ? )) | |
347 (setq pos (string-match "\n" str (1+ pos)))) | |
348 str)) | |
349 | |
350 ;; Auto wrap | |
351 | |
352 (defun longlines-auto-wrap (&optional arg) | |
353 "Turn on automatic line wrapping, and wrap the entire buffer. | |
354 With optional argument ARG, turn off line wrapping." | |
355 (interactive "P") | |
356 (remove-hook 'after-change-functions 'longlines-after-change-function t) | |
357 (remove-hook 'post-command-hook 'longlines-post-command-function t) | |
358 (if arg | |
359 (progn (setq longlines-auto-wrap nil) | |
360 (message "Auto wrap disabled.")) | |
361 (setq longlines-auto-wrap t) | |
362 (add-hook 'after-change-functions | |
363 'longlines-after-change-function nil t) | |
364 (add-hook 'post-command-hook | |
365 'longlines-post-command-function nil t) | |
366 (let ((mod (buffer-modified-p))) | |
367 (longlines-wrap-region (point-min) (point-max)) | |
368 (set-buffer-modified-p mod)) | |
369 (message "Auto wrap enabled."))) | |
370 | |
371 (defun longlines-after-change-function (beg end len) | |
372 "Update `longlines-wrap-beg' and `longlines-wrap-end'. | |
373 This is called by `after-change-functions' to keep track of the region | |
374 that has changed." | |
375 (unless undo-in-progress | |
376 (setq longlines-wrap-beg | |
377 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg)) | |
378 (setq longlines-wrap-end | |
379 (if longlines-wrap-end (max longlines-wrap-end end) end)))) | |
380 | |
381 (defun longlines-post-command-function () | |
382 "Perform line wrapping on the parts of the buffer that have changed. | |
383 This is called by `post-command-hook' after each command." | |
384 (when longlines-wrap-beg | |
385 (cond ((or (eq this-command 'yank) | |
386 (eq this-command 'yank-pop)) | |
387 (longlines-decode-region (point) (mark t)) | |
388 (if longlines-showing | |
389 (longlines-show-region (point) (mark t)))) | |
390 ((and (eq this-command 'newline) longlines-showing) | |
391 (save-excursion | |
392 (if (search-backward "\n" nil t) | |
393 (longlines-show-region | |
394 (match-beginning 0) (match-end 0)))))) | |
395 (unless (or (eq this-command 'fill-paragraph) | |
396 (eq this-command 'fill-region)) | |
397 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end)) | |
398 (setq longlines-wrap-beg nil) | |
399 (setq longlines-wrap-end nil))) | |
400 | |
401 (defun longlines-window-change-function () | |
402 "Re-wrap the buffer if the window width has changed. | |
403 This is called by `window-size-change-functions'." | |
404 (when (/= fill-column (- (window-width) window-min-width)) | |
405 (setq fill-column (- (window-width) window-min-width)) | |
406 (let ((mod (buffer-modified-p))) | |
407 (longlines-wrap-region (point-min) (point-max)) | |
408 (set-buffer-modified-p mod)))) | |
409 | |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
410 ;; Isearch |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
411 |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
412 (defun longlinges-search-function () |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
413 (cond |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
414 (isearch-word |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
415 (if isearch-forward 'word-search-forward 'word-search-backward)) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
416 (isearch-regexp |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
417 (if isearch-forward 're-search-forward 're-search-backward)) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
418 (t |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
419 (if isearch-forward |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
420 'longlines-search-forward |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
421 'longlines-search-backward)))) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
422 |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
423 (defun longlines-search-forward (string &optional bound noerror count) |
66132
3fe20f1edf7b
* longlines.el (longlines-search-forward)
Chong Yidong <cyd@stupidchicken.com>
parents:
66088
diff
changeset
|
424 (let ((search-spaces-regexp "[ \n]+")) |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
425 (re-search-forward (regexp-quote string) bound noerror count))) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
426 |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
427 (defun longlines-search-backward (string &optional bound noerror count) |
66132
3fe20f1edf7b
* longlines.el (longlines-search-forward)
Chong Yidong <cyd@stupidchicken.com>
parents:
66088
diff
changeset
|
428 (let ((search-spaces-regexp "[ \n]+")) |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
429 (re-search-backward (regexp-quote string) bound noerror count))) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
430 |
61110 | 431 ;; Loading and saving |
432 | |
433 (add-to-list | |
434 'format-alist | |
435 (list 'longlines "Automatically wrap long lines." nil | |
436 'longlines-decode-region 'longlines-encode-region t nil)) | |
437 | |
438 (provide 'longlines) | |
439 | |
61125
93886f61ae3e
Changes from arch/CVS synchronization
Miles Bader <miles@gnu.org>
parents:
61110
diff
changeset
|
440 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624 |
61110 | 441 ;;; longlines.el ends here |