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