Sync to HEAD
[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
123If you change this, you should also change `sentence-end'. See Info
124node `Sentences'."
125 :type 'boolean
126 :group 'fill)
127
128(defcustom sentence-end-without-period nil
129 "*Non-nil means a sentence will end without a period.
130For example, a sentence in Thai text ends with double space but
131without a period."
132 :type 'boolean
133 :group 'fill)
134
6b61353c
KH
135(defcustom sentence-end-without-space
136 "\e$B!#!%!)!*\e$A!##.#?#!\e$(0!$!%!)!*\e$(G!$!%!)!*\e(B"
137 "*String containing characters that end sentence without following spaces.
138If you change this, you should also change `sentence-end'. See Info
139node `Sentences'."
140 :group 'paragraphs
141 :type 'string)
142
629261c7
SM
143(defcustom sentence-end
144 (purecopy
145 ;; This is a bit stupid since it's not auto-updated when the
146 ;; other variables are changes, but it's still useful info.
147 (concat (if sentence-end-without-period "\\w \\|")
6b61353c 148 "\\([.?!][]\"')}]*"
629261c7
SM
149 (if sentence-end-double-space
150 "\\($\\| $\\|\t\\| \\)" "\\($\\|[\t ]\\)")
6b61353c 151 "\\|[" sentence-end-without-space "]+\\)"
629261c7 152 "[ \t\n]*"))
e4550233 153 "*Regexp describing the end of a sentence.
ac1470eb 154The value includes the whitespace following the sentence.
51534471
JB
155All paragraph boundaries also end sentences, regardless.
156
220dae72
DL
157The default value specifies that in order to be recognized as the end
158of a sentence, the ending period, question mark, or exclamation point
159must be followed by two spaces, unless it's inside some sort of quotes
160or parenthesis.
161
8919155a
DL
162See also the variable `sentence-end-double-space', the variable
163`sentence-end-without-period' and Info node `Sentences'."
e4550233
RS
164 :group 'paragraphs
165 :type 'regexp)
166
167(defcustom page-delimiter "^\014"
168 "*Regexp describing line-beginnings that separate pages."
169 :group 'paragraphs
170 :type 'regexp)
171
172(defcustom paragraph-ignore-fill-prefix nil
173 "*Non-nil means the paragraph commands are not affected by `fill-prefix'.
174This is desirable in modes where blank lines are the paragraph delimiters."
175 :group 'paragraphs
176 :type 'boolean)
77176e73 177
a2535589
JA
178(defun forward-paragraph (&optional arg)
179 "Move forward to end of paragraph.
94d63a23
RS
180With argument ARG, do it ARG times;
181a negative argument ARG = -N means move backward N paragraphs.
a2535589
JA
182
183A line which `paragraph-start' matches either separates paragraphs
184\(if `paragraph-separate' matches it also) or is the first line of a paragraph.
185A paragraph end is the beginning of a line which is not part of the paragraph
de24b077
SM
186to which the end of the previous line belongs, or the end of the buffer.
187Returns the count of paragraphs left to move."
a2535589
JA
188 (interactive "p")
189 (or arg (setq arg 1))
17cca868
GM
190 (let* ((opoint (point))
191 (fill-prefix-regexp
a2535589
JA
192 (and fill-prefix (not (equal fill-prefix ""))
193 (not paragraph-ignore-fill-prefix)
194 (regexp-quote fill-prefix)))
55cc5677
BG
195 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
196 ;; These regexps shouldn't be anchored, because we look for them
197 ;; starting at the left-margin. This allows paragraph commands to
198 ;; work normally with indented text.
199 ;; This hack will not find problem cases like "whatever\\|^something".
629261c7
SM
200 (parstart (if (and (not (equal "" paragraph-start))
201 (equal ?^ (aref paragraph-start 0)))
202 (substring paragraph-start 1)
203 paragraph-start))
204 (parsep (if (and (not (equal "" paragraph-separate))
205 (equal ?^ (aref paragraph-separate 0)))
206 (substring paragraph-separate 1)
207 paragraph-separate))
208 (parsep
a2535589 209 (if fill-prefix-regexp
629261c7 210 (concat parsep "\\|"
a2535589 211 fill-prefix-regexp "[ \t]*$")
629261c7 212 parsep))
55cc5677 213 ;; This is used for searching.
629261c7 214 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
eeb0f327 215 start found-start)
8a2a4ced 216 (while (and (< arg 0) (not (bobp)))
629261c7 217 (if (and (not (looking-at parsep))
a37669ec 218 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
629261c7 219 (looking-at parsep))
de24b077 220 (setq arg (1+ arg))
2be01738 221 (setq start (point))
8a2a4ced 222 ;; Move back over paragraph-separating lines.
a2535589 223 (forward-char -1) (beginning-of-line)
a37669ec 224 (while (and (not (bobp))
55cc5677 225 (progn (move-to-left-margin)
629261c7 226 (looking-at parsep)))
3f5dc0b0 227 (forward-line -1))
8a2a4ced
RS
228 (if (bobp)
229 nil
de24b077 230 (setq arg (1+ arg))
8a2a4ced
RS
231 ;; Go to end of the previous (non-separating) line.
232 (end-of-line)
233 ;; Search back for line that starts or separates paragraphs.
234 (if (if fill-prefix-regexp
629261c7 235 ;; There is a fill prefix; it overrides parstart.
2be01738 236 (let (multiple-lines)
55cc5677
BG
237 (while (and (progn (beginning-of-line) (not (bobp)))
238 (progn (move-to-left-margin)
629261c7 239 (not (looking-at parsep)))
55cc5677 240 (looking-at fill-prefix-regexp))
3f5dc0b0
SM
241 (unless (= (point) start)
242 (setq multiple-lines t))
55cc5677 243 (forward-line -1))
2be01738 244 (move-to-left-margin)
629261c7
SM
245 ;; This deleted code caused a long hanging-indent line
246 ;; not to be filled together with the following lines.
247 ;; ;; Don't move back over a line before the paragraph
248 ;; ;; which doesn't start with fill-prefix
249 ;; ;; unless that is the only line we've moved over.
250 ;; (and (not (looking-at fill-prefix-regexp))
251 ;; multiple-lines
252 ;; (forward-line 1))
2be01738 253 (not (bobp)))
629261c7 254 (while (and (re-search-backward sp-parstart nil 1)
eeb0f327 255 (setq found-start t)
55cc5677 256 ;; Found a candidate, but need to check if it is a
629261c7 257 ;; REAL parstart.
55cc5677
BG
258 (progn (setq start (point))
259 (move-to-left-margin)
629261c7
SM
260 (not (looking-at parsep)))
261 (not (and (looking-at parstart)
262 (or (not use-hard-newlines)
263 (get-text-property (1- start) 'hard)
264 (bobp)))))
eeb0f327 265 (setq found-start nil)
55cc5677 266 (goto-char start))
eeb0f327 267 found-start)
de24b077
SM
268 ;; Found one.
269 (progn
8a2a4ced
RS
270 ;; Move forward over paragraph separators.
271 ;; We know this cannot reach the place we started
272 ;; because we know we moved back over a non-separator.
55cc5677
BG
273 (while (and (not (eobp))
274 (progn (move-to-left-margin)
629261c7 275 (looking-at parsep)))
8a2a4ced 276 (forward-line 1))
55cc5677
BG
277 ;; If line before paragraph is just margin, back up to there.
278 (end-of-line 0)
279 (if (> (current-column) (current-left-margin))
280 (forward-char 1)
281 (skip-chars-backward " \t")
282 (if (not (bolp))
283 (forward-line 1))))
8a2a4ced 284 ;; No starter or separator line => use buffer beg.
de24b077 285 (goto-char (point-min))))))
629261c7 286
8a2a4ced 287 (while (and (> arg 0) (not (eobp)))
de24b077
SM
288 ;; Move forward over separator lines...
289 (while (and (not (eobp))
290 (progn (move-to-left-margin) (not (eobp)))
291 (looking-at parsep))
292 (forward-line 1))
293 (unless (eobp) (setq arg (1- arg)))
294 ;; ... and one more line.
295 (forward-line 1)
a2535589 296 (if fill-prefix-regexp
629261c7 297 ;; There is a fill prefix; it overrides parstart.
a2535589 298 (while (and (not (eobp))
55cc5677 299 (progn (move-to-left-margin) (not (eobp)))
629261c7 300 (not (looking-at parsep))
a2535589
JA
301 (looking-at fill-prefix-regexp))
302 (forward-line 1))
629261c7 303 (while (and (re-search-forward sp-parstart nil 1)
55cc5677
BG
304 (progn (setq start (match-beginning 0))
305 (goto-char start)
4669fb3c
RS
306 (not (eobp)))
307 (progn (move-to-left-margin)
629261c7
SM
308 (not (looking-at parsep)))
309 (or (not (looking-at parstart))
55cc5677
BG
310 (and use-hard-newlines
311 (not (get-text-property (1- start) 'hard)))))
a37669ec
RS
312 (forward-char 1))
313 (if (< (point) (point-max))
de24b077 314 (goto-char start))))
7b462fc6
SM
315 (constrain-to-field nil opoint t)
316 ;; Return the number of steps that could not be done.
317 arg))
a2535589
JA
318
319(defun backward-paragraph (&optional arg)
320 "Move backward to start of paragraph.
94d63a23
RS
321With argument ARG, do it ARG times;
322a negative argument ARG = -N means move forward N paragraphs.
a2535589 323
23b34992
BP
324A paragraph start is the beginning of a line which is a
325`first-line-of-paragraph' or which is ordinary text and follows a
326paragraph-separating line; except: if the first real line of a
327paragraph is preceded by a blank line, the paragraph starts at that
328blank line.
329
330See `forward-paragraph' for more information."
a2535589
JA
331 (interactive "p")
332 (or arg (setq arg 1))
333 (forward-paragraph (- arg)))
334
f48b59a2 335(defun mark-paragraph (&optional arg)
a2535589 336 "Put point at beginning of this paragraph, mark at end.
f48b59a2
KG
337The paragraph marked is the one that contains point or follows point.
338
339With argument ARG, puts mark at end of a following paragraph, so that
340the number of paragraphs marked equals ARG.
341
342If ARG is negative, point is put at end of this paragraph, mark is put
cad113ae
KG
343at beginning of this or a previous paragraph.
344
345If this command is repeated, it marks the next ARG paragraphs after (or
346before, if arg is negative) the ones already marked."
f48b59a2 347 (interactive "p")
be0d25b6
KG
348 (unless arg (setq arg 1))
349 (when (zerop arg)
350 (error "Cannot mark zero paragraphs"))
351 (cond ((and (eq last-command this-command) (mark t))
352 (set-mark
353 (save-excursion
354 (goto-char (mark))
355 (forward-paragraph arg)
356 (point))))
357 (t
358 (forward-paragraph arg)
359 (push-mark nil t t)
360 (backward-paragraph arg))))
a2535589
JA
361
362(defun kill-paragraph (arg)
363 "Kill forward to end of paragraph.
364With arg N, kill forward to Nth end of paragraph;
365negative arg -N means kill backward to Nth start of paragraph."
275cf1b2 366 (interactive "p")
8d6eaa00 367 (kill-region (point) (progn (forward-paragraph arg) (point))))
a2535589
JA
368
369(defun backward-kill-paragraph (arg)
370 "Kill back to start of paragraph.
371With arg N, kill back to Nth start of paragraph;
372negative arg -N means kill forward to Nth end of paragraph."
275cf1b2 373 (interactive "p")
17cca868 374 (kill-region (point) (progn (backward-paragraph arg) (point))))
a2535589
JA
375
376(defun transpose-paragraphs (arg)
377 "Interchange this (or next) paragraph with previous one."
378 (interactive "*p")
379 (transpose-subr 'forward-paragraph arg))
380
381(defun start-of-paragraph-text ()
382 (let ((opoint (point)) npoint)
383 (forward-paragraph -1)
384 (setq npoint (point))
385 (skip-chars-forward " \t\n")
b4e6c391
RS
386 ;; If the range of blank lines found spans the original start point,
387 ;; try again from the beginning of it.
388 ;; Must be careful to avoid infinite loop
389 ;; when following a single return at start of buffer.
390 (if (and (>= (point) opoint) (< npoint opoint))
a2535589
JA
391 (progn
392 (goto-char npoint)
393 (if (> npoint (point-min))
394 (start-of-paragraph-text))))))
395
396(defun end-of-paragraph-text ()
397 (let ((opoint (point)))
398 (forward-paragraph 1)
399 (if (eq (preceding-char) ?\n) (forward-char -1))
400 (if (<= (point) opoint)
401 (progn
402 (forward-char 1)
403 (if (< (point) (point-max))
404 (end-of-paragraph-text))))))
405
406(defun forward-sentence (&optional arg)
51534471 407 "Move forward to next `sentence-end'. With argument, repeat.
23b34992 408With negative argument, move backward repeatedly to `sentence-beginning'.
a2535589 409
23b34992
BP
410The variable `sentence-end' is a regular expression that matches ends of
411sentences. Also, every paragraph boundary terminates sentences as well."
a2535589
JA
412 (interactive "p")
413 (or arg (setq arg 1))
17cca868
GM
414 (let ((opoint (point)))
415 (while (< arg 0)
ea2c6478
GM
416 (let ((pos (point))
417 (par-beg (save-excursion (start-of-paragraph-text) (point))))
418 (if (and (re-search-backward sentence-end par-beg t)
419 (or (< (match-end 0) pos)
420 (re-search-backward sentence-end par-beg t)))
421 (goto-char (match-end 0))
17cca868
GM
422 (goto-char par-beg)))
423 (setq arg (1+ arg)))
424 (while (> arg 0)
425 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
426 (if (re-search-forward sentence-end par-end t)
427 (skip-chars-backward " \t\n")
428 (goto-char par-end)))
429 (setq arg (1- arg)))
430 (constrain-to-field nil opoint t)))
a2535589 431
d320a41d 432(defun repunctuate-sentences ()
d320a41d
RS
433 "Put two spaces at the end of sentences from point to the end of buffer.
434It works using `query-replace-regexp'."
6b61353c 435 (interactive)
d320a41d
RS
436 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
437 "\\1\\2\\3 "))
438
439
a2535589
JA
440(defun backward-sentence (&optional arg)
441 "Move backward to start of sentence. With arg, do it arg times.
23b34992 442See `forward-sentence' for more information."
a2535589
JA
443 (interactive "p")
444 (or arg (setq arg 1))
445 (forward-sentence (- arg)))
446
447(defun kill-sentence (&optional arg)
448 "Kill from point to end of sentence.
449With arg, repeat; negative arg -N means kill back to Nth start of sentence."
275cf1b2 450 (interactive "p")
8d6eaa00 451 (kill-region (point) (progn (forward-sentence arg) (point))))
a2535589
JA
452
453(defun backward-kill-sentence (&optional arg)
454 "Kill back from point to start of sentence.
455With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
275cf1b2 456 (interactive "p")
17cca868 457 (kill-region (point) (progn (backward-sentence arg) (point))))
a2535589
JA
458
459(defun mark-end-of-sentence (arg)
cad113ae
KG
460 "Put mark at end of sentence. Arg works as in `forward-sentence'.
461If this command is repeated, it marks the next ARG sentences after the
462ones already marked."
a2535589
JA
463 (interactive "p")
464 (push-mark
cad113ae
KG
465 (save-excursion
466 (if (and (eq last-command this-command) (mark t))
467 (goto-char (mark)))
468 (forward-sentence arg)
469 (point))
470 nil t))
a2535589
JA
471
472(defun transpose-sentences (arg)
473 "Interchange this (next) and previous sentence."
474 (interactive "*p")
475 (transpose-subr 'forward-sentence arg))
6594deb0 476
cc4fe8d2
KH
477;;; Local Variables:
478;;; coding: iso-2022-7bit
479;;; End:
480
6b61353c 481;;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
6594deb0 482;;; paragraphs.el ends here