Thanks to Christopher Allan Webber, fix typo.
[bpt/emacs.git] / lisp / textmodes / paragraphs.el
CommitLineData
55535639 1;;; paragraphs.el --- paragraph and sentence parsing
6594deb0 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
07187d55 37(define-minor-mode use-hard-newlines
965eb84a
RS
38 "Minor mode to distinguish hard and soft newlines.
39When active, the functions `newline' and `open-line' add the
40text-property `hard' to newlines that they insert, and a line is
55cc5677 41only considered as a candidate to match `paragraph-start' or
965eb84a 42`paragraph-separate' if it follows a hard newline.
55cc5677 43
965eb84a
RS
44Prefix argument says to turn mode on if positive, off if negative.
45When the mode is turned on, if there are newlines in the buffer but no hard
db95369b 46newlines, ask the user whether to mark as hard any newlines preceeding a
965eb84a
RS
47`paragraph-start' line. From a program, second arg INSERT specifies whether
48to do this; it can be `never' to change nothing, t or `always' to force
db95369b 49marking, `guess' to try to do the right thing with no questions, nil
965eb84a
RS
50or anything else to ask the user.
51
52Newlines not marked hard are called \"soft\", and are always internal
53to paragraphs. The fill functions insert and delete only soft newlines."
eacd92dd 54 :group 'paragraphs
07187d55
SM
55 :extra-args (insert)
56 (when use-hard-newlines
965eb84a
RS
57 ;; Turn mode on
58 ;; Intuit hard newlines --
59 ;; mark as hard any newlines preceding a paragraph-start line.
60 (if (or (eq insert t) (eq insert 'always)
61 (and (not (eq 'never insert))
965eb84a
RS
62 (not (text-property-any (point-min) (point-max) 'hard t))
63 (save-excursion
64 (goto-char (point-min))
65 (search-forward "\n" nil t))
66 (or (eq insert 'guess)
67 (y-or-n-p "Make newlines between paragraphs hard? "))))
68 (save-excursion
69 (goto-char (point-min))
70 (while (search-forward "\n" nil t)
71 (let ((pos (point)))
72 (move-to-left-margin)
07187d55
SM
73 (when (looking-at paragraph-start)
74 (set-hard-newline-properties (1- pos) pos))
75 ;; If paragraph-separate, newline after it is hard too.
76 (when (looking-at paragraph-separate)
77 (set-hard-newline-properties (1- pos) pos)
78 (end-of-line)
79 (unless (eobp)
80 (set-hard-newline-properties (point) (1+ (point)))))))))))
55cc5677 81
07187d55 82(defcustom paragraph-start "\f\\|[ \t]*$" "\
1f2007b3
RS
83*Regexp for beginning of a line that starts OR separates paragraphs.
84This regexp should match lines that separate paragraphs
85and should also match lines that start a paragraph
86\(and are part of that paragraph).
a37669ec 87
55cc5677
BG
88This is matched against the text at the left margin, which is not necessarily
89the beginning of the line, so it should never use \"^\" as an anchor. This
90ensures that the paragraph functions will work equally well within a region
91of text indented by a margin setting.
92
1f2007b3 93The variable `paragraph-separate' specifies how to distinguish
a37669ec
RS
94lines that start paragraphs from lines that separate them.
95
3f5dc0b0 96If the variable `use-hard-newlines' is non-nil, then only lines following a
e4550233
RS
97hard newline are considered to match."
98 :group 'paragraphs
99 :type 'regexp)
6503cec3 100
55cc5677
BG
101;; paragraph-start requires a hard newline, but paragraph-separate does not:
102;; It is assumed that paragraph-separate is distinctive enough to be believed
103;; whenever it occurs, while it is reasonable to set paragraph-start to
104;; something very minimal, even including "." (which makes every hard newline
105;; start a new paragraph).
106
e4550233
RS
107(defcustom paragraph-separate "[ \t\f]*$"
108 "*Regexp for beginning of a line that separates paragraphs.
9d7c4eb5 109If you change this, you may have to change `paragraph-start' also.
a37669ec 110
55cc5677
BG
111This is matched against the text at the left margin, which is not necessarily
112the beginning of the line, so it should not use \"^\" as an anchor. This
113ensures that the paragraph functions will work equally within a region of
e4550233
RS
114text indented by a margin setting."
115 :group 'paragraphs
116 :type 'regexp)
6503cec3 117
629261c7
SM
118(defcustom sentence-end-double-space t
119 "*Non-nil means a single space does not end a sentence.
120This is relevant for filling. See also `sentence-end-without-period'
121and `colon-double-space'.
122
32e40471
JL
123This value is used by the function `sentence-end' to construct the
124regexp describing the end of a sentence, in case when the value of
125the variable `sentence-end' is nil. See Info node `Sentences'."
629261c7
SM
126 :type 'boolean
127 :group 'fill)
128
129(defcustom sentence-end-without-period nil
130 "*Non-nil means a sentence will end without a period.
131For example, a sentence in Thai text ends with double space but
32e40471
JL
132without a period.
133
134This value is used by the function `sentence-end' to construct the
135regexp describing the end of a sentence, in case when the value of
136the variable `sentence-end' is nil. See Info node `Sentences'."
629261c7
SM
137 :type 'boolean
138 :group 'fill)
139
9402a4b9
KH
140(defcustom sentence-end-without-space
141 "\e$B!#!%!)!*\e$A!##.#?#!\e$(0!$!%!)!*\e$(G!$!%!)!*\e(B"
142 "*String containing characters that end sentence without following spaces.
32e40471
JL
143
144This value is used by the function `sentence-end' to construct the
145regexp describing the end of a sentence, in case when the value of
146the variable `sentence-end' is nil. See Info node `Sentences'."
9402a4b9
KH
147 :group 'paragraphs
148 :type 'string)
149
32e40471 150(defcustom sentence-end nil
e4550233 151 "*Regexp describing the end of a sentence.
ac1470eb 152The value includes the whitespace following the sentence.
51534471
JB
153All paragraph boundaries also end sentences, regardless.
154
32e40471
JL
155The value nil means to use the default value defined by the
156function `sentence-end'. You should always use this function
157to obtain the value of this variable."
e4550233 158 :group 'paragraphs
32e40471
JL
159 :type '(choice regexp (const :tag "Use default value" nil)))
160
161(defun sentence-end ()
162 "Return the regexp describing the end of a sentence.
163
164This function returns either the value of the variable `sentence-end'
165if it is non-nil, or the default value constructed from the
166variables `sentence-end-double-space', `sentence-end-without-period'
167and `sentence-end-without-space'. The default value specifies
168that in order to be recognized as the end of a sentence, the
169ending period, question mark, or exclamation point must be
170followed by two spaces, unless it's inside some sort of quotes or
171parenthesis. See Info node `Sentences'."
172 (or sentence-end
173 (concat (if sentence-end-without-period "\\w \\|")
7f2081e3 174 "\\([.?!][]\"'\xd0c9\x5397d)}]*"
32e40471
JL
175 (if sentence-end-double-space
176 "\\($\\| $\\|\t\\| \\)" "\\($\\|[\t ]\\)")
177 "\\|[" sentence-end-without-space "]+\\)"
178 "[ \t\n]*")))
e4550233
RS
179
180(defcustom page-delimiter "^\014"
181 "*Regexp describing line-beginnings that separate pages."
182 :group 'paragraphs
183 :type 'regexp)
184
185(defcustom paragraph-ignore-fill-prefix nil
186 "*Non-nil means the paragraph commands are not affected by `fill-prefix'.
187This is desirable in modes where blank lines are the paragraph delimiters."
188 :group 'paragraphs
189 :type 'boolean)
77176e73 190
a2535589
JA
191(defun forward-paragraph (&optional arg)
192 "Move forward to end of paragraph.
94d63a23
RS
193With argument ARG, do it ARG times;
194a negative argument ARG = -N means move backward N paragraphs.
a2535589
JA
195
196A line which `paragraph-start' matches either separates paragraphs
197\(if `paragraph-separate' matches it also) or is the first line of a paragraph.
198A paragraph end is the beginning of a line which is not part of the paragraph
de24b077
SM
199to which the end of the previous line belongs, or the end of the buffer.
200Returns the count of paragraphs left to move."
a2535589
JA
201 (interactive "p")
202 (or arg (setq arg 1))
17cca868
GM
203 (let* ((opoint (point))
204 (fill-prefix-regexp
a2535589
JA
205 (and fill-prefix (not (equal fill-prefix ""))
206 (not paragraph-ignore-fill-prefix)
207 (regexp-quote fill-prefix)))
55cc5677
BG
208 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
209 ;; These regexps shouldn't be anchored, because we look for them
210 ;; starting at the left-margin. This allows paragraph commands to
211 ;; work normally with indented text.
212 ;; This hack will not find problem cases like "whatever\\|^something".
629261c7
SM
213 (parstart (if (and (not (equal "" paragraph-start))
214 (equal ?^ (aref paragraph-start 0)))
215 (substring paragraph-start 1)
216 paragraph-start))
217 (parsep (if (and (not (equal "" paragraph-separate))
218 (equal ?^ (aref paragraph-separate 0)))
219 (substring paragraph-separate 1)
220 paragraph-separate))
221 (parsep
a2535589 222 (if fill-prefix-regexp
629261c7 223 (concat parsep "\\|"
a2535589 224 fill-prefix-regexp "[ \t]*$")
629261c7 225 parsep))
55cc5677 226 ;; This is used for searching.
629261c7 227 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
eeb0f327 228 start found-start)
8a2a4ced 229 (while (and (< arg 0) (not (bobp)))
629261c7 230 (if (and (not (looking-at parsep))
a37669ec 231 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
629261c7 232 (looking-at parsep))
de24b077 233 (setq arg (1+ arg))
2be01738 234 (setq start (point))
8a2a4ced 235 ;; Move back over paragraph-separating lines.
a2535589 236 (forward-char -1) (beginning-of-line)
a37669ec 237 (while (and (not (bobp))
55cc5677 238 (progn (move-to-left-margin)
629261c7 239 (looking-at parsep)))
3f5dc0b0 240 (forward-line -1))
8a2a4ced
RS
241 (if (bobp)
242 nil
de24b077 243 (setq arg (1+ arg))
8a2a4ced
RS
244 ;; Go to end of the previous (non-separating) line.
245 (end-of-line)
246 ;; Search back for line that starts or separates paragraphs.
247 (if (if fill-prefix-regexp
629261c7 248 ;; There is a fill prefix; it overrides parstart.
2be01738 249 (let (multiple-lines)
55cc5677
BG
250 (while (and (progn (beginning-of-line) (not (bobp)))
251 (progn (move-to-left-margin)
629261c7 252 (not (looking-at parsep)))
55cc5677 253 (looking-at fill-prefix-regexp))
3f5dc0b0
SM
254 (unless (= (point) start)
255 (setq multiple-lines t))
55cc5677 256 (forward-line -1))
2be01738 257 (move-to-left-margin)
629261c7
SM
258 ;; This deleted code caused a long hanging-indent line
259 ;; not to be filled together with the following lines.
260 ;; ;; Don't move back over a line before the paragraph
261 ;; ;; which doesn't start with fill-prefix
262 ;; ;; unless that is the only line we've moved over.
263 ;; (and (not (looking-at fill-prefix-regexp))
264 ;; multiple-lines
265 ;; (forward-line 1))
2be01738 266 (not (bobp)))
629261c7 267 (while (and (re-search-backward sp-parstart nil 1)
eeb0f327 268 (setq found-start t)
55cc5677 269 ;; Found a candidate, but need to check if it is a
629261c7 270 ;; REAL parstart.
55cc5677
BG
271 (progn (setq start (point))
272 (move-to-left-margin)
629261c7
SM
273 (not (looking-at parsep)))
274 (not (and (looking-at parstart)
275 (or (not use-hard-newlines)
276 (get-text-property (1- start) 'hard)
277 (bobp)))))
eeb0f327 278 (setq found-start nil)
55cc5677 279 (goto-char start))
eeb0f327 280 found-start)
de24b077
SM
281 ;; Found one.
282 (progn
8a2a4ced
RS
283 ;; Move forward over paragraph separators.
284 ;; We know this cannot reach the place we started
285 ;; because we know we moved back over a non-separator.
55cc5677
BG
286 (while (and (not (eobp))
287 (progn (move-to-left-margin)
629261c7 288 (looking-at parsep)))
8a2a4ced 289 (forward-line 1))
55cc5677
BG
290 ;; If line before paragraph is just margin, back up to there.
291 (end-of-line 0)
292 (if (> (current-column) (current-left-margin))
293 (forward-char 1)
294 (skip-chars-backward " \t")
295 (if (not (bolp))
296 (forward-line 1))))
8a2a4ced 297 ;; No starter or separator line => use buffer beg.
de24b077 298 (goto-char (point-min))))))
629261c7 299
8a2a4ced 300 (while (and (> arg 0) (not (eobp)))
de24b077
SM
301 ;; Move forward over separator lines...
302 (while (and (not (eobp))
303 (progn (move-to-left-margin) (not (eobp)))
304 (looking-at parsep))
305 (forward-line 1))
306 (unless (eobp) (setq arg (1- arg)))
307 ;; ... and one more line.
308 (forward-line 1)
a2535589 309 (if fill-prefix-regexp
629261c7 310 ;; There is a fill prefix; it overrides parstart.
a2535589 311 (while (and (not (eobp))
55cc5677 312 (progn (move-to-left-margin) (not (eobp)))
629261c7 313 (not (looking-at parsep))
a2535589
JA
314 (looking-at fill-prefix-regexp))
315 (forward-line 1))
629261c7 316 (while (and (re-search-forward sp-parstart nil 1)
55cc5677
BG
317 (progn (setq start (match-beginning 0))
318 (goto-char start)
4669fb3c
RS
319 (not (eobp)))
320 (progn (move-to-left-margin)
629261c7
SM
321 (not (looking-at parsep)))
322 (or (not (looking-at parstart))
55cc5677
BG
323 (and use-hard-newlines
324 (not (get-text-property (1- start) 'hard)))))
a37669ec
RS
325 (forward-char 1))
326 (if (< (point) (point-max))
de24b077 327 (goto-char start))))
7b462fc6
SM
328 (constrain-to-field nil opoint t)
329 ;; Return the number of steps that could not be done.
330 arg))
a2535589
JA
331
332(defun backward-paragraph (&optional arg)
333 "Move backward to start of paragraph.
94d63a23
RS
334With argument ARG, do it ARG times;
335a negative argument ARG = -N means move forward N paragraphs.
a2535589 336
23b34992
BP
337A paragraph start is the beginning of a line which is a
338`first-line-of-paragraph' or which is ordinary text and follows a
339paragraph-separating line; except: if the first real line of a
340paragraph is preceded by a blank line, the paragraph starts at that
341blank line.
342
343See `forward-paragraph' for more information."
a2535589
JA
344 (interactive "p")
345 (or arg (setq arg 1))
346 (forward-paragraph (- arg)))
347
f48b59a2 348(defun mark-paragraph (&optional arg)
a2535589 349 "Put point at beginning of this paragraph, mark at end.
f48b59a2
KG
350The paragraph marked is the one that contains point or follows point.
351
352With argument ARG, puts mark at end of a following paragraph, so that
353the number of paragraphs marked equals ARG.
354
355If ARG is negative, point is put at end of this paragraph, mark is put
cad113ae
KG
356at beginning of this or a previous paragraph.
357
358If this command is repeated, it marks the next ARG paragraphs after (or
359before, if arg is negative) the ones already marked."
f48b59a2 360 (interactive "p")
be0d25b6
KG
361 (unless arg (setq arg 1))
362 (when (zerop arg)
363 (error "Cannot mark zero paragraphs"))
364 (cond ((and (eq last-command this-command) (mark t))
365 (set-mark
366 (save-excursion
367 (goto-char (mark))
368 (forward-paragraph arg)
369 (point))))
370 (t
371 (forward-paragraph arg)
372 (push-mark nil t t)
373 (backward-paragraph arg))))
a2535589
JA
374
375(defun kill-paragraph (arg)
376 "Kill forward to end of paragraph.
377With arg N, kill forward to Nth end of paragraph;
378negative arg -N means kill backward to Nth start of paragraph."
275cf1b2 379 (interactive "p")
8d6eaa00 380 (kill-region (point) (progn (forward-paragraph arg) (point))))
a2535589
JA
381
382(defun backward-kill-paragraph (arg)
383 "Kill back to start of paragraph.
384With arg N, kill back to Nth start of paragraph;
385negative arg -N means kill forward to Nth end of paragraph."
275cf1b2 386 (interactive "p")
17cca868 387 (kill-region (point) (progn (backward-paragraph arg) (point))))
a2535589
JA
388
389(defun transpose-paragraphs (arg)
390 "Interchange this (or next) paragraph with previous one."
391 (interactive "*p")
392 (transpose-subr 'forward-paragraph arg))
393
394(defun start-of-paragraph-text ()
395 (let ((opoint (point)) npoint)
396 (forward-paragraph -1)
397 (setq npoint (point))
398 (skip-chars-forward " \t\n")
b4e6c391
RS
399 ;; If the range of blank lines found spans the original start point,
400 ;; try again from the beginning of it.
401 ;; Must be careful to avoid infinite loop
402 ;; when following a single return at start of buffer.
403 (if (and (>= (point) opoint) (< npoint opoint))
a2535589
JA
404 (progn
405 (goto-char npoint)
406 (if (> npoint (point-min))
407 (start-of-paragraph-text))))))
408
409(defun end-of-paragraph-text ()
410 (let ((opoint (point)))
411 (forward-paragraph 1)
412 (if (eq (preceding-char) ?\n) (forward-char -1))
413 (if (<= (point) opoint)
414 (progn
415 (forward-char 1)
416 (if (< (point) (point-max))
417 (end-of-paragraph-text))))))
418
419(defun forward-sentence (&optional arg)
51534471 420 "Move forward to next `sentence-end'. With argument, repeat.
23b34992 421With negative argument, move backward repeatedly to `sentence-beginning'.
a2535589 422
23b34992
BP
423The variable `sentence-end' is a regular expression that matches ends of
424sentences. Also, every paragraph boundary terminates sentences as well."
a2535589
JA
425 (interactive "p")
426 (or arg (setq arg 1))
32e40471
JL
427 (let ((opoint (point))
428 (sentence-end (sentence-end)))
17cca868 429 (while (< arg 0)
ea2c6478
GM
430 (let ((pos (point))
431 (par-beg (save-excursion (start-of-paragraph-text) (point))))
432 (if (and (re-search-backward sentence-end par-beg t)
433 (or (< (match-end 0) pos)
434 (re-search-backward sentence-end par-beg t)))
435 (goto-char (match-end 0))
17cca868
GM
436 (goto-char par-beg)))
437 (setq arg (1+ arg)))
438 (while (> arg 0)
439 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
440 (if (re-search-forward sentence-end par-end t)
441 (skip-chars-backward " \t\n")
442 (goto-char par-end)))
443 (setq arg (1- arg)))
444 (constrain-to-field nil opoint t)))
a2535589 445
d320a41d 446(defun repunctuate-sentences ()
d320a41d
RS
447 "Put two spaces at the end of sentences from point to the end of buffer.
448It works using `query-replace-regexp'."
279dffd6 449 (interactive)
d320a41d
RS
450 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
451 "\\1\\2\\3 "))
452
453
a2535589
JA
454(defun backward-sentence (&optional arg)
455 "Move backward to start of sentence. With arg, do it arg times.
23b34992 456See `forward-sentence' for more information."
a2535589
JA
457 (interactive "p")
458 (or arg (setq arg 1))
459 (forward-sentence (- arg)))
460
461(defun kill-sentence (&optional arg)
462 "Kill from point to end of sentence.
463With arg, repeat; negative arg -N means kill back to Nth start of sentence."
275cf1b2 464 (interactive "p")
8d6eaa00 465 (kill-region (point) (progn (forward-sentence arg) (point))))
a2535589
JA
466
467(defun backward-kill-sentence (&optional arg)
468 "Kill back from point to start of sentence.
469With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
275cf1b2 470 (interactive "p")
17cca868 471 (kill-region (point) (progn (backward-sentence arg) (point))))
a2535589
JA
472
473(defun mark-end-of-sentence (arg)
cad113ae
KG
474 "Put mark at end of sentence. Arg works as in `forward-sentence'.
475If this command is repeated, it marks the next ARG sentences after the
476ones already marked."
a2535589
JA
477 (interactive "p")
478 (push-mark
cad113ae
KG
479 (save-excursion
480 (if (and (eq last-command this-command) (mark t))
481 (goto-char (mark)))
482 (forward-sentence arg)
483 (point))
484 nil t))
a2535589
JA
485
486(defun transpose-sentences (arg)
487 "Interchange this (next) and previous sentence."
488 (interactive "*p")
489 (transpose-subr 'forward-sentence arg))
6594deb0 490
cc4fe8d2
KH
491;;; Local Variables:
492;;; coding: iso-2022-7bit
493;;; End:
494
ab5796a9 495;;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
6594deb0 496;;; paragraphs.el ends here