annotate lisp/textmodes/fill.el @ 75:a13ef7914930

Initial revision
author root <root>
date Fri, 27 Jul 1990 04:20:16 +0000
parents
children d492f16a8743
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
1 ;; Fill commands for Emacs
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
2 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
3
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
4 ;; This file is part of GNU Emacs.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
5
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
7 ;; it under the terms of the GNU General Public License as published by
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
8 ;; the Free Software Foundation; either version 1, or (at your option)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
9 ;; any later version.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
10
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
11 ;; GNU Emacs is distributed in the hope that it will be useful,
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
14 ;; GNU General Public License for more details.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
15
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
16 ;; You should have received a copy of the GNU General Public License
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
19
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
20
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
21 (defun set-fill-prefix ()
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
22 "Set the fill-prefix to the current line up to point.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
23 Filling expects lines to start with the fill prefix
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
24 and reinserts the fill prefix in each resulting line."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
25 (interactive)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
26 (setq fill-prefix (buffer-substring
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
27 (save-excursion (beginning-of-line) (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
28 (point)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
29 (if (equal fill-prefix "")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
30 (setq fill-prefix nil))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
31 (if fill-prefix
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
32 (message "fill-prefix: \"%s\"" fill-prefix)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
33 (message "fill-prefix cancelled")))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
34
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
35 (defun fill-region-as-paragraph (from to &optional justify-flag)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
36 "Fill region as one paragraph: break lines to fit fill-column.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
37 Prefix arg means justify too.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
38 From program, pass args FROM, TO and JUSTIFY-FLAG."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
39 (interactive "r\nP")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
40 (save-restriction
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
41 (narrow-to-region from to)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
42 (goto-char (point-min))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
43 (skip-chars-forward "\n")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
44 (narrow-to-region (point) (point-max))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
45 (setq from (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
46 (goto-char (point-max))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
47 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
48 (regexp-quote fill-prefix))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
49 ;; Delete the fill prefix from every line except the first.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
50 ;; The first line may not even have a fill prefix.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
51 (and fpre
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
52 (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
53 (if (>= (length fill-prefix) fill-column)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
54 (error "fill-prefix too long for specified width"))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
55 (goto-char (point-min))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
56 (forward-line 1)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
57 (while (not (eobp))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
58 (if (looking-at fpre)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
59 (delete-region (point) (match-end 0)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
60 (forward-line 1))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
61 (goto-char (point-min))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
62 (and (looking-at fpre) (forward-char (length fill-prefix)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
63 (setq from (point)))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
64 ;; from is now before the text to fill,
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
65 ;; but after any fill prefix on the first line.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
66
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
67 ;; Make sure sentences ending at end of line get an extra space.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
68 ;; loses on split abbrevs ("Mr.\nSmith")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
69 (goto-char from)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
70 (while (re-search-forward "[.?!][])\"']*$" nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
71 (insert ? ))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
72
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
73 ;; Then change all newlines to spaces.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
74 (subst-char-in-region from (point-max) ?\n ?\ )
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
76 ;; Flush excess spaces, except in the paragraph indentation.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
77 (goto-char from)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
78 (skip-chars-forward " \t")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
79 ;; nuke tabs while we're at it; they get screwed up in a fill
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
80 ;; this is quick, but loses when a sole tab follows the end of a sentence.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
81 ;; actually, it is difficult to tell that from "Mr.\tSmith".
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
82 ;; blame the typist.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
83 (subst-char-in-region (point) (point-max) ?\t ?\ )
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
84 (while (re-search-forward " *" nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
85 (delete-region
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
86 (+ (match-beginning 0)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
87 (if (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
88 (skip-chars-backward " ])\"'")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
89 (memq (preceding-char) '(?. ?? ?!)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
90 2 1))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
91 (match-end 0)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
92 (goto-char (point-max))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
93 (delete-horizontal-space)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
94 (insert " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
95 (goto-char (point-min))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
96
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
97 (let ((prefixcol 0))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
98 (while (not (eobp))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
99 (move-to-column (1+ fill-column))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
100 (if (eobp)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
101 nil
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
102 (skip-chars-backward "^ \n")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
103 (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
104 (skip-chars-forward "^ \n")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
105 (forward-char -1)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
106 ;; Inserting the newline first prevents losing track of point.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
107 (skip-chars-backward " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
108 (insert ?\n)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
109 (delete-horizontal-space)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
110 (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
111 (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
112 (insert fill-prefix)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
113 (setq prefixcol (current-column))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
114 (and justify-flag (not (eobp))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
115 (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
116 (forward-line -1)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
117 (justify-current-line)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
118 (forward-line 1)))))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
119
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
120 (defun fill-paragraph (arg)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
121 "Fill paragraph at or after point.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
122 Prefix arg means justify as well."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
123 (interactive "P")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
124 (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
125 (forward-paragraph)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
126 (or (bolp) (newline 1))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
127 (let ((end (point)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
128 (backward-paragraph)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
129 (fill-region-as-paragraph (point) end arg))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
130
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
131 (defun fill-region (from to &optional justify-flag)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
132 "Fill each of the paragraphs in the region.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
133 Prefix arg (non-nil third arg, if called from program)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
134 means justify as well."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
135 (interactive "r\nP")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
136 (save-restriction
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
137 (narrow-to-region from to)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
138 (goto-char (point-min))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
139 (while (not (eobp))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
140 (let ((initial (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
141 (end (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
142 (forward-paragraph 1) (point))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
143 (forward-paragraph -1)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
144 (if (>= (point) initial)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
145 (fill-region-as-paragraph (point) end justify-flag)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
146 (goto-char end))))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
147
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
148 (defun justify-current-line ()
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
149 "Add spaces to line point is in, so it ends at fill-column."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
150 (interactive)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
151 (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
152 (save-restriction
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
153 (let (ncols beg)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
154 (beginning-of-line)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
155 (forward-char (length fill-prefix))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
156 (skip-chars-forward " \t")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
157 (setq beg (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
158 (end-of-line)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
159 (narrow-to-region beg (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
160 (goto-char beg)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
161 (while (re-search-forward " *" nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
162 (delete-region
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
163 (+ (match-beginning 0)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
164 (if (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
165 (skip-chars-backward " ])\"'")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
166 (memq (preceding-char) '(?. ?? ?!)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
167 2 1))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
168 (match-end 0)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
169 (goto-char beg)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
170 (while (re-search-forward "[.?!][])""']*\n" nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
171 (forward-char -1)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
172 (insert ? ))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
173 (goto-char (point-max))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
174 (setq ncols (- fill-column (current-column)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
175 (if (search-backward " " nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
176 (while (> ncols 0)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
177 (let ((nmove (+ 3 (random 3))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
178 (while (> nmove 0)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
179 (or (search-backward " " nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
180 (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
181 (goto-char (point-max))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
182 (search-backward " ")))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
183 (skip-chars-backward " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
184 (setq nmove (1- nmove))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
185 (insert " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
186 (skip-chars-backward " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
187 (setq ncols (1- ncols))))))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
188
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
189 (defun fill-individual-paragraphs (min max &optional justifyp mailp)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
190 "Fill each paragraph in region according to its individual fill prefix.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
191 Calling from a program, pass range to fill as first two arguments.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
192 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
193 JUSTIFY-FLAG to justify paragraphs (prefix arg),
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
194 MAIL-FLAG for a mail message, i. e. don't fill header lines."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
195 (interactive "r\nP")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
196 (let (fill-prefix)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
197 (save-restriction
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
198 (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
199 (narrow-to-region min max)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
200 (goto-char (point-min))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
201 (while (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
202 (skip-chars-forward " \t\n")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
203 (not (eobp)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
204 (setq fill-prefix (buffer-substring (point) (progn (beginning-of-line) (point))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
205 (let ((fin (save-excursion (forward-paragraph) (point)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
206 (start (point)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
207 (if mailp
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
208 (while (re-search-forward "^[ \t]*[^ \t\n]*:" fin t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
209 (forward-line 1)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
210 (cond ((= start (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
211 (fill-region-as-paragraph (point) fin justifyp)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
212 (goto-char fin)))))))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
213