annotate lisp/textmodes/fill.el @ 424:a9b60e014edd

*** empty log message ***
author Jim Blandy <jimb@redhat.com>
date Mon, 04 Nov 1991 06:29:57 +0000
parents d492f16a8743
children c3bbd755b7da
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.
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
23 Filling expects lines to start with the fill prefix and
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
24 reinserts the fill prefix in each resulting line."
75
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
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
35 (defconst adaptive-fill-mode t
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
36 "*Non-nil means determine a paragraph's fill prefix from its text.")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
37
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
38 (defconst adaptive-fill-regexp "[ \t]*\\([>*] +\\)?"
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
39 "*Regexp to match text at start of line that constitutes indentation.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
40 If Adaptive Fill mode is enabled, whatever text matches this pattern
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
41 on the second line of a paragraph is used as the standard indentation
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
42 for the paragraph.")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
43
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
44 (defun fill-region-as-paragraph (from to &optional justify-flag)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
45 "Fill region as one paragraph: break lines to fit fill-column.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
46 Prefix arg means justify too.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
47 From program, pass args FROM, TO and JUSTIFY-FLAG."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
48 (interactive "r\nP")
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
49 ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
50 (let ((fill-prefix fill-prefix))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
51 ;; Figure out how this paragraph is indented, if desired.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
52 (if adaptive-fill-mode
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
53 (save-excursion
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
54 (goto-char (min from to))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
55 (if (eolp) (forward-line 1))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
56 (forward-line 1)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
57 (if (< (point) (max from to))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
58 (let ((start (point)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
59 (re-search-forward adaptive-fill-regexp)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
60 (setq fill-prefix (buffer-substring start (point))))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
61 (goto-char (min from to))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
62 (if (eolp) (forward-line 1))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
63 ;; If paragraph has only one line, don't assume
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
64 ;; that additional lines would have the same starting
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
65 ;; decoration. Instead, assume they would have white space
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
66 ;; reaching to the same column.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
67 (re-search-forward adaptive-fill-regexp)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
68 (setq fill-prefix (make-string (current-column) ?\ )))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
69
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
70 (save-restriction
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
71 (narrow-to-region from to)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
72 (goto-char (point-min))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
73 (skip-chars-forward "\n")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
74 (narrow-to-region (point) (point-max))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
75 (setq from (point))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
76 (goto-char (point-max))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
77 (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
78 (regexp-quote fill-prefix))))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
79 ;; Delete the fill prefix from every line except the first.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
80 ;; The first line may not even have a fill prefix.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
81 (and fpre
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
82 (progn
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
83 (if (>= (length fill-prefix) fill-column)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
84 (error "fill-prefix too long for specified width"))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
85 (goto-char (point-min))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
86 (forward-line 1)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
87 (while (not (eobp))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
88 (if (looking-at fpre)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
89 (delete-region (point) (match-end 0)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
90 (forward-line 1))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
91 (goto-char (point-min))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
92 (and (looking-at fpre) (forward-char (length fill-prefix)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
93 (setq from (point)))))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
94 ;; from is now before the text to fill,
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
95 ;; but after any fill prefix on the first line.
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
96
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
97 ;; Make sure sentences ending at end of line get an extra space.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
98 ;; loses on split abbrevs ("Mr.\nSmith")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
99 (goto-char from)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
100 (while (re-search-forward "[.?!][])\"']*$" nil t)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
101 (insert ? ))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
102
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
103 ;; Then change all newlines to spaces.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
104 (subst-char-in-region from (point-max) ?\n ?\ )
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
105
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
106 ;; Flush excess spaces, except in the paragraph indentation.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
107 (goto-char from)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
108 (skip-chars-forward " \t")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
109 ;; nuke tabs while we're at it; they get screwed up in a fill
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
110 ;; this is quick, but loses when a sole tab follows the end of a sentence.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
111 ;; actually, it is difficult to tell that from "Mr.\tSmith".
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
112 ;; blame the typist.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
113 (subst-char-in-region (point) (point-max) ?\t ?\ )
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
114 (while (re-search-forward " *" nil t)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
115 (delete-region
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
116 (+ (match-beginning 0)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
117 (if (save-excursion
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
118 (skip-chars-backward " ])\"'")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
119 (memq (preceding-char) '(?. ?? ?!)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
120 2 1))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
121 (match-end 0)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
122 (goto-char (point-max))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
123 (delete-horizontal-space)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
124 (insert " ")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
125 (goto-char (point-min))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
126
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
127 (let ((prefixcol 0))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
128 (while (not (eobp))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
129 (move-to-column (1+ fill-column))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
130 (if (eobp)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
131 nil
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
132 (skip-chars-backward "^ \n")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
133 (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
134 (skip-chars-forward "^ \n")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
135 (forward-char -1)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
136 ;; Inserting the newline first prevents losing track of point.
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
137 (skip-chars-backward " ")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
138 (insert ?\n)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
139 (delete-horizontal-space)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
140 (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
141 (progn
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
142 (insert fill-prefix)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
143 (setq prefixcol (current-column))))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
144 (and justify-flag (not (eobp))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
145 (progn
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
146 (forward-line -1)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
147 (justify-current-line)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
148 (forward-line 1))))))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
149
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
150 (defun fill-paragraph (arg)
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
151 "Fill paragraph at or after point. Prefix arg means justify as well."
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
152 (interactive "P")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
153 (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
154 (forward-paragraph)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
155 (or (bolp) (newline 1))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
156 (let ((end (point)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
157 (backward-paragraph)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
158 (fill-region-as-paragraph (point) end arg))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
159
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
160 (defun fill-region (from to &optional justify-flag)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
161 "Fill each of the paragraphs in the region.
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
162 Prefix arg (non-nil third arg, if called from program) means justify as well."
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
163 (interactive "r\nP")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
164 (save-restriction
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
165 (narrow-to-region from to)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
166 (goto-char (point-min))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
167 (while (not (eobp))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
168 (let ((initial (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
169 (end (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
170 (forward-paragraph 1) (point))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
171 (forward-paragraph -1)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
172 (if (>= (point) initial)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
173 (fill-region-as-paragraph (point) end justify-flag)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
174 (goto-char end))))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
175
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
176 (defun justify-current-line ()
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
177 "Add spaces to line point is in, so it ends at `fill-column'."
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
178 (interactive)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
179 (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
180 (save-restriction
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
181 (let (ncols beg indent)
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
182 (beginning-of-line)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
183 (forward-char (length fill-prefix))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
184 (skip-chars-forward " \t")
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
185 (setq indent (current-column))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
186 (setq beg (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
187 (end-of-line)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
188 (narrow-to-region beg (point))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
189 (goto-char beg)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
190 (while (re-search-forward " *" nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
191 (delete-region
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
192 (+ (match-beginning 0)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
193 (if (save-excursion
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
194 (skip-chars-backward " ])\"'")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
195 (memq (preceding-char) '(?. ?? ?!)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
196 2 1))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
197 (match-end 0)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
198 (goto-char beg)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
199 (while (re-search-forward "[.?!][])""']*\n" nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
200 (forward-char -1)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
201 (insert ? ))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
202 (goto-char (point-max))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
203 ;; Note that the buffer bounds start after the indentation,
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
204 ;; so the columns counted by INDENT don't appear in (current-column).
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
205 (setq ncols (- fill-column (current-column) indent))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
206 (if (search-backward " " nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
207 (while (> ncols 0)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
208 (let ((nmove (+ 3 (random 3))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
209 (while (> nmove 0)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
210 (or (search-backward " " nil t)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
211 (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
212 (goto-char (point-max))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
213 (search-backward " ")))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
214 (skip-chars-backward " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
215 (setq nmove (1- nmove))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
216 (insert " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
217 (skip-chars-backward " ")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
218 (setq ncols (1- ncols))))))))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
219
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
220 (defun fill-individual-paragraphs (min max &optional justifyp mailp)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
221 "Fill each paragraph in region according to its individual fill prefix.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
222 Calling from a program, pass range to fill as first two arguments.
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
223 Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
224 JUSTIFY-FLAG to justify paragraphs (prefix arg),
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
225 MAIL-FLAG for a mail message, i. e. don't fill header lines."
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
226 (interactive "r\nP")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
227 (let (fill-prefix)
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
228 (save-restriction
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
229 (save-excursion
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
230 (goto-char min)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
231 (if mailp
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
232 (while (looking-at "[^ \t\n]*:")
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
233 (forward-line 1)))
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
234 (narrow-to-region (point) max)
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
235 (while (progn
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
236 (skip-chars-forward " \t\n")
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
237 (not (eobp)))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
238 (setq fill-prefix
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
239 (buffer-substring (point) (progn (beginning-of-line) (point))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
240 (let ((fin (save-excursion (forward-paragraph) (point)))
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
241 (start (point)))
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
242 (fill-region-as-paragraph (point) fin justifyp)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
243 (goto-char start)
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
244 (forward-paragraph)))))))
75
a13ef7914930 Initial revision
root <root>
parents:
diff changeset
245
218
d492f16a8743 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 75
diff changeset
246