d7526a192b57f639df22580fead3da947628d92b
1 ;; Fill commands for Emacs
2 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 (defun set-fill-prefix ()
22 "Set the fill-prefix to the current line up to point.
23 Filling expects lines to start with the fill prefix and
24 reinserts the fill prefix in each resulting line."
26 (setq fill-prefix
(buffer-substring
27 (save-excursion (beginning-of-line) (point))
29 (if (equal fill-prefix
"")
30 (setq fill-prefix nil
))
32 (message "fill-prefix: \"%s\"" fill-prefix
)
33 (message "fill-prefix cancelled")))
35 (defconst adaptive-fill-mode t
36 "*Non-nil means determine a paragraph's fill prefix from its text.")
38 (defconst adaptive-fill-regexp
"[ \t]*\\([>*] +\\)?"
39 "*Regexp to match text at start of line that constitutes indentation.
40 If Adaptive Fill mode is enabled, whatever text matches this pattern
41 on the second line of a paragraph is used as the standard indentation
44 (defun fill-region-as-paragraph (from to
&optional justify-flag
)
45 "Fill region as one paragraph: break lines to fit fill-column.
46 Prefix arg means justify too.
47 From program, pass args FROM, TO and JUSTIFY-FLAG."
49 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
50 (let ((fill-prefix fill-prefix
))
51 ;; Figure out how this paragraph is indented, if desired.
52 (if adaptive-fill-mode
54 (goto-char (min from to
))
55 (if (eolp) (forward-line 1))
57 (if (< (point) (max from to
))
58 (let ((start (point)))
59 (re-search-forward adaptive-fill-regexp
)
60 (setq fill-prefix
(buffer-substring start
(point))))
61 (goto-char (min from to
))
62 (if (eolp) (forward-line 1))
63 ;; If paragraph has only one line, don't assume
64 ;; that additional lines would have the same starting
65 ;; decoration. Instead, assume they would have white space
66 ;; reaching to the same column.
67 (re-search-forward adaptive-fill-regexp
)
68 (setq fill-prefix
(make-string (current-column) ?\
)))))
71 (narrow-to-region from to
)
72 (goto-char (point-min))
73 (skip-chars-forward "\n")
74 (narrow-to-region (point) (point-max))
76 (goto-char (point-max))
77 (let ((fpre (and fill-prefix
(not (equal fill-prefix
""))
78 (regexp-quote fill-prefix
))))
79 ;; Delete the fill prefix from every line except the first.
80 ;; The first line may not even have a fill prefix.
83 (if (>= (length fill-prefix
) fill-column
)
84 (error "fill-prefix too long for specified width"))
85 (goto-char (point-min))
89 (delete-region (point) (match-end 0)))
91 (goto-char (point-min))
92 (and (looking-at fpre
) (forward-char (length fill-prefix
)))
93 (setq from
(point)))))
94 ;; from is now before the text to fill,
95 ;; but after any fill prefix on the first line.
97 ;; Make sure sentences ending at end of line get an extra space.
98 ;; loses on split abbrevs ("Mr.\nSmith")
100 (while (re-search-forward "[.?!][])\"']*$" nil t
)
103 ;; Then change all newlines to spaces.
104 (subst-char-in-region from
(point-max) ?
\n ?\
)
106 ;; Flush excess spaces, except in the paragraph indentation.
108 (skip-chars-forward " \t")
109 ;; nuke tabs while we're at it; they get screwed up in a fill
110 ;; this is quick, but loses when a sole tab follows the end of a sentence.
111 ;; actually, it is difficult to tell that from "Mr.\tSmith".
113 (subst-char-in-region (point) (point-max) ?
\t ?\
)
114 (while (re-search-forward " *" nil t
)
116 (+ (match-beginning 0)
118 (skip-chars-backward " ])\"'")
119 (memq (preceding-char) '(?. ?? ?
!)))
122 (goto-char (point-max))
123 (delete-horizontal-space)
125 (goto-char (point-min))
129 (move-to-column (1+ fill-column
))
132 (skip-chars-backward "^ \n")
133 (if (if (zerop prefixcol
) (bolp) (>= prefixcol
(current-column)))
134 (skip-chars-forward "^ \n")
136 ;; Inserting the newline first prevents losing track of point.
137 (skip-chars-backward " ")
139 (delete-horizontal-space)
140 (and (not (eobp)) fill-prefix
(not (equal fill-prefix
""))
143 (setq prefixcol
(current-column))))
144 (and justify-flag
(not (eobp))
147 (justify-current-line)
148 (forward-line 1))))))))
150 (defun fill-paragraph (arg)
151 "Fill paragraph at or after point. Prefix arg means justify as well."
155 (or (bolp) (newline 1))
158 (fill-region-as-paragraph (point) end arg
))))
160 (defun fill-region (from to
&optional justify-flag
)
161 "Fill each of the paragraphs in the region.
162 Prefix arg (non-nil third arg, if called from program) means justify as well."
165 (narrow-to-region from to
)
166 (goto-char (point-min))
168 (let ((initial (point))
170 (forward-paragraph 1) (point))))
171 (forward-paragraph -
1)
172 (if (>= (point) initial
)
173 (fill-region-as-paragraph (point) end justify-flag
)
176 (defun justify-current-line ()
177 "Add spaces to line point is in, so it ends at `fill-column'."
181 (let (ncols beg indent
)
183 (forward-char (length fill-prefix
))
184 (skip-chars-forward " \t")
185 (setq indent
(current-column))
188 (narrow-to-region beg
(point))
190 (while (re-search-forward " *" nil t
)
192 (+ (match-beginning 0)
194 (skip-chars-backward " ])\"'")
195 (memq (preceding-char) '(?. ?? ?
!)))
199 (while (re-search-forward "[.?!][])""']*\n" nil t
)
202 (goto-char (point-max))
203 ;; Note that the buffer bounds start after the indentation,
204 ;; so the columns counted by INDENT don't appear in (current-column).
205 (setq ncols
(- fill-column
(current-column) indent
))
206 (if (search-backward " " nil t
)
208 (let ((nmove (+ 3 (random 3))))
210 (or (search-backward " " nil t
)
212 (goto-char (point-max))
213 (search-backward " ")))
214 (skip-chars-backward " ")
215 (setq nmove
(1- nmove
))))
217 (skip-chars-backward " ")
218 (setq ncols
(1- ncols
))))))))
220 (defun fill-individual-paragraphs (min max
&optional justifyp mailp
)
221 "Fill each paragraph in region according to its individual fill prefix.
222 Calling from a program, pass range to fill as first two arguments.
223 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
224 JUSTIFY-FLAG to justify paragraphs (prefix arg),
225 MAIL-FLAG for a mail message, i. e. don't fill header lines."
232 (while (looking-at "[^ \t\n]*:")
234 (narrow-to-region (point) max
)
236 (skip-chars-forward " \t\n")
239 (buffer-substring (point) (progn (beginning-of-line) (point))))
240 (let ((fin (save-excursion (forward-paragraph) (point)))
242 (fill-region-as-paragraph (point) fin justifyp
)
244 (forward-paragraph)))))))