Update FSF's address.
[bpt/emacs.git] / lisp / textmodes / paragraphs.el
1 ;;; paragraphs.el --- paragraph and sentence parsing.
2
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: wp
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This package provides the paragraph-oriented commands documented in the
28 ;; Emacs manual.
29
30 ;;; Code:
31
32 (defvar use-hard-newlines nil
33 "Non-nil means to distinguish hard and soft newlines.
34 When this is non-nil, the functions `newline' and `open-line' add the
35 text-property `hard' to newlines that they insert. Also, a line is
36 only considered as a candidate to match `paragraph-start' or
37 `paragraph-separate' if it follows a hard newline. Newlines not
38 marked hard are called \"soft\", and are always internal to
39 paragraphs. The fill functions always insert soft newlines.
40
41 Each buffer has its own value of this variable.")
42 (make-variable-buffer-local 'use-hard-newlines)
43
44 (defconst paragraph-start "[ \t\n\f]" "\
45 *Regexp for beginning of a line that starts OR separates paragraphs.
46 This regexp should match lines that separate paragraphs
47 and should also match lines that start a paragraph
48 \(and are part of that paragraph).
49
50 This is matched against the text at the left margin, which is not necessarily
51 the beginning of the line, so it should never use \"^\" as an anchor. This
52 ensures that the paragraph functions will work equally well within a region
53 of text indented by a margin setting.
54
55 The variable `paragraph-separate' specifies how to distinguish
56 lines that start paragraphs from lines that separate them.
57
58 If the variable `use-hard-newlines' is nonnil, then only lines following a
59 hard newline are considered to match.")
60
61 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
62 ;; It is assumed that paragraph-separate is distinctive enough to be believed
63 ;; whenever it occurs, while it is reasonable to set paragraph-start to
64 ;; something very minimal, even including "." (which makes every hard newline
65 ;; start a new paragraph).
66
67 (defconst paragraph-separate "[ \t\f]*$" "\
68 *Regexp for beginning of a line that separates paragraphs.
69 If you change this, you may have to change paragraph-start also.
70
71 This is matched against the text at the left margin, which is not necessarily
72 the beginning of the line, so it should not use \"^\" as an anchor. This
73 ensures that the paragraph functions will work equally within a region of
74 text indented by a margin setting.")
75
76 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
77 *Regexp describing the end of a sentence.
78 All paragraph boundaries also end sentences, regardless.
79
80 In order to be recognized as the end of a sentence, the ending period,
81 question mark, or exclamation point must be followed by two spaces,
82 unless it's inside some sort of quotes or parenthesis.")
83
84 (defconst page-delimiter "^\014" "\
85 *Regexp describing line-beginnings that separate pages.")
86
87 (defvar paragraph-ignore-fill-prefix nil "\
88 Non-nil means the paragraph commands are not affected by `fill-prefix'.
89 This is desirable in modes where blank lines are the paragraph delimiters.")
90
91 (defun forward-paragraph (&optional arg)
92 "Move forward to end of paragraph.
93 With arg N, do it N times; negative arg -N means move backward N paragraphs.
94
95 A line which `paragraph-start' matches either separates paragraphs
96 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
97 A paragraph end is the beginning of a line which is not part of the paragraph
98 to which the end of the previous line belongs, or the end of the buffer."
99 (interactive "p")
100 (or arg (setq arg 1))
101 (let* ((fill-prefix-regexp
102 (and fill-prefix (not (equal fill-prefix ""))
103 (not paragraph-ignore-fill-prefix)
104 (regexp-quote fill-prefix)))
105 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
106 ;; These regexps shouldn't be anchored, because we look for them
107 ;; starting at the left-margin. This allows paragraph commands to
108 ;; work normally with indented text.
109 ;; This hack will not find problem cases like "whatever\\|^something".
110 (paragraph-start (if (and (not (equal "" paragraph-start))
111 (equal ?^ (aref paragraph-start 0)))
112 (substring paragraph-start 1)
113 paragraph-start))
114 (paragraph-separate (if (and (not (equal "" paragraph-start))
115 (equal ?^ (aref paragraph-separate 0)))
116 (substring paragraph-separate 1)
117 paragraph-separate))
118 (paragraph-separate
119 (if fill-prefix-regexp
120 (concat paragraph-separate "\\|"
121 fill-prefix-regexp "[ \t]*$")
122 paragraph-separate))
123 ;; This is used for searching.
124 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
125 start)
126 (while (and (< arg 0) (not (bobp)))
127 (if (and (not (looking-at paragraph-separate))
128 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
129 (looking-at paragraph-separate))
130 nil
131 (setq start (point))
132 ;; Move back over paragraph-separating lines.
133 (forward-char -1) (beginning-of-line)
134 (while (and (not (bobp))
135 (progn (move-to-left-margin)
136 (looking-at paragraph-separate)))
137 (forward-line -1))
138 (if (bobp)
139 nil
140 ;; Go to end of the previous (non-separating) line.
141 (end-of-line)
142 ;; Search back for line that starts or separates paragraphs.
143 (if (if fill-prefix-regexp
144 ;; There is a fill prefix; it overrides paragraph-start.
145 (let (multiple-lines)
146 (while (and (progn (beginning-of-line) (not (bobp)))
147 (progn (move-to-left-margin)
148 (not (looking-at paragraph-separate)))
149 (looking-at fill-prefix-regexp))
150 (if (not (= (point) start))
151 (setq multiple-lines t))
152 (forward-line -1))
153 (move-to-left-margin)
154 ;; Don't move back over a line before the paragraph
155 ;; which doesn't start with fill-prefix
156 ;; unless that is the only line we've moved over.
157 (and (not (looking-at fill-prefix-regexp))
158 multiple-lines
159 (forward-line 1))
160 (not (bobp)))
161 (while (and (re-search-backward sp-paragraph-start nil 1)
162 ;; Found a candidate, but need to check if it is a
163 ;; REAL paragraph-start.
164 (not (bobp))
165 (progn (setq start (point))
166 (move-to-left-margin)
167 (not (looking-at paragraph-separate)))
168 (or (not (looking-at paragraph-start))
169 (and use-hard-newlines
170 (not (get-text-property (1- start)
171 'hard)))))
172 (goto-char start))
173 (> (point) (point-min)))
174 ;; Found one.
175 (progn
176 ;; Move forward over paragraph separators.
177 ;; We know this cannot reach the place we started
178 ;; because we know we moved back over a non-separator.
179 (while (and (not (eobp))
180 (progn (move-to-left-margin)
181 (looking-at paragraph-separate)))
182 (forward-line 1))
183 ;; If line before paragraph is just margin, back up to there.
184 (end-of-line 0)
185 (if (> (current-column) (current-left-margin))
186 (forward-char 1)
187 (skip-chars-backward " \t")
188 (if (not (bolp))
189 (forward-line 1))))
190 ;; No starter or separator line => use buffer beg.
191 (goto-char (point-min)))))
192 (setq arg (1+ arg)))
193 (while (and (> arg 0) (not (eobp)))
194 (while (prog1 (and (not (eobp))
195 (progn (move-to-left-margin) (not (eobp)))
196 (looking-at paragraph-separate))
197 (forward-line 1)))
198 (if fill-prefix-regexp
199 ;; There is a fill prefix; it overrides paragraph-start.
200 (while (and (not (eobp))
201 (progn (move-to-left-margin) (not (eobp)))
202 (not (looking-at paragraph-separate))
203 (looking-at fill-prefix-regexp))
204 (forward-line 1))
205 (while (and (re-search-forward sp-paragraph-start nil 1)
206 (progn (setq start (match-beginning 0))
207 (goto-char start)
208 (not (eobp)))
209 (progn (move-to-left-margin)
210 (not (looking-at paragraph-separate)))
211 (or (not (looking-at paragraph-start))
212 (and use-hard-newlines
213 (not (get-text-property (1- start) 'hard)))))
214 (forward-char 1))
215 (if (< (point) (point-max))
216 (goto-char start)))
217 (setq arg (1- arg)))))
218
219 (defun backward-paragraph (&optional arg)
220 "Move backward to start of paragraph.
221 With arg N, do it N times; negative arg -N means move forward N paragraphs.
222
223 A paragraph start is the beginning of a line which is a
224 `first-line-of-paragraph' or which is ordinary text and follows a
225 paragraph-separating line; except: if the first real line of a
226 paragraph is preceded by a blank line, the paragraph starts at that
227 blank line.
228
229 See `forward-paragraph' for more information."
230 (interactive "p")
231 (or arg (setq arg 1))
232 (forward-paragraph (- arg)))
233
234 (defun mark-paragraph ()
235 "Put point at beginning of this paragraph, mark at end.
236 The paragraph marked is the one that contains point or follows point."
237 (interactive)
238 (forward-paragraph 1)
239 (push-mark nil t t)
240 (backward-paragraph 1))
241
242 (defun kill-paragraph (arg)
243 "Kill forward to end of paragraph.
244 With arg N, kill forward to Nth end of paragraph;
245 negative arg -N means kill backward to Nth start of paragraph."
246 (interactive "p")
247 (kill-region (point) (progn (forward-paragraph arg) (point))))
248
249 (defun backward-kill-paragraph (arg)
250 "Kill back to start of paragraph.
251 With arg N, kill back to Nth start of paragraph;
252 negative arg -N means kill forward to Nth end of paragraph."
253 (interactive "p")
254 (kill-region (point) (progn (backward-paragraph arg) (point))))
255
256 (defun transpose-paragraphs (arg)
257 "Interchange this (or next) paragraph with previous one."
258 (interactive "*p")
259 (transpose-subr 'forward-paragraph arg))
260
261 (defun start-of-paragraph-text ()
262 (let ((opoint (point)) npoint)
263 (forward-paragraph -1)
264 (setq npoint (point))
265 (skip-chars-forward " \t\n")
266 ;; If the range of blank lines found spans the original start point,
267 ;; try again from the beginning of it.
268 ;; Must be careful to avoid infinite loop
269 ;; when following a single return at start of buffer.
270 (if (and (>= (point) opoint) (< npoint opoint))
271 (progn
272 (goto-char npoint)
273 (if (> npoint (point-min))
274 (start-of-paragraph-text))))))
275
276 (defun end-of-paragraph-text ()
277 (let ((opoint (point)))
278 (forward-paragraph 1)
279 (if (eq (preceding-char) ?\n) (forward-char -1))
280 (if (<= (point) opoint)
281 (progn
282 (forward-char 1)
283 (if (< (point) (point-max))
284 (end-of-paragraph-text))))))
285
286 (defun forward-sentence (&optional arg)
287 "Move forward to next `sentence-end'. With argument, repeat.
288 With negative argument, move backward repeatedly to `sentence-beginning'.
289
290 The variable `sentence-end' is a regular expression that matches ends of
291 sentences. Also, every paragraph boundary terminates sentences as well."
292 (interactive "p")
293 (or arg (setq arg 1))
294 (while (< arg 0)
295 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
296 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
297 (goto-char (1- (match-end 0)))
298 (goto-char par-beg)))
299 (setq arg (1+ arg)))
300 (while (> arg 0)
301 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
302 (if (re-search-forward sentence-end par-end t)
303 (skip-chars-backward " \t\n")
304 (goto-char par-end)))
305 (setq arg (1- arg))))
306
307 (defun backward-sentence (&optional arg)
308 "Move backward to start of sentence. With arg, do it arg times.
309 See `forward-sentence' for more information."
310 (interactive "p")
311 (or arg (setq arg 1))
312 (forward-sentence (- arg)))
313
314 (defun kill-sentence (&optional arg)
315 "Kill from point to end of sentence.
316 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
317 (interactive "p")
318 (kill-region (point) (progn (forward-sentence arg) (point))))
319
320 (defun backward-kill-sentence (&optional arg)
321 "Kill back from point to start of sentence.
322 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
323 (interactive "p")
324 (kill-region (point) (progn (backward-sentence arg) (point))))
325
326 (defun mark-end-of-sentence (arg)
327 "Put mark at end of sentence. Arg works as in `forward-sentence'."
328 (interactive "p")
329 (push-mark
330 (save-excursion
331 (forward-sentence arg)
332 (point))
333 nil t))
334
335 (defun transpose-sentences (arg)
336 "Interchange this (next) and previous sentence."
337 (interactive "*p")
338 (transpose-subr 'forward-sentence arg))
339
340 ;;; paragraphs.el ends here