61110
|
1 ;;; longlines.el --- automatically wrap long lines
|
|
2
|
75347
|
3 ;; Copyright (C) 2000, 2001, 2004, 2005, 2006, 2007 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>
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
|
78236
|
15 ;; the Free Software Foundation; either version 3, or (at your option)
|
61110
|
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
|
72225
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
47 "Non-nil means long lines are automatically wrapped after each command.
|
61110
|
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
|
72225
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
55 "Non-nil means wrapping and filling happen at the edge of the window.
|
61110
|
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
|
72225
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
63 "Non-nil means each hard newline is marked on the screen.
|
63557
|
64 \(The variable `longlines-show-effect' controls what they look like.)
|
61110
|
65 You can also enable the display temporarily, using the command
|
75226
|
66 `longlines-show-hard-newlines'."
|
61110
|
67 :group 'longlines
|
|
68 :type 'boolean)
|
|
69
|
|
70 (defcustom longlines-show-effect (propertize "|\n" 'face 'escape-glyph)
|
72225
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
71 "A string to display when showing hard newlines.
|
61110
|
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)
|
82317
|
82 (defvar longlines-decoded nil)
|
61110
|
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)
|
82317
|
88 (make-variable-buffer-local 'longlines-decoded)
|
61110
|
89
|
|
90 ;; Mode
|
|
91
|
|
92 ;;;###autoload
|
|
93 (define-minor-mode longlines-mode
|
|
94 "Toggle Long Lines mode.
|
|
95 In Long Lines mode, long lines are wrapped if they extend beyond
|
|
96 `fill-column'. The soft newlines used for line wrapping will not
|
|
97 show up when the text is yanked or saved to disk.
|
|
98
|
63557
|
99 If the variable `longlines-auto-wrap' is non-nil, lines are automatically
|
61110
|
100 wrapped whenever the buffer is changed. You can always call
|
|
101 `fill-paragraph' to fill individual paragraphs.
|
|
102
|
63557
|
103 If the variable `longlines-show-hard-newlines' is non-nil, hard newlines
|
|
104 are indicated with a symbol."
|
61289
|
105 :group 'longlines :lighter " ll"
|
61110
|
106 (if longlines-mode
|
|
107 ;; Turn on longlines mode
|
|
108 (progn
|
|
109 (use-hard-newlines 1 'never)
|
|
110 (set (make-local-variable 'require-final-newline) nil)
|
|
111 (add-to-list 'buffer-file-format 'longlines)
|
|
112 (add-hook 'change-major-mode-hook 'longlines-mode-off nil t)
|
67021
|
113 (add-hook 'before-revert-hook 'longlines-before-revert-hook nil t)
|
61110
|
114 (make-local-variable 'buffer-substring-filters)
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
115 (make-local-variable 'longlines-auto-wrap)
|
66088
|
116 (set (make-local-variable 'isearch-search-fun-function)
|
67701
|
117 'longlines-search-function)
|
61110
|
118 (add-to-list 'buffer-substring-filters 'longlines-encode-string)
|
|
119 (when longlines-wrap-follows-window-size
|
|
120 (set (make-local-variable 'fill-column)
|
|
121 (- (window-width) window-min-width))
|
|
122 (add-hook 'window-configuration-change-hook
|
|
123 'longlines-window-change-function nil t))
|
|
124 (let ((buffer-undo-list t)
|
65312
|
125 (inhibit-read-only t)
|
66465
9a896c723ab2
* longlines.el (longlines-mode): Bind after-change-functions to
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
126 (after-change-functions nil)
|
61110
|
127 (mod (buffer-modified-p)))
|
|
128 ;; Turning off undo is OK since (spaces + newlines) is
|
|
129 ;; conserved, except for a corner case in
|
|
130 ;; longlines-wrap-lines that we'll never encounter from here
|
66453
|
131 (save-restriction
|
|
132 (widen)
|
82317
|
133 (unless longlines-decoded
|
|
134 (longlines-decode-buffer)
|
|
135 (setq longlines-decoded t))
|
67673
|
136 (longlines-wrap-region (point-min) (point-max)))
|
61110
|
137 (set-buffer-modified-p mod))
|
|
138 (when (and longlines-show-hard-newlines
|
|
139 (not longlines-showing))
|
|
140 (longlines-show-hard-newlines))
|
66995
|
141
|
|
142 ;; Hacks to make longlines play nice with various modes.
|
|
143 (cond ((eq major-mode 'mail-mode)
|
67213
|
144 (add-hook 'mail-setup-hook 'longlines-decode-buffer nil t)
|
66995
|
145 (or mail-citation-hook
|
|
146 (add-hook 'mail-citation-hook 'mail-indent-citation nil t))
|
|
147 (add-hook 'mail-citation-hook 'longlines-decode-region nil t))
|
|
148 ((eq major-mode 'message-mode)
|
67031
|
149 (add-hook 'message-setup-hook 'longlines-decode-buffer nil t)
|
66995
|
150 (make-local-variable 'message-indent-citation-function)
|
|
151 (if (not (listp message-indent-citation-function))
|
|
152 (setq message-indent-citation-function
|
|
153 (list message-indent-citation-function)))
|
|
154 (add-to-list 'message-indent-citation-function
|
|
155 'longlines-decode-region t)))
|
66996
|
156
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
157 (add-hook 'after-change-functions 'longlines-after-change-function nil t)
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
158 (add-hook 'post-command-hook 'longlines-post-command-function nil t)
|
66996
|
159 (when longlines-auto-wrap
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
160 (auto-fill-mode 0)))
|
61110
|
161 ;; Turn off longlines mode
|
|
162 (setq buffer-file-format (delete 'longlines buffer-file-format))
|
|
163 (if longlines-showing
|
|
164 (longlines-unshow-hard-newlines))
|
65312
|
165 (let ((buffer-undo-list t)
|
66465
9a896c723ab2
* longlines.el (longlines-mode): Bind after-change-functions to
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
166 (after-change-functions nil)
|
65312
|
167 (inhibit-read-only t))
|
82317
|
168 (if longlines-decoded
|
|
169 (save-restriction
|
|
170 (widen)
|
|
171 (longlines-encode-region (point-min) (point-max))
|
|
172 (setq longlines-decoded nil))))
|
61110
|
173 (remove-hook 'change-major-mode-hook 'longlines-mode-off t)
|
|
174 (remove-hook 'after-change-functions 'longlines-after-change-function t)
|
|
175 (remove-hook 'post-command-hook 'longlines-post-command-function t)
|
67021
|
176 (remove-hook 'before-revert-hook 'longlines-before-revert-hook t)
|
61110
|
177 (remove-hook 'window-configuration-change-hook
|
|
178 'longlines-window-change-function t)
|
65312
|
179 (when longlines-wrap-follows-window-size
|
|
180 (kill-local-variable 'fill-column))
|
66088
|
181 (kill-local-variable 'isearch-search-fun-function)
|
65312
|
182 (kill-local-variable 'require-final-newline)
|
|
183 (kill-local-variable 'buffer-substring-filters)
|
|
184 (kill-local-variable 'use-hard-newlines)))
|
61110
|
185
|
|
186 (defun longlines-mode-off ()
|
|
187 "Turn off longlines mode.
|
|
188 This function exists to be called by `change-major-mode-hook' when the
|
|
189 major mode changes."
|
|
190 (longlines-mode 0))
|
|
191
|
|
192 ;; Showing the effect of hard newlines in the buffer
|
|
193
|
|
194 (defun longlines-show-hard-newlines (&optional arg)
|
|
195 "Make hard newlines visible by adding a face.
|
|
196 With optional argument ARG, make the hard newlines invisible again."
|
|
197 (interactive "P")
|
|
198 (let ((buffer-undo-list t)
|
|
199 (mod (buffer-modified-p)))
|
|
200 (if arg
|
|
201 (longlines-unshow-hard-newlines)
|
|
202 (setq longlines-showing t)
|
|
203 (longlines-show-region (point-min) (point-max)))
|
|
204 (set-buffer-modified-p mod)))
|
|
205
|
|
206 (defun longlines-show-region (beg end)
|
|
207 "Make hard newlines between BEG and END visible."
|
|
208 (let* ((pmin (min beg end))
|
|
209 (pmax (max beg end))
|
72225
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
210 (pos (text-property-not-all pmin pmax 'hard nil))
|
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
211 (inhibit-read-only t))
|
61110
|
212 (while pos
|
|
213 (put-text-property pos (1+ pos) 'display
|
|
214 (copy-sequence longlines-show-effect))
|
64216
|
215 (setq pos (text-property-not-all (1+ pos) pmax 'hard nil)))))
|
61110
|
216
|
|
217 (defun longlines-unshow-hard-newlines ()
|
|
218 "Make hard newlines invisible again."
|
|
219 (interactive)
|
|
220 (setq longlines-showing nil)
|
64216
|
221 (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil)))
|
61110
|
222 (while pos
|
|
223 (remove-text-properties pos (1+ pos) '(display))
|
64216
|
224 (setq pos (text-property-not-all (1+ pos) (point-max) 'hard nil)))))
|
61110
|
225
|
|
226 ;; Wrapping the paragraphs.
|
|
227
|
|
228 (defun longlines-wrap-region (beg end)
|
|
229 "Wrap each successive line, starting with the line before BEG.
|
|
230 Stop when we reach lines after END that don't need wrapping, or the
|
|
231 end of the buffer."
|
78088
|
232 (let ((mod (buffer-modified-p)))
|
|
233 (setq longlines-wrap-point (point))
|
|
234 (goto-char beg)
|
|
235 (forward-line -1)
|
|
236 ;; Two successful longlines-wrap-line's in a row mean successive
|
|
237 ;; lines don't need wrapping.
|
|
238 (while (null (and (longlines-wrap-line)
|
|
239 (or (eobp)
|
|
240 (and (>= (point) end)
|
|
241 (longlines-wrap-line))))))
|
|
242 (goto-char longlines-wrap-point)
|
|
243 (set-buffer-modified-p mod)))
|
61110
|
244
|
|
245 (defun longlines-wrap-line ()
|
|
246 "If the current line needs to be wrapped, wrap it and return nil.
|
|
247 If wrapping is performed, point remains on the line. If the line does
|
|
248 not need to be wrapped, move point to the next line and return t."
|
|
249 (if (longlines-set-breakpoint)
|
67033
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
250 (progn (insert-before-markers ?\n)
|
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
251 (backward-char 1)
|
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
252 (delete-char -1)
|
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
253 (forward-char 1)
|
61110
|
254 nil)
|
|
255 (if (longlines-merge-lines-p)
|
|
256 (progn (end-of-line)
|
|
257 ;; After certain commands (e.g. kill-line), there may be two
|
|
258 ;; successive soft newlines in the buffer. In this case, we
|
|
259 ;; replace these two newlines by a single space. Unfortunately,
|
|
260 ;; this breaks the conservation of (spaces + newlines), so we
|
|
261 ;; have to fiddle with longlines-wrap-point.
|
67120
|
262 (if (or (prog1 (bolp) (forward-char 1)) (eolp))
|
|
263 (progn
|
|
264 (delete-char -1)
|
|
265 (if (> longlines-wrap-point (point))
|
|
266 (setq longlines-wrap-point
|
|
267 (1- longlines-wrap-point))))
|
74234
|
268 (insert-before-markers-and-inherit ?\s)
|
67120
|
269 (backward-char 1)
|
|
270 (delete-char -1)
|
|
271 (forward-char 1))
|
61110
|
272 nil)
|
|
273 (forward-line 1)
|
|
274 t)))
|
|
275
|
|
276 (defun longlines-set-breakpoint ()
|
|
277 "Place point where we should break the current line, and return t.
|
|
278 If the line should not be broken, return nil; point remains on the
|
|
279 line."
|
|
280 (move-to-column fill-column)
|
|
281 (if (and (re-search-forward "[^ ]" (line-end-position) 1)
|
|
282 (> (current-column) fill-column))
|
|
283 ;; This line is too long. Can we break it?
|
|
284 (or (longlines-find-break-backward)
|
|
285 (progn (move-to-column fill-column)
|
|
286 (longlines-find-break-forward)))))
|
|
287
|
|
288 (defun longlines-find-break-backward ()
|
|
289 "Move point backward to the first available breakpoint and return t.
|
|
290 If no breakpoint is found, return nil."
|
|
291 (and (search-backward " " (line-beginning-position) 1)
|
|
292 (save-excursion
|
|
293 (skip-chars-backward " " (line-beginning-position))
|
|
294 (null (bolp)))
|
|
295 (progn (forward-char 1)
|
|
296 (if (and fill-nobreak-predicate
|
|
297 (run-hook-with-args-until-success
|
|
298 'fill-nobreak-predicate))
|
|
299 (progn (skip-chars-backward " " (line-beginning-position))
|
|
300 (longlines-find-break-backward))
|
|
301 t))))
|
|
302
|
|
303 (defun longlines-find-break-forward ()
|
|
304 "Move point forward to the first available breakpoint and return t.
|
|
305 If no break point is found, return nil."
|
|
306 (and (search-forward " " (line-end-position) 1)
|
|
307 (progn (skip-chars-forward " " (line-end-position))
|
|
308 (null (eolp)))
|
|
309 (if (and fill-nobreak-predicate
|
|
310 (run-hook-with-args-until-success
|
|
311 'fill-nobreak-predicate))
|
|
312 (longlines-find-break-forward)
|
|
313 t)))
|
|
314
|
|
315 (defun longlines-merge-lines-p ()
|
|
316 "Return t if part of the next line can fit onto the current line.
|
|
317 Otherwise, return nil. Text cannot be moved across hard newlines."
|
|
318 (save-excursion
|
|
319 (end-of-line)
|
|
320 (and (null (eobp))
|
|
321 (null (get-text-property (point) 'hard))
|
|
322 (let ((space (- fill-column (current-column))))
|
|
323 (forward-line 1)
|
|
324 (if (eq (char-after) ? )
|
|
325 t ; We can always merge some spaces
|
|
326 (<= (if (search-forward " " (line-end-position) 1)
|
|
327 (current-column)
|
|
328 (1+ (current-column)))
|
|
329 space))))))
|
|
330
|
66995
|
331 (defun longlines-decode-region (&optional beg end)
|
|
332 "Turn all newlines between BEG and END into hard newlines.
|
|
333 If BEG and END are nil, the point and mark are used."
|
|
334 (if (null beg) (setq beg (point)))
|
|
335 (if (null end) (setq end (mark t)))
|
61110
|
336 (save-excursion
|
67673
|
337 (let ((reg-max (max beg end)))
|
|
338 (goto-char (min beg end))
|
|
339 (while (search-forward "\n" reg-max t)
|
|
340 (set-hard-newline-properties
|
|
341 (match-beginning 0) (match-end 0))))))
|
61110
|
342
|
67031
|
343 (defun longlines-decode-buffer ()
|
|
344 "Turn all newlines in the buffer into hard newlines."
|
|
345 (longlines-decode-region (point-min) (point-max)))
|
|
346
|
61110
|
347 (defun longlines-encode-region (beg end &optional buffer)
|
|
348 "Replace each soft newline between BEG and END with exactly one space.
|
|
349 Hard newlines are left intact. The optional argument BUFFER exists for
|
|
350 compatibility with `format-alist', and is ignored."
|
|
351 (save-excursion
|
67673
|
352 (let ((reg-max (max beg end))
|
|
353 (mod (buffer-modified-p)))
|
61110
|
354 (goto-char (min beg end))
|
67673
|
355 (while (search-forward "\n" reg-max t)
|
61110
|
356 (unless (get-text-property (match-beginning 0) 'hard)
|
|
357 (replace-match " ")))
|
|
358 (set-buffer-modified-p mod)
|
|
359 end)))
|
|
360
|
|
361 (defun longlines-encode-string (string)
|
|
362 "Return a copy of STRING with each soft newline replaced by a space.
|
|
363 Hard newlines are left intact."
|
|
364 (let* ((str (copy-sequence string))
|
|
365 (pos (string-match "\n" str)))
|
|
366 (while pos
|
|
367 (if (null (get-text-property pos 'hard str))
|
|
368 (aset str pos ? ))
|
|
369 (setq pos (string-match "\n" str (1+ pos))))
|
|
370 str))
|
|
371
|
|
372 ;; Auto wrap
|
|
373
|
|
374 (defun longlines-auto-wrap (&optional arg)
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
375 "Toggle automatic line wrapping.
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
376 With optional argument ARG, turn on line wrapping if and only if ARG is positive.
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
377 If automatic line wrapping is turned on, wrap the entire buffer."
|
61110
|
378 (interactive "P")
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
379 (setq arg (if arg
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
380 (> (prefix-numeric-value arg) 0)
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
381 (not longlines-auto-wrap)))
|
61110
|
382 (if arg
|
78088
|
383 (progn
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
384 (setq longlines-auto-wrap t)
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
385 (longlines-wrap-region (point-min) (point-max))
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
386 (message "Auto wrap enabled."))
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
387 (setq longlines-auto-wrap nil)
|
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
388 (message "Auto wrap disabled.")))
|
61110
|
389
|
|
390 (defun longlines-after-change-function (beg end len)
|
|
391 "Update `longlines-wrap-beg' and `longlines-wrap-end'.
|
|
392 This is called by `after-change-functions' to keep track of the region
|
|
393 that has changed."
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
394 (when (and longlines-auto-wrap (not undo-in-progress))
|
61110
|
395 (setq longlines-wrap-beg
|
|
396 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg))
|
|
397 (setq longlines-wrap-end
|
|
398 (if longlines-wrap-end (max longlines-wrap-end end) end))))
|
|
399
|
|
400 (defun longlines-post-command-function ()
|
|
401 "Perform line wrapping on the parts of the buffer that have changed.
|
|
402 This is called by `post-command-hook' after each command."
|
77871
2b390b4a6ba8
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
403 (when (and longlines-auto-wrap longlines-wrap-beg)
|
71873
|
404 (if (or (eq this-command 'yank)
|
|
405 (eq this-command 'yank-pop))
|
|
406 (longlines-decode-region (point) (mark t)))
|
|
407 (if longlines-showing
|
|
408 (longlines-show-region longlines-wrap-beg longlines-wrap-end))
|
61110
|
409 (unless (or (eq this-command 'fill-paragraph)
|
|
410 (eq this-command 'fill-region))
|
|
411 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end))
|
|
412 (setq longlines-wrap-beg nil)
|
|
413 (setq longlines-wrap-end nil)))
|
|
414
|
|
415 (defun longlines-window-change-function ()
|
|
416 "Re-wrap the buffer if the window width has changed.
|
73330
|
417 This is called by `window-configuration-change-hook'."
|
61110
|
418 (when (/= fill-column (- (window-width) window-min-width))
|
|
419 (setq fill-column (- (window-width) window-min-width))
|
78088
|
420 (longlines-wrap-region (point-min) (point-max))))
|
61110
|
421
|
66088
|
422 ;; Isearch
|
|
423
|
67701
|
424 (defun longlines-search-function ()
|
66088
|
425 (cond
|
|
426 (isearch-word
|
|
427 (if isearch-forward 'word-search-forward 'word-search-backward))
|
|
428 (isearch-regexp
|
|
429 (if isearch-forward 're-search-forward 're-search-backward))
|
|
430 (t
|
|
431 (if isearch-forward
|
|
432 'longlines-search-forward
|
|
433 'longlines-search-backward))))
|
|
434
|
|
435 (defun longlines-search-forward (string &optional bound noerror count)
|
66132
|
436 (let ((search-spaces-regexp "[ \n]+"))
|
66088
|
437 (re-search-forward (regexp-quote string) bound noerror count)))
|
|
438
|
|
439 (defun longlines-search-backward (string &optional bound noerror count)
|
66132
|
440 (let ((search-spaces-regexp "[ \n]+"))
|
66088
|
441 (re-search-backward (regexp-quote string) bound noerror count)))
|
|
442
|
61110
|
443 ;; Loading and saving
|
|
444
|
67021
|
445 (defun longlines-before-revert-hook ()
|
|
446 (add-hook 'after-revert-hook 'longlines-after-revert-hook nil t)
|
|
447 (longlines-mode 0))
|
|
448
|
|
449 (defun longlines-after-revert-hook ()
|
|
450 (remove-hook 'after-revert-hook 'longlines-after-revert-hook t)
|
|
451 (longlines-mode 1))
|
|
452
|
61110
|
453 (add-to-list
|
|
454 'format-alist
|
67024
|
455 (list 'longlines "Automatically wrap long lines." nil nil
|
67021
|
456 'longlines-encode-region t nil))
|
61110
|
457
|
|
458 (provide 'longlines)
|
|
459
|
61125
|
460 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624
|
61110
|
461 ;;; longlines.el ends here
|