Mercurial > emacs
annotate lisp/textmodes/fill.el @ 8967:80b92977ed08
(overlays_at): New arg PREV_PTR.
Return previous overlay end.
(Foverlays_at): Pass NULL for last 2 args of overlays_at.
(Fnext_overlay_change): Pass new arg to overlays_at.
(Fprevious_overlay_change): New function.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 21 Sep 1994 08:14:33 +0000 |
parents | bf559544bd48 |
children | 74fd93bc3568 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
1 ;;; fill.el --- fill commands for Emacs |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
2 |
7300 | 3 ;; Copyright (C) 1985, 1986, 1992, 1994 Free Software Foundation, Inc. |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
821
diff
changeset
|
4 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
5 ;; Keywords: wp |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
6 |
75 | 7 ;; This file is part of GNU Emacs. |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
11 ;; the Free Software Foundation; either version 2, or (at your option) |
75 | 12 ;; any later version. |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
23 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
24 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
25 ;; All the commands for filling text. These are documented in the Emacs |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
26 ;; manual. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2109
diff
changeset
|
27 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
28 ;;; Code: |
75 | 29 |
638 | 30 (defconst fill-individual-varying-indent nil |
31 "*Controls criterion for a new paragraph in `fill-individual-paragraphs'. | |
32 Non-nil means changing indent doesn't end a paragraph. | |
33 That mode can handle paragraphs with extra indentation on the first line, | |
34 but it requires separator lines between paragraphs. | |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
35 A value of nil means that any change in indentation starts a new paragraph.") |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
36 |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
37 (defconst sentence-end-double-space t |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
38 "*Non-nil means a single space does not end a sentence.") |
638 | 39 |
75 | 40 (defun set-fill-prefix () |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
41 "Set the fill prefix to the current line up to point. |
218 | 42 Filling expects lines to start with the fill prefix and |
43 reinserts the fill prefix in each resulting line." | |
75 | 44 (interactive) |
45 (setq fill-prefix (buffer-substring | |
46 (save-excursion (beginning-of-line) (point)) | |
47 (point))) | |
48 (if (equal fill-prefix "") | |
49 (setq fill-prefix nil)) | |
50 (if fill-prefix | |
51 (message "fill-prefix: \"%s\"" fill-prefix) | |
52 (message "fill-prefix cancelled"))) | |
53 | |
218 | 54 (defconst adaptive-fill-mode t |
55 "*Non-nil means determine a paragraph's fill prefix from its text.") | |
56 | |
57 (defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?" | |
58 "*Regexp to match text at start of line that constitutes indentation. | |
59 If Adaptive Fill mode is enabled, whatever text matches this pattern | |
60 on the second line of a paragraph is used as the standard indentation | |
61 for the paragraph.") | |
62 | |
75 | 63 (defun fill-region-as-paragraph (from to &optional justify-flag) |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
64 "Fill region as one paragraph: break lines to fit `fill-column'. |
75 | 65 Prefix arg means justify too. |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
66 If `sentence-end-double-space' is non-nil, then period followed by one |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
67 space does not end a sentence, so don't break a line there. |
75 | 68 From program, pass args FROM, TO and JUSTIFY-FLAG." |
69 (interactive "r\nP") | |
1332
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
70 ;; Arrange for undoing the fill to restore point. |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
71 (if (and buffer-undo-list (not (eq buffer-undo-list t))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
72 (setq buffer-undo-list (cons (point) buffer-undo-list))) |
218 | 73 ;; Don't let Adaptive Fill mode alter the fill prefix permanently. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
74 (let ((fill-prefix fill-prefix)) |
218 | 75 ;; Figure out how this paragraph is indented, if desired. |
670
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
76 (if (and adaptive-fill-mode |
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
77 (or (null fill-prefix) (string= fill-prefix ""))) |
218 | 78 (save-excursion |
79 (goto-char (min from to)) | |
80 (if (eolp) (forward-line 1)) | |
81 (forward-line 1) | |
82 (if (< (point) (max from to)) | |
83 (let ((start (point))) | |
84 (re-search-forward adaptive-fill-regexp) | |
85 (setq fill-prefix (buffer-substring start (point)))) | |
86 (goto-char (min from to)) | |
87 (if (eolp) (forward-line 1)) | |
7721
b04897534717
(fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
88 ;; If paragraph has only one line, don't assume in general |
218 | 89 ;; that additional lines would have the same starting |
729
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
90 ;; decoration. Assume no indentation. |
7721
b04897534717
(fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
91 ;; But if left-margin is nonzero, we can assume ordinary |
b04897534717
(fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
92 ;; lines do have indentation. |
b04897534717
(fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
93 (if (> left-margin 0) |
b04897534717
(fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
94 (progn |
b04897534717
(fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
95 (re-search-forward adaptive-fill-regexp) |
b04897534717
(fill-region-as-paragraph): if left-margin is nonzero,
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
96 (setq fill-prefix (make-string (current-column) ?\ )))) |
729
5d684b81ac6b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
719
diff
changeset
|
97 ))) |
75 | 98 |
218 | 99 (save-restriction |
100 (narrow-to-region from to) | |
101 (goto-char (point-min)) | |
102 (skip-chars-forward "\n") | |
103 (narrow-to-region (point) (point-max)) | |
104 (setq from (point)) | |
105 (goto-char (point-max)) | |
106 (let ((fpre (and fill-prefix (not (equal fill-prefix "")) | |
107 (regexp-quote fill-prefix)))) | |
108 ;; Delete the fill prefix from every line except the first. | |
109 ;; The first line may not even have a fill prefix. | |
110 (and fpre | |
111 (progn | |
112 (if (>= (length fill-prefix) fill-column) | |
113 (error "fill-prefix too long for specified width")) | |
114 (goto-char (point-min)) | |
115 (forward-line 1) | |
116 (while (not (eobp)) | |
117 (if (looking-at fpre) | |
118 (delete-region (point) (match-end 0))) | |
119 (forward-line 1)) | |
120 (goto-char (point-min)) | |
121 (and (looking-at fpre) (forward-char (length fill-prefix))) | |
122 (setq from (point))))) | |
123 ;; from is now before the text to fill, | |
124 ;; but after any fill prefix on the first line. | |
75 | 125 |
218 | 126 ;; Make sure sentences ending at end of line get an extra space. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
127 ;; loses on split abbrevs ("Mr.\nSmith") |
218 | 128 (goto-char from) |
670
bff41708644e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
129 (while (re-search-forward "[.?!][])}\"']*$" nil t) |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
130 (insert ? )) |
218 | 131 |
132 ;; Then change all newlines to spaces. | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
133 (subst-char-in-region from (point-max) ?\n ?\ ) |
2376
f7a3795bd1c5
(fill-paragraph, justify-current-line) Now uses the skip-syntax-
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2307
diff
changeset
|
134 |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
135 ;; Flush excess spaces, except in the paragraph indentation. |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
136 (goto-char from) |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
137 (skip-chars-forward " \t") |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
138 ;; Nuke tabs while we're at it; they get screwed up in a fill. |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
139 ;; This is quick, but loses when a tab follows the end of a sentence. |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
140 ;; Actually, it is difficult to tell that from "Mr.\tSmith". |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
141 ;; Blame the typist. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
142 (subst-char-in-region (point) (point-max) ?\t ?\ ) |
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
143 (while (re-search-forward " *" nil t) |
218 | 144 (delete-region |
145 (+ (match-beginning 0) | |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
146 (if (and sentence-end-double-space |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
147 (save-excursion |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
148 (skip-chars-backward " ]})\"'") |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
149 (memq (preceding-char) '(?. ?? ?!)))) |
218 | 150 2 1)) |
151 (match-end 0))) | |
152 (goto-char (point-max)) | |
153 (delete-horizontal-space) | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
154 (insert " ") |
218 | 155 (goto-char (point-min)) |
156 | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
157 ;; This is the actual filling loop. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
158 (let ((prefixcol 0) linebeg) |
218 | 159 (while (not (eobp)) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
160 (setq linebeg (point)) |
218 | 161 (move-to-column (1+ fill-column)) |
162 (if (eobp) | |
163 nil | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
164 ;; Move back to start of word. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
165 (skip-chars-backward "^ \n" linebeg) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
166 ;; Don't break after a period followed by just one space. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
167 ;; Move back to the previous place to break. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
168 ;; The reason is that if a period ends up at the end of a line, |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
169 ;; further fills will assume it ends a sentence. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
170 ;; If we now know it does not end a sentence, |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
171 ;; avoid putting it at the end of the line. |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
172 (if sentence-end-double-space |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
173 (while (and (> (point) (+ linebeg 2)) |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
174 (eq (preceding-char) ?\ ) |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
175 (not (eq (following-char) ?\ )) |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
176 (eq (char-after (- (point) 2)) ?\.)) |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
177 (forward-char -2) |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
178 (skip-chars-backward "^ \n" linebeg))) |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
179 (if (if (zerop prefixcol) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
180 (save-excursion |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
181 (skip-chars-backward " " linebeg) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
182 (bolp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
183 (>= prefixcol (current-column))) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
184 ;; Keep at least one word even if fill prefix exceeds margin. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
185 ;; This handles all but the first line of the paragraph. |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
186 ;; Meanwhile, don't stop at a period followed by one space. |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
187 (let ((first t)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
188 (move-to-column prefixcol) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
189 (while (and (not (eobp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
190 (or first |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
191 (and (not (bobp)) |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
192 sentence-end-double-space |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
193 (save-excursion (forward-char -1) |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
194 (and (looking-at "\\. ") |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
195 (not (looking-at "\\. "))))))) |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
196 (skip-chars-forward " ") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
197 (skip-chars-forward "^ \n") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
198 (setq first nil))) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
199 ;; Normally, move back over the single space between the words. |
4352
d00889d02f75
(fill-region-as-paragraph): Move misplaced paren
Richard M. Stallman <rms@gnu.org>
parents:
2520
diff
changeset
|
200 (forward-char -1)) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
201 (if (and fill-prefix (zerop prefixcol) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
202 (< (- (point) (point-min)) (length fill-prefix)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
203 (string= (buffer-substring (point-min) (point)) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
204 (substring fill-prefix 0 (- (point) (point-min))))) |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
205 ;; Keep at least one word even if fill prefix exceeds margin. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
206 ;; This handles the first line of the paragraph. |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
207 ;; Don't stop at a period followed by just one space. |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
208 (let ((first t)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
209 (while (and (not (eobp)) |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
210 (or first |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
211 (and (not (bobp)) |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
212 sentence-end-double-space |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
213 (save-excursion (forward-char -1) |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
214 (and (looking-at "\\. ") |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
215 (not (looking-at "\\. "))))))) |
4413
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
216 (skip-chars-forward " ") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
217 (skip-chars-forward "^ \n") |
5a00cec8e9b0
(fill-region-as-paragraph): When we take one word
Richard M. Stallman <rms@gnu.org>
parents:
4352
diff
changeset
|
218 (setq first nil))))) |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
219 ;; Replace all whitespace here with one newline. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
220 ;; Insert before deleting, so we don't forget which side of |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
221 ;; the whitespace point or markers used to be on. |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
222 (skip-chars-backward " ") |
218 | 223 (insert ?\n) |
224 (delete-horizontal-space) | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
225 ;; Insert the fill prefix at start of each line. |
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
226 ;; Set prefixcol so whitespace in the prefix won't get lost. |
218 | 227 (and (not (eobp)) fill-prefix (not (equal fill-prefix "")) |
228 (progn | |
229 (insert fill-prefix) | |
230 (setq prefixcol (current-column)))) | |
719
3007551721dc
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
670
diff
changeset
|
231 ;; Justify the line just ended, if desired. |
218 | 232 (and justify-flag (not (eobp)) |
233 (progn | |
234 (forward-line -1) | |
235 (justify-current-line) | |
236 (forward-line 1)))))))) | |
75 | 237 |
238 (defun fill-paragraph (arg) | |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
239 "Fill paragraph at or after point. Prefix arg means justify as well. |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
240 If `sentence-end-double-space' is non-nil, then period followed by one |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
241 space does not end a sentence, so don't break a line there." |
75 | 242 (interactive "P") |
1332
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
243 (let ((before (point))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
244 (save-excursion |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
245 (forward-paragraph) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
246 (or (bolp) (newline 1)) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
247 (let ((end (point)) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
248 (beg (progn (backward-paragraph) (point)))) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
249 (goto-char before) |
955ebdb98095
(fill-paragraph): Don't actually change point before
Richard M. Stallman <rms@gnu.org>
parents:
1067
diff
changeset
|
250 (fill-region-as-paragraph beg end arg))))) |
75 | 251 |
252 (defun fill-region (from to &optional justify-flag) | |
253 "Fill each of the paragraphs in the region. | |
5770
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
254 Prefix arg (non-nil third arg, if called from program) means justify as well. |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
255 If `sentence-end-double-space' is non-nil, then period followed by one |
92dd602768d2
(sentence-end-double-space): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
5291
diff
changeset
|
256 space does not end a sentence, so don't break a line there." |
75 | 257 (interactive "r\nP") |
258 (save-restriction | |
259 (narrow-to-region from to) | |
260 (goto-char (point-min)) | |
261 (while (not (eobp)) | |
262 (let ((initial (point)) | |
263 (end (progn | |
264 (forward-paragraph 1) (point)))) | |
265 (forward-paragraph -1) | |
266 (if (>= (point) initial) | |
267 (fill-region-as-paragraph (point) end justify-flag) | |
268 (goto-char end)))))) | |
269 | |
270 (defun justify-current-line () | |
218 | 271 "Add spaces to line point is in, so it ends at `fill-column'." |
75 | 272 (interactive) |
273 (save-excursion | |
274 (save-restriction | |
8363
bf559544bd48
(justify-current-line): Delete space at end of line.
Richard M. Stallman <rms@gnu.org>
parents:
7721
diff
changeset
|
275 (let (ncols beg indent end) |
75 | 276 (beginning-of-line) |
277 (forward-char (length fill-prefix)) | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
278 (skip-chars-forward " \t") |
218 | 279 (setq indent (current-column)) |
75 | 280 (setq beg (point)) |
281 (end-of-line) | |
282 (narrow-to-region beg (point)) | |
8363
bf559544bd48
(justify-current-line): Delete space at end of line.
Richard M. Stallman <rms@gnu.org>
parents:
7721
diff
changeset
|
283 (setq end (point)) |
bf559544bd48
(justify-current-line): Delete space at end of line.
Richard M. Stallman <rms@gnu.org>
parents:
7721
diff
changeset
|
284 (skip-chars-backward " \t") |
bf559544bd48
(justify-current-line): Delete space at end of line.
Richard M. Stallman <rms@gnu.org>
parents:
7721
diff
changeset
|
285 (delete-char (- end (point))) |
75 | 286 (goto-char beg) |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
287 (while (re-search-forward " *" nil t) |
75 | 288 (delete-region |
289 (+ (match-beginning 0) | |
290 (if (save-excursion | |
2409
d03c7b8ac284
Cancel previous change; this version should be identical to version 1.17.
Richard M. Stallman <rms@gnu.org>
parents:
2376
diff
changeset
|
291 (skip-chars-backward " ])\"'") |
75 | 292 (memq (preceding-char) '(?. ?? ?!))) |
293 2 1)) | |
294 (match-end 0))) | |
295 (goto-char beg) | |
1067 | 296 (while (re-search-forward "[.?!][])\"']*\n" nil t) |
75 | 297 (forward-char -1) |
298 (insert ? )) | |
299 (goto-char (point-max)) | |
218 | 300 ;; Note that the buffer bounds start after the indentation, |
301 ;; so the columns counted by INDENT don't appear in (current-column). | |
302 (setq ncols (- fill-column (current-column) indent)) | |
75 | 303 (if (search-backward " " nil t) |
304 (while (> ncols 0) | |
305 (let ((nmove (+ 3 (random 3)))) | |
306 (while (> nmove 0) | |
307 (or (search-backward " " nil t) | |
308 (progn | |
309 (goto-char (point-max)) | |
310 (search-backward " "))) | |
311 (skip-chars-backward " ") | |
312 (setq nmove (1- nmove)))) | |
313 (insert " ") | |
314 (skip-chars-backward " ") | |
2109
73485780a8f9
* simple.el (yank, yank-pop): Always return nil; don't rely on
Jim Blandy <jimb@redhat.com>
parents:
1332
diff
changeset
|
315 (setq ncols (1- ncols))))))) |
73485780a8f9
* simple.el (yank, yank-pop): Always return nil; don't rely on
Jim Blandy <jimb@redhat.com>
parents:
1332
diff
changeset
|
316 nil) |
75 | 317 |
2520
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
318 (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
319 "Fill paragraphs within the region, allowing varying indentation within each. |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
320 This command divides the region into \"paragraphs\", |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
321 only at paragraph-separator lines, then fills each paragraph |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
322 using as the fill prefix the smallest indentation of any line |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
323 in the paragraph. |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
324 |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
325 When calling from a program, pass range to fill as first two arguments. |
638 | 326 |
2520
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
327 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG: |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
328 JUSTIFY-FLAG to justify paragraphs (prefix arg), |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
329 MAIL-FLAG for a mail message, i. e. don't fill header lines." |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
330 (interactive "r\nP") |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
331 (let ((fill-individual-varying-indent t)) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
332 (fill-individual-paragraphs min max justifyp mailp))) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
333 |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
334 (defun fill-individual-paragraphs (min max &optional justifyp mailp) |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
335 "Fill paragraphs of uniform indentation within the region. |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
336 This command divides the region into \"paragraphs\", |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
337 treating every change in indentation level as a paragraph boundary, |
ef643dbb7d40
(fill-nonuniform-paragraphs): New command.
Richard M. Stallman <rms@gnu.org>
parents:
2412
diff
changeset
|
338 then fills each paragraph using its indentation level as the fill prefix. |
638 | 339 |
340 When calling from a program, pass range to fill as first two arguments. | |
341 | |
75 | 342 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG: |
343 JUSTIFY-FLAG to justify paragraphs (prefix arg), | |
344 MAIL-FLAG for a mail message, i. e. don't fill header lines." | |
345 (interactive "r\nP") | |
474 | 346 (save-restriction |
347 (save-excursion | |
348 (goto-char min) | |
349 (beginning-of-line) | |
350 (if mailp | |
821
cee415e2dae8
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
811
diff
changeset
|
351 (while (or (looking-at "[ \t]*[^ \t\n]*:") (looking-at "[ \t]*$")) |
4992
8a3293abe1ba
(fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents:
4413
diff
changeset
|
352 (if (looking-at "[ \t]*[^ \t\n]*:") |
8a3293abe1ba
(fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents:
4413
diff
changeset
|
353 (search-forward "\n\n" nil 'move) |
8a3293abe1ba
(fill-individual-paragraphs): Fix skipping headers of yanked message.
Richard M. Stallman <rms@gnu.org>
parents:
4413
diff
changeset
|
354 (forward-line 1)))) |
474 | 355 (narrow-to-region (point) max) |
356 ;; Loop over paragraphs. | |
357 (while (progn (skip-chars-forward " \t\n") (not (eobp))) | |
358 (beginning-of-line) | |
359 (let ((start (point)) | |
360 fill-prefix fill-prefix-regexp) | |
361 ;; Find end of paragraph, and compute the smallest fill-prefix | |
362 ;; that fits all the lines in this paragraph. | |
363 (while (progn | |
364 ;; Update the fill-prefix on the first line | |
365 ;; and whenever the prefix good so far is too long. | |
366 (if (not (and fill-prefix | |
367 (looking-at fill-prefix-regexp))) | |
368 (setq fill-prefix | |
369 (buffer-substring (point) | |
370 (save-excursion (skip-chars-forward " \t") (point))) | |
371 fill-prefix-regexp | |
372 (regexp-quote fill-prefix))) | |
373 (forward-line 1) | |
374 ;; Now stop the loop if end of paragraph. | |
375 (and (not (eobp)) | |
638 | 376 (if fill-individual-varying-indent |
377 ;; If this line is a separator line, with or | |
378 ;; without prefix, end the paragraph. | |
379 (and | |
474 | 380 (not (looking-at paragraph-separate)) |
381 (save-excursion | |
382 (not (and (looking-at fill-prefix-regexp) | |
383 (progn (forward-char (length fill-prefix)) | |
638 | 384 (looking-at paragraph-separate)))))) |
385 ;; If this line has more or less indent | |
386 ;; than the fill prefix wants, end the paragraph. | |
387 (and (looking-at fill-prefix-regexp) | |
388 (save-excursion | |
389 (not (progn (forward-char (length fill-prefix)) | |
390 (or (looking-at paragraph-separate) | |
391 (looking-at paragraph-start)))))))))) | |
474 | 392 ;; Fill this paragraph, but don't add a newline at the end. |
393 (let ((had-newline (bolp))) | |
394 (fill-region-as-paragraph start (point) justifyp) | |
638 | 395 (or had-newline (delete-char -1)))))))) |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
396 |
732 | 397 ;;; fill.el ends here |