Mercurial > emacs
annotate lisp/longlines.el @ 81862:4dbb728eaf94
*** empty log message ***
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Sat, 14 Jul 2007 11:04:31 +0000 |
parents | c8bb24d5506d |
children | b98604865ea0 |
rev | line source |
---|---|
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>
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 | |
72225
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
71873
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>
parents:
71873
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>
parents:
71873
diff
changeset
|
63 "Non-nil means each hard newline is marked on the screen. |
63557
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 |
75226
7257e69d3378
(longlines-show-hard-newlines): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
74234
diff
changeset
|
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>
parents:
71873
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) | |
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) | |
67021
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
111 (add-hook 'before-revert-hook 'longlines-before-revert-hook nil t) |
61110 | 112 (make-local-variable 'buffer-substring-filters) |
80903
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
113 (make-local-variable 'longlines-auto-wrap) |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
114 (set (make-local-variable 'isearch-search-fun-function) |
67701 | 115 'longlines-search-function) |
61110 | 116 (add-to-list 'buffer-substring-filters 'longlines-encode-string) |
117 (when longlines-wrap-follows-window-size | |
118 (set (make-local-variable 'fill-column) | |
119 (- (window-width) window-min-width)) | |
120 (add-hook 'window-configuration-change-hook | |
121 'longlines-window-change-function nil t)) | |
122 (let ((buffer-undo-list t) | |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
123 (inhibit-read-only t) |
66465
9a896c723ab2
* longlines.el (longlines-mode): Bind after-change-functions to
Chong Yidong <cyd@stupidchicken.com>
parents:
66453
diff
changeset
|
124 (after-change-functions nil) |
61110 | 125 (mod (buffer-modified-p))) |
126 ;; Turning off undo is OK since (spaces + newlines) is | |
127 ;; conserved, except for a corner case in | |
128 ;; 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
|
129 (save-restriction |
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
130 (widen) |
67673
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
131 (longlines-decode-buffer) |
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
132 (longlines-wrap-region (point-min) (point-max))) |
61110 | 133 (set-buffer-modified-p mod)) |
134 (when (and longlines-show-hard-newlines | |
135 (not longlines-showing)) | |
136 (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
|
137 |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
138 ;; 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
|
139 (cond ((eq major-mode 'mail-mode) |
67213
818361523ce8
* longlines.el (longlines-mode): Add mail-setup-hook.
Chong Yidong <cyd@stupidchicken.com>
parents:
67120
diff
changeset
|
140 (add-hook 'mail-setup-hook 'longlines-decode-buffer nil t) |
66995
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
141 (or mail-citation-hook |
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
142 (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
|
143 (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
|
144 ((eq major-mode 'message-mode) |
67031
9dbc51935e01
(longlines-mode): Add a message-setup-hook.
Chong Yidong <cyd@stupidchicken.com>
parents:
67024
diff
changeset
|
145 (add-hook 'message-setup-hook 'longlines-decode-buffer nil t) |
66995
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
146 (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
|
147 (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
|
148 (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
|
149 (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
|
150 (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
|
151 'longlines-decode-region t))) |
66996
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
152 |
80903
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
153 (add-hook 'after-change-functions 'longlines-after-change-function nil t) |
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
154 (add-hook 'post-command-hook 'longlines-post-command-function nil t) |
66996
cf7bf8dadf78
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
66995
diff
changeset
|
155 (when longlines-auto-wrap |
80903
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
156 (auto-fill-mode 0))) |
61110 | 157 ;; Turn off longlines mode |
158 (setq buffer-file-format (delete 'longlines buffer-file-format)) | |
159 (if longlines-showing | |
160 (longlines-unshow-hard-newlines)) | |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
161 (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
|
162 (after-change-functions nil) |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
163 (inhibit-read-only t)) |
66453
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
164 (save-restriction |
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
165 (widen) |
ce840b1d13ad
* longlines.el (longlines-mode): Remove narrowing before
Chong Yidong <cyd@stupidchicken.com>
parents:
66132
diff
changeset
|
166 (longlines-encode-region (point-min) (point-max)))) |
61110 | 167 (remove-hook 'change-major-mode-hook 'longlines-mode-off t) |
168 (remove-hook 'after-change-functions 'longlines-after-change-function t) | |
169 (remove-hook 'post-command-hook 'longlines-post-command-function t) | |
67021
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
170 (remove-hook 'before-revert-hook 'longlines-before-revert-hook t) |
61110 | 171 (remove-hook 'window-configuration-change-hook |
172 'longlines-window-change-function t) | |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
173 (when longlines-wrap-follows-window-size |
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
174 (kill-local-variable 'fill-column)) |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
175 (kill-local-variable 'isearch-search-fun-function) |
65312
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
176 (kill-local-variable 'require-final-newline) |
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
177 (kill-local-variable 'buffer-substring-filters) |
915d671fef52
*** empty log message ***
Chong Yidong <cyd@stupidchicken.com>
parents:
64762
diff
changeset
|
178 (kill-local-variable 'use-hard-newlines))) |
61110 | 179 |
180 (defun longlines-mode-off () | |
181 "Turn off longlines mode. | |
182 This function exists to be called by `change-major-mode-hook' when the | |
183 major mode changes." | |
184 (longlines-mode 0)) | |
185 | |
186 ;; Showing the effect of hard newlines in the buffer | |
187 | |
188 (defun longlines-show-hard-newlines (&optional arg) | |
189 "Make hard newlines visible by adding a face. | |
190 With optional argument ARG, make the hard newlines invisible again." | |
191 (interactive "P") | |
192 (let ((buffer-undo-list t) | |
193 (mod (buffer-modified-p))) | |
194 (if arg | |
195 (longlines-unshow-hard-newlines) | |
196 (setq longlines-showing t) | |
197 (longlines-show-region (point-min) (point-max))) | |
198 (set-buffer-modified-p mod))) | |
199 | |
200 (defun longlines-show-region (beg end) | |
201 "Make hard newlines between BEG and END visible." | |
202 (let* ((pmin (min beg end)) | |
203 (pmax (max beg end)) | |
72225
1d3db748db52
(longlines-show-region): Make it work on read-only buffers as well.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
71873
diff
changeset
|
204 (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>
parents:
71873
diff
changeset
|
205 (inhibit-read-only t)) |
61110 | 206 (while pos |
207 (put-text-property pos (1+ pos) 'display | |
208 (copy-sequence longlines-show-effect)) | |
64216
6c19076eecb0
(longlines-show-region, longlines-unshow-hard-newlines):
Richard M. Stallman <rms@gnu.org>
parents:
64091
diff
changeset
|
209 (setq pos (text-property-not-all (1+ pos) pmax 'hard nil))))) |
61110 | 210 |
211 (defun longlines-unshow-hard-newlines () | |
212 "Make hard newlines invisible again." | |
213 (interactive) | |
214 (setq longlines-showing nil) | |
64216
6c19076eecb0
(longlines-show-region, longlines-unshow-hard-newlines):
Richard M. Stallman <rms@gnu.org>
parents:
64091
diff
changeset
|
215 (let ((pos (text-property-not-all (point-min) (point-max) 'hard nil))) |
61110 | 216 (while pos |
217 (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
|
218 (setq pos (text-property-not-all (1+ pos) (point-max) 'hard nil))))) |
61110 | 219 |
220 ;; Wrapping the paragraphs. | |
221 | |
222 (defun longlines-wrap-region (beg end) | |
223 "Wrap each successive line, starting with the line before BEG. | |
224 Stop when we reach lines after END that don't need wrapping, or the | |
225 end of the buffer." | |
81761
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
226 (let ((mod (buffer-modified-p))) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
227 (setq longlines-wrap-point (point)) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
228 (goto-char beg) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
229 (forward-line -1) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
230 ;; Two successful longlines-wrap-line's in a row mean successive |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
231 ;; lines don't need wrapping. |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
232 (while (null (and (longlines-wrap-line) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
233 (or (eobp) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
234 (and (>= (point) end) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
235 (longlines-wrap-line)))))) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
236 (goto-char longlines-wrap-point) |
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
237 (set-buffer-modified-p mod))) |
61110 | 238 |
239 (defun longlines-wrap-line () | |
240 "If the current line needs to be wrapped, wrap it and return nil. | |
241 If wrapping is performed, point remains on the line. If the line does | |
242 not need to be wrapped, move point to the next line and return t." | |
243 (if (longlines-set-breakpoint) | |
67033
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
parents:
67031
diff
changeset
|
244 (progn (insert-before-markers ?\n) |
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
parents:
67031
diff
changeset
|
245 (backward-char 1) |
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
parents:
67031
diff
changeset
|
246 (delete-char -1) |
1a383aa3be8a
longlines.el (longlines-wrap-line): Preserve marker positions.
Chong Yidong <cyd@stupidchicken.com>
parents:
67031
diff
changeset
|
247 (forward-char 1) |
61110 | 248 nil) |
249 (if (longlines-merge-lines-p) | |
250 (progn (end-of-line) | |
251 ;; After certain commands (e.g. kill-line), there may be two | |
252 ;; successive soft newlines in the buffer. In this case, we | |
253 ;; replace these two newlines by a single space. Unfortunately, | |
254 ;; this breaks the conservation of (spaces + newlines), so we | |
255 ;; have to fiddle with longlines-wrap-point. | |
67120
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
256 (if (or (prog1 (bolp) (forward-char 1)) (eolp)) |
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
257 (progn |
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
258 (delete-char -1) |
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
259 (if (> longlines-wrap-point (point)) |
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
260 (setq longlines-wrap-point |
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
261 (1- longlines-wrap-point)))) |
74234
d966c458c782
(longlines-wrap-line): "?\ " -> "?\s".
Juanma Barranquero <lekktu@gmail.com>
parents:
73330
diff
changeset
|
262 (insert-before-markers-and-inherit ?\s) |
67120
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
263 (backward-char 1) |
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
264 (delete-char -1) |
d3b833785ae6
(longlines-wrap-line): Reorder wrapping to "insert
David Kastrup <dak@gnu.org>
parents:
67033
diff
changeset
|
265 (forward-char 1)) |
61110 | 266 nil) |
267 (forward-line 1) | |
268 t))) | |
269 | |
270 (defun longlines-set-breakpoint () | |
271 "Place point where we should break the current line, and return t. | |
272 If the line should not be broken, return nil; point remains on the | |
273 line." | |
274 (move-to-column fill-column) | |
275 (if (and (re-search-forward "[^ ]" (line-end-position) 1) | |
276 (> (current-column) fill-column)) | |
277 ;; This line is too long. Can we break it? | |
278 (or (longlines-find-break-backward) | |
279 (progn (move-to-column fill-column) | |
280 (longlines-find-break-forward))))) | |
281 | |
282 (defun longlines-find-break-backward () | |
283 "Move point backward to the first available breakpoint and return t. | |
284 If no breakpoint is found, return nil." | |
285 (and (search-backward " " (line-beginning-position) 1) | |
286 (save-excursion | |
287 (skip-chars-backward " " (line-beginning-position)) | |
288 (null (bolp))) | |
289 (progn (forward-char 1) | |
290 (if (and fill-nobreak-predicate | |
291 (run-hook-with-args-until-success | |
292 'fill-nobreak-predicate)) | |
293 (progn (skip-chars-backward " " (line-beginning-position)) | |
294 (longlines-find-break-backward)) | |
295 t)))) | |
296 | |
297 (defun longlines-find-break-forward () | |
298 "Move point forward to the first available breakpoint and return t. | |
299 If no break point is found, return nil." | |
300 (and (search-forward " " (line-end-position) 1) | |
301 (progn (skip-chars-forward " " (line-end-position)) | |
302 (null (eolp))) | |
303 (if (and fill-nobreak-predicate | |
304 (run-hook-with-args-until-success | |
305 'fill-nobreak-predicate)) | |
306 (longlines-find-break-forward) | |
307 t))) | |
308 | |
309 (defun longlines-merge-lines-p () | |
310 "Return t if part of the next line can fit onto the current line. | |
311 Otherwise, return nil. Text cannot be moved across hard newlines." | |
312 (save-excursion | |
313 (end-of-line) | |
314 (and (null (eobp)) | |
315 (null (get-text-property (point) 'hard)) | |
316 (let ((space (- fill-column (current-column)))) | |
317 (forward-line 1) | |
318 (if (eq (char-after) ? ) | |
319 t ; We can always merge some spaces | |
320 (<= (if (search-forward " " (line-end-position) 1) | |
321 (current-column) | |
322 (1+ (current-column))) | |
323 space)))))) | |
324 | |
66995
e10f2e7919f0
Add hacks for citation in mail-mode and message-mode.
Chong Yidong <cyd@stupidchicken.com>
parents:
66465
diff
changeset
|
325 (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
|
326 "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
|
327 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
|
328 (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
|
329 (if (null end) (setq end (mark t))) |
61110 | 330 (save-excursion |
67673
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
331 (let ((reg-max (max beg end))) |
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
332 (goto-char (min beg end)) |
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
333 (while (search-forward "\n" reg-max t) |
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
334 (set-hard-newline-properties |
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
335 (match-beginning 0) (match-end 0)))))) |
61110 | 336 |
67031
9dbc51935e01
(longlines-mode): Add a message-setup-hook.
Chong Yidong <cyd@stupidchicken.com>
parents:
67024
diff
changeset
|
337 (defun longlines-decode-buffer () |
9dbc51935e01
(longlines-mode): Add a message-setup-hook.
Chong Yidong <cyd@stupidchicken.com>
parents:
67024
diff
changeset
|
338 "Turn all newlines in the buffer into hard newlines." |
9dbc51935e01
(longlines-mode): Add a message-setup-hook.
Chong Yidong <cyd@stupidchicken.com>
parents:
67024
diff
changeset
|
339 (longlines-decode-region (point-min) (point-max))) |
9dbc51935e01
(longlines-mode): Add a message-setup-hook.
Chong Yidong <cyd@stupidchicken.com>
parents:
67024
diff
changeset
|
340 |
61110 | 341 (defun longlines-encode-region (beg end &optional buffer) |
342 "Replace each soft newline between BEG and END with exactly one space. | |
343 Hard newlines are left intact. The optional argument BUFFER exists for | |
344 compatibility with `format-alist', and is ignored." | |
345 (save-excursion | |
67673
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
346 (let ((reg-max (max beg end)) |
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
347 (mod (buffer-modified-p))) |
61110 | 348 (goto-char (min beg end)) |
67673
947510e65020
* longlines.el (longlines-mode): Wrap while widened.
Chong Yidong <cyd@stupidchicken.com>
parents:
67213
diff
changeset
|
349 (while (search-forward "\n" reg-max t) |
61110 | 350 (unless (get-text-property (match-beginning 0) 'hard) |
351 (replace-match " "))) | |
352 (set-buffer-modified-p mod) | |
353 end))) | |
354 | |
355 (defun longlines-encode-string (string) | |
356 "Return a copy of STRING with each soft newline replaced by a space. | |
357 Hard newlines are left intact." | |
358 (let* ((str (copy-sequence string)) | |
359 (pos (string-match "\n" str))) | |
360 (while pos | |
361 (if (null (get-text-property pos 'hard str)) | |
362 (aset str pos ? )) | |
363 (setq pos (string-match "\n" str (1+ pos)))) | |
364 str)) | |
365 | |
366 ;; Auto wrap | |
367 | |
368 (defun longlines-auto-wrap (&optional arg) | |
80903
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
369 "Toggle automatic line wrapping. |
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
370 With optional argument ARG, turn on line wrapping if and only if ARG is positive. |
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
371 If automatic line wrapping is turned on, wrap the entire buffer." |
61110 | 372 (interactive "P") |
81142
d0d5c1ec56b8
(longlines-auto-wrap): Handle argument correctly.
Chong Yidong <cyd@stupidchicken.com>
parents:
80903
diff
changeset
|
373 (setq arg (if arg |
d0d5c1ec56b8
(longlines-auto-wrap): Handle argument correctly.
Chong Yidong <cyd@stupidchicken.com>
parents:
80903
diff
changeset
|
374 (> (prefix-numeric-value arg) 0) |
d0d5c1ec56b8
(longlines-auto-wrap): Handle argument correctly.
Chong Yidong <cyd@stupidchicken.com>
parents:
80903
diff
changeset
|
375 (not longlines-auto-wrap))) |
d0d5c1ec56b8
(longlines-auto-wrap): Handle argument correctly.
Chong Yidong <cyd@stupidchicken.com>
parents:
80903
diff
changeset
|
376 (if arg |
81761
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
377 (progn |
80903
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
378 (setq longlines-auto-wrap t) |
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
379 (longlines-wrap-region (point-min) (point-max)) |
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
380 (message "Auto wrap enabled.")) |
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
381 (setq longlines-auto-wrap nil) |
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
382 (message "Auto wrap disabled."))) |
61110 | 383 |
384 (defun longlines-after-change-function (beg end len) | |
385 "Update `longlines-wrap-beg' and `longlines-wrap-end'. | |
386 This is called by `after-change-functions' to keep track of the region | |
387 that has changed." | |
80903
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
388 (when (and longlines-auto-wrap (not undo-in-progress)) |
61110 | 389 (setq longlines-wrap-beg |
390 (if longlines-wrap-beg (min longlines-wrap-beg beg) beg)) | |
391 (setq longlines-wrap-end | |
392 (if longlines-wrap-end (max longlines-wrap-end end) end)))) | |
393 | |
394 (defun longlines-post-command-function () | |
395 "Perform line wrapping on the parts of the buffer that have changed. | |
396 This is called by `post-command-hook' after each command." | |
80903
b088dc2e413f
(longlines-mode): Make longlines-auto-wrap buffer-local. Add hooks unconditionally.
Chong Yidong <cyd@stupidchicken.com>
parents:
75347
diff
changeset
|
397 (when (and longlines-auto-wrap longlines-wrap-beg) |
71873 | 398 (if (or (eq this-command 'yank) |
399 (eq this-command 'yank-pop)) | |
400 (longlines-decode-region (point) (mark t))) | |
401 (if longlines-showing | |
402 (longlines-show-region longlines-wrap-beg longlines-wrap-end)) | |
61110 | 403 (unless (or (eq this-command 'fill-paragraph) |
404 (eq this-command 'fill-region)) | |
405 (longlines-wrap-region longlines-wrap-beg longlines-wrap-end)) | |
406 (setq longlines-wrap-beg nil) | |
407 (setq longlines-wrap-end nil))) | |
408 | |
409 (defun longlines-window-change-function () | |
410 "Re-wrap the buffer if the window width has changed. | |
73330
400487787181
* dnd.el (dnd-handle-one-url): Fix typo in doc-string.
Jan Djärv <jan.h.d@swipnet.se>
parents:
72225
diff
changeset
|
411 This is called by `window-configuration-change-hook'." |
61110 | 412 (when (/= fill-column (- (window-width) window-min-width)) |
413 (setq fill-column (- (window-width) window-min-width)) | |
81761
c8bb24d5506d
(longlines-wrap-region): Avoid marking buffer as modified.
Chong Yidong <cyd@stupidchicken.com>
parents:
81142
diff
changeset
|
414 (longlines-wrap-region (point-min) (point-max)))) |
61110 | 415 |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
416 ;; Isearch |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
417 |
67701 | 418 (defun longlines-search-function () |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
419 (cond |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
420 (isearch-word |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
421 (if isearch-forward 'word-search-forward 'word-search-backward)) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
422 (isearch-regexp |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
423 (if isearch-forward 're-search-forward 're-search-backward)) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
424 (t |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
425 (if isearch-forward |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
426 'longlines-search-forward |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
427 'longlines-search-backward)))) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
428 |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
429 (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
|
430 (let ((search-spaces-regexp "[ \n]+")) |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
431 (re-search-forward (regexp-quote string) bound noerror count))) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
432 |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
433 (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
|
434 (let ((search-spaces-regexp "[ \n]+")) |
66088
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
435 (re-search-backward (regexp-quote string) bound noerror count))) |
b63142efae15
* longlines.el (longlinges-search-function)
Chong Yidong <cyd@stupidchicken.com>
parents:
65312
diff
changeset
|
436 |
61110 | 437 ;; Loading and saving |
438 | |
67021
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
439 (defun longlines-before-revert-hook () |
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
440 (add-hook 'after-revert-hook 'longlines-after-revert-hook nil t) |
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
441 (longlines-mode 0)) |
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
442 |
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
443 (defun longlines-after-revert-hook () |
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
444 (remove-hook 'after-revert-hook 'longlines-after-revert-hook t) |
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
445 (longlines-mode 1)) |
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
446 |
61110 | 447 (add-to-list |
448 'format-alist | |
67024 | 449 (list 'longlines "Automatically wrap long lines." nil nil |
67021
e220d76071f7
* longlines.el (longlines-before-revert-hook)
Chong Yidong <cyd@stupidchicken.com>
parents:
66996
diff
changeset
|
450 'longlines-encode-region t nil)) |
61110 | 451 |
452 (provide 'longlines) | |
453 | |
61125
93886f61ae3e
Changes from arch/CVS synchronization
Miles Bader <miles@gnu.org>
parents:
61110
diff
changeset
|
454 ;; arch-tag: 3489d225-5506-47b9-8659-d8807b77c624 |
61110 | 455 ;;; longlines.el ends here |