Clarify initial discussion.
[bpt/emacs.git] / lisp / textmodes / paragraphs.el
CommitLineData
6594deb0
ER
1;;; paragraphs.el --- paragraph and sentence parsing.
2
3fe35f35 3;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96, 1997, 1999, 2000, 2001
e4550233 4;; Free Software Foundation, Inc.
9750e079 5
4821e2af 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: wp
4821e2af 8
a2535589
JA
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
29add8b9 13;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
a2535589 25
edbd2f74
ER
26;;; Commentary:
27
28;; This package provides the paragraph-oriented commands documented in the
29;; Emacs manual.
30
4821e2af 31;;; Code:
a2535589 32
e4550233
RS
33(defgroup paragraphs nil
34 "Paragraph and sentence parsing."
35 :group 'editing)
36
bde0f7ca
RS
37;; It isn't useful to use defcustom for this variable
38;; because it is always buffer-local.
39(defvar use-hard-newlines nil
55cc5677 40 "Non-nil means to distinguish hard and soft newlines.
a994501e 41See also the documentation for the function `use-hard-newlines'.")
965eb84a
RS
42(make-variable-buffer-local 'use-hard-newlines)
43
44(defun use-hard-newlines (&optional arg insert)
45 "Minor mode to distinguish hard and soft newlines.
46When active, the functions `newline' and `open-line' add the
47text-property `hard' to newlines that they insert, and a line is
55cc5677 48only considered as a candidate to match `paragraph-start' or
965eb84a 49`paragraph-separate' if it follows a hard newline.
55cc5677 50
965eb84a
RS
51Prefix argument says to turn mode on if positive, off if negative.
52When the mode is turned on, if there are newlines in the buffer but no hard
53newlines, ask the user whether to mark as hard any newlines preceeding a
54`paragraph-start' line. From a program, second arg INSERT specifies whether
55to do this; it can be `never' to change nothing, t or `always' to force
56marking, `guess' to try to do the right thing with no questions, nil
57or anything else to ask the user.
58
59Newlines not marked hard are called \"soft\", and are always internal
60to paragraphs. The fill functions insert and delete only soft newlines."
61 (interactive (list current-prefix-arg nil))
62 (if (or (<= (prefix-numeric-value arg) 0)
63 (and use-hard-newlines (null arg)))
64 ;; Turn mode off
65 (setq use-hard-newlines nil)
66 ;; Turn mode on
67 ;; Intuit hard newlines --
68 ;; mark as hard any newlines preceding a paragraph-start line.
69 (if (or (eq insert t) (eq insert 'always)
70 (and (not (eq 'never insert))
71 (not use-hard-newlines)
72 (not (text-property-any (point-min) (point-max) 'hard t))
73 (save-excursion
74 (goto-char (point-min))
75 (search-forward "\n" nil t))
76 (or (eq insert 'guess)
77 (y-or-n-p "Make newlines between paragraphs hard? "))))
78 (save-excursion
79 (goto-char (point-min))
80 (while (search-forward "\n" nil t)
81 (let ((pos (point)))
82 (move-to-left-margin)
83 (if (looking-at paragraph-start)
84 (progn
85 (set-hard-newline-properties (1- pos) pos)
86 ;; If paragraph-separate, newline after it is hard too.
87 (if (looking-at paragraph-separate)
88 (progn
89 (end-of-line)
90 (if (not (eobp))
91 (set-hard-newline-properties
92 (point) (1+ (point))))))))))))
93 (setq use-hard-newlines t)))
55cc5677 94
e4550233 95(defcustom paragraph-start "[ \t\n\f]" "\
1f2007b3
RS
96*Regexp for beginning of a line that starts OR separates paragraphs.
97This regexp should match lines that separate paragraphs
98and should also match lines that start a paragraph
99\(and are part of that paragraph).
a37669ec 100
55cc5677
BG
101This is matched against the text at the left margin, which is not necessarily
102the beginning of the line, so it should never use \"^\" as an anchor. This
103ensures that the paragraph functions will work equally well within a region
104of text indented by a margin setting.
105
1f2007b3 106The variable `paragraph-separate' specifies how to distinguish
a37669ec
RS
107lines that start paragraphs from lines that separate them.
108
3f5dc0b0 109If the variable `use-hard-newlines' is non-nil, then only lines following a
e4550233
RS
110hard newline are considered to match."
111 :group 'paragraphs
112 :type 'regexp)
6503cec3 113
55cc5677
BG
114;; paragraph-start requires a hard newline, but paragraph-separate does not:
115;; It is assumed that paragraph-separate is distinctive enough to be believed
116;; whenever it occurs, while it is reasonable to set paragraph-start to
117;; something very minimal, even including "." (which makes every hard newline
118;; start a new paragraph).
119
e4550233
RS
120(defcustom paragraph-separate "[ \t\f]*$"
121 "*Regexp for beginning of a line that separates paragraphs.
9d7c4eb5 122If you change this, you may have to change `paragraph-start' also.
a37669ec 123
55cc5677
BG
124This is matched against the text at the left margin, which is not necessarily
125the beginning of the line, so it should not use \"^\" as an anchor. This
126ensures that the paragraph functions will work equally within a region of
e4550233
RS
127text indented by a margin setting."
128 :group 'paragraphs
129 :type 'regexp)
6503cec3 130
e4550233
RS
131(defcustom sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*")
132 "*Regexp describing the end of a sentence.
ac1470eb 133The value includes the whitespace following the sentence.
51534471
JB
134All paragraph boundaries also end sentences, regardless.
135
220dae72
DL
136The default value specifies that in order to be recognized as the end
137of a sentence, the ending period, question mark, or exclamation point
138must be followed by two spaces, unless it's inside some sort of quotes
139or parenthesis.
140
8919155a
DL
141See also the variable `sentence-end-double-space', the variable
142`sentence-end-without-period' and Info node `Sentences'."
e4550233
RS
143 :group 'paragraphs
144 :type 'regexp)
145
146(defcustom page-delimiter "^\014"
147 "*Regexp describing line-beginnings that separate pages."
148 :group 'paragraphs
149 :type 'regexp)
150
151(defcustom paragraph-ignore-fill-prefix nil
152 "*Non-nil means the paragraph commands are not affected by `fill-prefix'.
153This is desirable in modes where blank lines are the paragraph delimiters."
154 :group 'paragraphs
155 :type 'boolean)
77176e73 156
a2535589
JA
157(defun forward-paragraph (&optional arg)
158 "Move forward to end of paragraph.
94d63a23
RS
159With argument ARG, do it ARG times;
160a negative argument ARG = -N means move backward N paragraphs.
a2535589
JA
161
162A line which `paragraph-start' matches either separates paragraphs
163\(if `paragraph-separate' matches it also) or is the first line of a paragraph.
164A paragraph end is the beginning of a line which is not part of the paragraph
165to which the end of the previous line belongs, or the end of the buffer."
166 (interactive "p")
167 (or arg (setq arg 1))
17cca868
GM
168 (let* ((opoint (point))
169 (fill-prefix-regexp
a2535589
JA
170 (and fill-prefix (not (equal fill-prefix ""))
171 (not paragraph-ignore-fill-prefix)
172 (regexp-quote fill-prefix)))
55cc5677
BG
173 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
174 ;; These regexps shouldn't be anchored, because we look for them
175 ;; starting at the left-margin. This allows paragraph commands to
176 ;; work normally with indented text.
177 ;; This hack will not find problem cases like "whatever\\|^something".
178 (paragraph-start (if (and (not (equal "" paragraph-start))
179 (equal ?^ (aref paragraph-start 0)))
180 (substring paragraph-start 1)
181 paragraph-start))
c4ac30da 182 (paragraph-separate (if (and (not (equal "" paragraph-separate))
55cc5677 183 (equal ?^ (aref paragraph-separate 0)))
c4ac30da 184 (substring paragraph-separate 1)
55cc5677 185 paragraph-separate))
a2535589
JA
186 (paragraph-separate
187 (if fill-prefix-regexp
55cc5677 188 (concat paragraph-separate "\\|"
a2535589 189 fill-prefix-regexp "[ \t]*$")
55cc5677
BG
190 paragraph-separate))
191 ;; This is used for searching.
192 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
eeb0f327 193 start found-start)
8a2a4ced 194 (while (and (< arg 0) (not (bobp)))
55cc5677 195 (if (and (not (looking-at paragraph-separate))
a37669ec 196 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
55cc5677 197 (looking-at paragraph-separate))
a2535589 198 nil
2be01738 199 (setq start (point))
8a2a4ced 200 ;; Move back over paragraph-separating lines.
a2535589 201 (forward-char -1) (beginning-of-line)
a37669ec 202 (while (and (not (bobp))
55cc5677
BG
203 (progn (move-to-left-margin)
204 (looking-at paragraph-separate)))
3f5dc0b0 205 (forward-line -1))
8a2a4ced
RS
206 (if (bobp)
207 nil
208 ;; Go to end of the previous (non-separating) line.
209 (end-of-line)
210 ;; Search back for line that starts or separates paragraphs.
211 (if (if fill-prefix-regexp
212 ;; There is a fill prefix; it overrides paragraph-start.
2be01738 213 (let (multiple-lines)
55cc5677
BG
214 (while (and (progn (beginning-of-line) (not (bobp)))
215 (progn (move-to-left-margin)
216 (not (looking-at paragraph-separate)))
217 (looking-at fill-prefix-regexp))
3f5dc0b0
SM
218 (unless (= (point) start)
219 (setq multiple-lines t))
55cc5677 220 (forward-line -1))
2be01738 221 (move-to-left-margin)
b53ce41d
RS
222;;; This deleted code caused a long hanging-indent line
223;;; not to be filled together with the following lines.
224;;; ;; Don't move back over a line before the paragraph
225;;; ;; which doesn't start with fill-prefix
226;;; ;; unless that is the only line we've moved over.
227;;; (and (not (looking-at fill-prefix-regexp))
228;;; multiple-lines
229;;; (forward-line 1))
2be01738 230 (not (bobp)))
55cc5677 231 (while (and (re-search-backward sp-paragraph-start nil 1)
eeb0f327 232 (setq found-start t)
55cc5677
BG
233 ;; Found a candidate, but need to check if it is a
234 ;; REAL paragraph-start.
55cc5677
BG
235 (progn (setq start (point))
236 (move-to-left-margin)
237 (not (looking-at paragraph-separate)))
eeb0f327
RS
238 (not (and (looking-at paragraph-start)
239 (not
240 (and use-hard-newlines
241 (not (bobp))
242 (not (get-text-property (1- start)
243 'hard)))))))
244 (setq found-start nil)
55cc5677 245 (goto-char start))
eeb0f327 246 found-start)
8a2a4ced
RS
247 ;; Found one.
248 (progn
249 ;; Move forward over paragraph separators.
250 ;; We know this cannot reach the place we started
251 ;; because we know we moved back over a non-separator.
55cc5677
BG
252 (while (and (not (eobp))
253 (progn (move-to-left-margin)
254 (looking-at paragraph-separate)))
8a2a4ced 255 (forward-line 1))
55cc5677
BG
256 ;; If line before paragraph is just margin, back up to there.
257 (end-of-line 0)
258 (if (> (current-column) (current-left-margin))
259 (forward-char 1)
260 (skip-chars-backward " \t")
261 (if (not (bolp))
262 (forward-line 1))))
8a2a4ced
RS
263 ;; No starter or separator line => use buffer beg.
264 (goto-char (point-min)))))
a2535589 265 (setq arg (1+ arg)))
8a2a4ced 266 (while (and (> arg 0) (not (eobp)))
88c2fc2c 267 ;; Move forward over separator lines, and one more line.
a2535589 268 (while (prog1 (and (not (eobp))
55cc5677
BG
269 (progn (move-to-left-margin) (not (eobp)))
270 (looking-at paragraph-separate))
a37669ec 271 (forward-line 1)))
a2535589
JA
272 (if fill-prefix-regexp
273 ;; There is a fill prefix; it overrides paragraph-start.
274 (while (and (not (eobp))
55cc5677
BG
275 (progn (move-to-left-margin) (not (eobp)))
276 (not (looking-at paragraph-separate))
a2535589
JA
277 (looking-at fill-prefix-regexp))
278 (forward-line 1))
55cc5677 279 (while (and (re-search-forward sp-paragraph-start nil 1)
55cc5677
BG
280 (progn (setq start (match-beginning 0))
281 (goto-char start)
4669fb3c
RS
282 (not (eobp)))
283 (progn (move-to-left-margin)
55cc5677
BG
284 (not (looking-at paragraph-separate)))
285 (or (not (looking-at paragraph-start))
286 (and use-hard-newlines
287 (not (get-text-property (1- start) 'hard)))))
a37669ec
RS
288 (forward-char 1))
289 (if (< (point) (point-max))
55cc5677 290 (goto-char start)))
779e6e56 291 (setq arg (1- arg)))
17cca868 292 (constrain-to-field nil opoint t)))
a2535589
JA
293
294(defun backward-paragraph (&optional arg)
295 "Move backward to start of paragraph.
94d63a23
RS
296With argument ARG, do it ARG times;
297a negative argument ARG = -N means move forward N paragraphs.
a2535589 298
23b34992
BP
299A paragraph start is the beginning of a line which is a
300`first-line-of-paragraph' or which is ordinary text and follows a
301paragraph-separating line; except: if the first real line of a
302paragraph is preceded by a blank line, the paragraph starts at that
303blank line.
304
305See `forward-paragraph' for more information."
a2535589
JA
306 (interactive "p")
307 (or arg (setq arg 1))
308 (forward-paragraph (- arg)))
309
310(defun mark-paragraph ()
311 "Put point at beginning of this paragraph, mark at end.
312The paragraph marked is the one that contains point or follows point."
313 (interactive)
314 (forward-paragraph 1)
0b108c01 315 (push-mark nil t t)
a2535589
JA
316 (backward-paragraph 1))
317
318(defun kill-paragraph (arg)
319 "Kill forward to end of paragraph.
320With arg N, kill forward to Nth end of paragraph;
321negative arg -N means kill backward to Nth start of paragraph."
275cf1b2 322 (interactive "p")
8d6eaa00 323 (kill-region (point) (progn (forward-paragraph arg) (point))))
a2535589
JA
324
325(defun backward-kill-paragraph (arg)
326 "Kill back to start of paragraph.
327With arg N, kill back to Nth start of paragraph;
328negative arg -N means kill forward to Nth end of paragraph."
275cf1b2 329 (interactive "p")
17cca868 330 (kill-region (point) (progn (backward-paragraph arg) (point))))
a2535589
JA
331
332(defun transpose-paragraphs (arg)
333 "Interchange this (or next) paragraph with previous one."
334 (interactive "*p")
335 (transpose-subr 'forward-paragraph arg))
336
337(defun start-of-paragraph-text ()
338 (let ((opoint (point)) npoint)
339 (forward-paragraph -1)
340 (setq npoint (point))
341 (skip-chars-forward " \t\n")
b4e6c391
RS
342 ;; If the range of blank lines found spans the original start point,
343 ;; try again from the beginning of it.
344 ;; Must be careful to avoid infinite loop
345 ;; when following a single return at start of buffer.
346 (if (and (>= (point) opoint) (< npoint opoint))
a2535589
JA
347 (progn
348 (goto-char npoint)
349 (if (> npoint (point-min))
350 (start-of-paragraph-text))))))
351
352(defun end-of-paragraph-text ()
353 (let ((opoint (point)))
354 (forward-paragraph 1)
355 (if (eq (preceding-char) ?\n) (forward-char -1))
356 (if (<= (point) opoint)
357 (progn
358 (forward-char 1)
359 (if (< (point) (point-max))
360 (end-of-paragraph-text))))))
361
362(defun forward-sentence (&optional arg)
51534471 363 "Move forward to next `sentence-end'. With argument, repeat.
23b34992 364With negative argument, move backward repeatedly to `sentence-beginning'.
a2535589 365
23b34992
BP
366The variable `sentence-end' is a regular expression that matches ends of
367sentences. Also, every paragraph boundary terminates sentences as well."
a2535589
JA
368 (interactive "p")
369 (or arg (setq arg 1))
17cca868
GM
370 (let ((opoint (point)))
371 (while (< arg 0)
372 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
3fe35f35
GM
373 (if (re-search-backward (concat "\\(" sentence-end "\\)[^ \t\n]")
374 par-beg t)
17cca868
GM
375 (goto-char (1- (match-end 0)))
376 (goto-char par-beg)))
377 (setq arg (1+ arg)))
378 (while (> arg 0)
379 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
380 (if (re-search-forward sentence-end par-end t)
381 (skip-chars-backward " \t\n")
382 (goto-char par-end)))
383 (setq arg (1- arg)))
384 (constrain-to-field nil opoint t)))
a2535589
JA
385
386(defun backward-sentence (&optional arg)
387 "Move backward to start of sentence. With arg, do it arg times.
23b34992 388See `forward-sentence' for more information."
a2535589
JA
389 (interactive "p")
390 (or arg (setq arg 1))
391 (forward-sentence (- arg)))
392
393(defun kill-sentence (&optional arg)
394 "Kill from point to end of sentence.
395With arg, repeat; negative arg -N means kill back to Nth start of sentence."
275cf1b2 396 (interactive "p")
8d6eaa00 397 (kill-region (point) (progn (forward-sentence arg) (point))))
a2535589
JA
398
399(defun backward-kill-sentence (&optional arg)
400 "Kill back from point to start of sentence.
401With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
275cf1b2 402 (interactive "p")
17cca868 403 (kill-region (point) (progn (backward-sentence arg) (point))))
a2535589
JA
404
405(defun mark-end-of-sentence (arg)
23b34992 406 "Put mark at end of sentence. Arg works as in `forward-sentence'."
a2535589
JA
407 (interactive "p")
408 (push-mark
409 (save-excursion
410 (forward-sentence arg)
a524dc5b
RS
411 (point))
412 nil t))
a2535589
JA
413
414(defun transpose-sentences (arg)
415 "Interchange this (next) and previous sentence."
416 (interactive "*p")
417 (transpose-subr 'forward-sentence arg))
6594deb0
ER
418
419;;; paragraphs.el ends here