(cperl-switch-to-doc-buffer): Don't use interactive-p.
[emacs.git] / lisp / textmodes / paragraphs.el
blob206f7a42f78cccbb89cd3e1cdd7a5a88fb596c32
1 ;;; paragraphs.el --- paragraph and sentence parsing
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96, 1997, 1999, 2000, 2001, 2004
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
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.
26 ;;; Commentary:
28 ;; This package provides the paragraph-oriented commands documented in the
29 ;; Emacs manual.
31 ;;; Code:
33 (defgroup paragraphs nil
34 "Paragraph and sentence parsing."
35 :group 'editing)
37 (put 'use-hard-newlines 'permanent-local t)
38 (define-minor-mode use-hard-newlines
39 "Minor mode to distinguish hard and soft newlines.
40 When active, the functions `newline' and `open-line' add the
41 text-property `hard' to newlines that they insert, and a line is
42 only considered as a candidate to match `paragraph-start' or
43 `paragraph-separate' if it follows a hard newline.
45 Prefix argument says to turn mode on if positive, off if negative.
46 When the mode is turned on, if there are newlines in the buffer but no hard
47 newlines, ask the user whether to mark as hard any newlines preceeding a
48 `paragraph-start' line. From a program, second arg INSERT specifies whether
49 to do this; it can be `never' to change nothing, t or `always' to force
50 marking, `guess' to try to do the right thing with no questions, nil
51 or anything else to ask the user.
53 Newlines not marked hard are called \"soft\", and are always internal
54 to paragraphs. The fill functions insert and delete only soft newlines."
55 :group 'paragraphs
56 :extra-args (insert)
57 (when use-hard-newlines
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))
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)
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)))))))))))
83 (defcustom paragraph-start "\f\\|[ \t]*$" "\
84 *Regexp for beginning of a line that starts OR separates paragraphs.
85 This regexp should match lines that separate paragraphs
86 and should also match lines that start a paragraph
87 \(and are part of that paragraph).
89 This is matched against the text at the left margin, which is not necessarily
90 the beginning of the line, so it should never use \"^\" as an anchor. This
91 ensures that the paragraph functions will work equally well within a region
92 of text indented by a margin setting.
94 The variable `paragraph-separate' specifies how to distinguish
95 lines that start paragraphs from lines that separate them.
97 If the variable `use-hard-newlines' is non-nil, then only lines following a
98 hard newline are considered to match."
99 :group 'paragraphs
100 :type 'regexp)
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).
108 (defcustom paragraph-separate "[ \t\f]*$"
109 "*Regexp for beginning of a line that separates paragraphs.
110 If you change this, you may have to change `paragraph-start' also.
112 This is matched against the text at the left margin, which is not necessarily
113 the beginning of the line, so it should not use \"^\" as an anchor. This
114 ensures that the paragraph functions will work equally within a region of
115 text indented by a margin setting."
116 :group 'paragraphs
117 :type 'regexp)
119 (defcustom sentence-end-double-space t
120 "*Non-nil means a single space does not end a sentence.
121 This is relevant for filling. See also `sentence-end-without-period'
122 and `colon-double-space'.
124 This value is used by the function `sentence-end' to construct the
125 regexp describing the end of a sentence, when the value of the variable
126 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
127 :type 'boolean
128 :group 'fill)
130 (defcustom sentence-end-without-period nil
131 "*Non-nil means a sentence will end without a period.
132 For example, a sentence in Thai text ends with double space but
133 without a period.
135 This value is used by the function `sentence-end' to construct the
136 regexp describing the end of a sentence, when the value of the variable
137 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
138 :type 'boolean
139 :group 'fill)
141 (defcustom sentence-end-without-space
142 "\e$B!#!%!)!*\e$A!##.#?#!\e$(0!$!%!)!*\e$(G!$!%!)!*\e(B"
143 "*String of characters that end sentence without following spaces.
145 This value is used by the function `sentence-end' to construct the
146 regexp describing the end of a sentence, when the value of the variable
147 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
148 :group 'paragraphs
149 :type 'string)
151 (defcustom sentence-end nil
152 "*Regexp describing the end of a sentence.
153 The value includes the whitespace following the sentence.
154 All paragraph boundaries also end sentences, regardless.
156 The value nil means to use the default value defined by the
157 function `sentence-end'. You should always use this function
158 to obtain the value of this variable."
159 :group 'paragraphs
160 :type '(choice regexp (const :tag "Use default value" nil)))
162 (defun sentence-end ()
163 "Return the regexp describing the end of a sentence.
165 This function returns either the value of the variable `sentence-end'
166 if it is non-nil, or the default value constructed from the
167 variables `sentence-end-double-space', `sentence-end-without-period'
168 and `sentence-end-without-space'. The default value specifies
169 that in order to be recognized as the end of a sentence, the
170 ending period, question mark, or exclamation point must be
171 followed by two spaces, unless it's inside some sort of quotes or
172 parenthesis. See Info node `(elisp)Standard Regexps'."
173 (or sentence-end
174 (concat (if sentence-end-without-period "\\w \\|")
175 "\\([.?!][]\"'\xd0c9\x5397d)}]*"
176 (if sentence-end-double-space
177 "\\($\\| $\\|\t\\| \\)" "\\($\\|[\t ]\\)")
178 "\\|[" sentence-end-without-space "]+\\)"
179 "[ \t\n]*")))
181 (defcustom page-delimiter "^\014"
182 "*Regexp describing line-beginnings that separate pages."
183 :group 'paragraphs
184 :type 'regexp)
186 (defcustom paragraph-ignore-fill-prefix nil
187 "*Non-nil means the paragraph commands are not affected by `fill-prefix'.
188 This is desirable in modes where blank lines are the paragraph delimiters."
189 :group 'paragraphs
190 :type 'boolean)
192 (defun forward-paragraph (&optional arg)
193 "Move forward to end of paragraph.
194 With argument ARG, do it ARG times;
195 a negative argument ARG = -N means move backward N paragraphs.
197 A line which `paragraph-start' matches either separates paragraphs
198 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
199 A paragraph end is the beginning of a line which is not part of the paragraph
200 to which the end of the previous line belongs, or the end of the buffer.
201 Returns the count of paragraphs left to move."
202 (interactive "p")
203 (or arg (setq arg 1))
204 (let* ((opoint (point))
205 (fill-prefix-regexp
206 (and fill-prefix (not (equal fill-prefix ""))
207 (not paragraph-ignore-fill-prefix)
208 (regexp-quote fill-prefix)))
209 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
210 ;; These regexps shouldn't be anchored, because we look for them
211 ;; starting at the left-margin. This allows paragraph commands to
212 ;; work normally with indented text.
213 ;; This hack will not find problem cases like "whatever\\|^something".
214 (parstart (if (and (not (equal "" paragraph-start))
215 (equal ?^ (aref paragraph-start 0)))
216 (substring paragraph-start 1)
217 paragraph-start))
218 (parsep (if (and (not (equal "" paragraph-separate))
219 (equal ?^ (aref paragraph-separate 0)))
220 (substring paragraph-separate 1)
221 paragraph-separate))
222 (parsep
223 (if fill-prefix-regexp
224 (concat parsep "\\|"
225 fill-prefix-regexp "[ \t]*$")
226 parsep))
227 ;; This is used for searching.
228 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
229 start found-start)
230 (while (and (< arg 0) (not (bobp)))
231 (if (and (not (looking-at parsep))
232 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
233 (looking-at parsep))
234 (setq arg (1+ arg))
235 (setq start (point))
236 ;; Move back over paragraph-separating lines.
237 (forward-char -1) (beginning-of-line)
238 (while (and (not (bobp))
239 (progn (move-to-left-margin)
240 (looking-at parsep)))
241 (forward-line -1))
242 (if (bobp)
244 (setq arg (1+ arg))
245 ;; Go to end of the previous (non-separating) line.
246 (end-of-line)
247 ;; Search back for line that starts or separates paragraphs.
248 (if (if fill-prefix-regexp
249 ;; There is a fill prefix; it overrides parstart.
250 (let (multiple-lines)
251 (while (and (progn (beginning-of-line) (not (bobp)))
252 (progn (move-to-left-margin)
253 (not (looking-at parsep)))
254 (looking-at fill-prefix-regexp))
255 (unless (= (point) start)
256 (setq multiple-lines t))
257 (forward-line -1))
258 (move-to-left-margin)
259 ;; This deleted code caused a long hanging-indent line
260 ;; not to be filled together with the following lines.
261 ;; ;; Don't move back over a line before the paragraph
262 ;; ;; which doesn't start with fill-prefix
263 ;; ;; unless that is the only line we've moved over.
264 ;; (and (not (looking-at fill-prefix-regexp))
265 ;; multiple-lines
266 ;; (forward-line 1))
267 (not (bobp)))
268 (while (and (re-search-backward sp-parstart nil 1)
269 (setq found-start t)
270 ;; Found a candidate, but need to check if it is a
271 ;; REAL parstart.
272 (progn (setq start (point))
273 (move-to-left-margin)
274 (not (looking-at parsep)))
275 (not (and (looking-at parstart)
276 (or (not use-hard-newlines)
277 (bobp)
278 (get-text-property
279 (1- start) 'hard)))))
280 (setq found-start nil)
281 (goto-char start))
282 found-start)
283 ;; Found one.
284 (progn
285 ;; Move forward over paragraph separators.
286 ;; We know this cannot reach the place we started
287 ;; because we know we moved back over a non-separator.
288 (while (and (not (eobp))
289 (progn (move-to-left-margin)
290 (looking-at parsep)))
291 (forward-line 1))
292 ;; If line before paragraph is just margin, back up to there.
293 (end-of-line 0)
294 (if (> (current-column) (current-left-margin))
295 (forward-char 1)
296 (skip-chars-backward " \t")
297 (if (not (bolp))
298 (forward-line 1))))
299 ;; No starter or separator line => use buffer beg.
300 (goto-char (point-min))))))
302 (while (and (> arg 0) (not (eobp)))
303 ;; Move forward over separator lines...
304 (while (and (not (eobp))
305 (progn (move-to-left-margin) (not (eobp)))
306 (looking-at parsep))
307 (forward-line 1))
308 (unless (eobp) (setq arg (1- arg)))
309 ;; ... and one more line.
310 (forward-line 1)
311 (if fill-prefix-regexp
312 ;; There is a fill prefix; it overrides parstart.
313 (while (and (not (eobp))
314 (progn (move-to-left-margin) (not (eobp)))
315 (not (looking-at parsep))
316 (looking-at fill-prefix-regexp))
317 (forward-line 1))
318 (while (and (re-search-forward sp-parstart nil 1)
319 (progn (setq start (match-beginning 0))
320 (goto-char start)
321 (not (eobp)))
322 (progn (move-to-left-margin)
323 (not (looking-at parsep)))
324 (or (not (looking-at parstart))
325 (and use-hard-newlines
326 (not (get-text-property (1- start) 'hard)))))
327 (forward-char 1))
328 (if (< (point) (point-max))
329 (goto-char start))))
330 (constrain-to-field nil opoint t)
331 ;; Return the number of steps that could not be done.
332 arg))
334 (defun backward-paragraph (&optional arg)
335 "Move backward to start of paragraph.
336 With argument ARG, do it ARG times;
337 a negative argument ARG = -N means move forward N paragraphs.
339 A paragraph start is the beginning of a line which is a
340 `first-line-of-paragraph' or which is ordinary text and follows a
341 paragraph-separating line; except: if the first real line of a
342 paragraph is preceded by a blank line, the paragraph starts at that
343 blank line.
345 See `forward-paragraph' for more information."
346 (interactive "p")
347 (or arg (setq arg 1))
348 (forward-paragraph (- arg)))
350 (defun mark-paragraph (&optional arg)
351 "Put point at beginning of this paragraph, mark at end.
352 The paragraph marked is the one that contains point or follows point.
354 With argument ARG, puts mark at end of a following paragraph, so that
355 the number of paragraphs marked equals ARG.
357 If ARG is negative, point is put at end of this paragraph, mark is put
358 at beginning of this or a previous paragraph.
360 If this command is repeated, it marks the next ARG paragraphs after (or
361 before, if arg is negative) the ones already marked."
362 (interactive "p")
363 (unless arg (setq arg 1))
364 (when (zerop arg)
365 (error "Cannot mark zero paragraphs"))
366 (cond ((and (eq last-command this-command) (mark t))
367 (set-mark
368 (save-excursion
369 (goto-char (mark))
370 (forward-paragraph arg)
371 (point))))
373 (forward-paragraph arg)
374 (push-mark nil t t)
375 (backward-paragraph arg))))
377 (defun kill-paragraph (arg)
378 "Kill forward to end of paragraph.
379 With arg N, kill forward to Nth end of paragraph;
380 negative arg -N means kill backward to Nth start of paragraph."
381 (interactive "p")
382 (kill-region (point) (progn (forward-paragraph arg) (point))))
384 (defun backward-kill-paragraph (arg)
385 "Kill back to start of paragraph.
386 With arg N, kill back to Nth start of paragraph;
387 negative arg -N means kill forward to Nth end of paragraph."
388 (interactive "p")
389 (kill-region (point) (progn (backward-paragraph arg) (point))))
391 (defun transpose-paragraphs (arg)
392 "Interchange this (or next) paragraph with previous one."
393 (interactive "*p")
394 (transpose-subr 'forward-paragraph arg))
396 (defun start-of-paragraph-text ()
397 (let ((opoint (point)) npoint)
398 (forward-paragraph -1)
399 (setq npoint (point))
400 (skip-chars-forward " \t\n")
401 ;; If the range of blank lines found spans the original start point,
402 ;; try again from the beginning of it.
403 ;; Must be careful to avoid infinite loop
404 ;; when following a single return at start of buffer.
405 (if (and (>= (point) opoint) (< npoint opoint))
406 (progn
407 (goto-char npoint)
408 (if (> npoint (point-min))
409 (start-of-paragraph-text))))))
411 (defun end-of-paragraph-text ()
412 (let ((opoint (point)))
413 (forward-paragraph 1)
414 (if (eq (preceding-char) ?\n) (forward-char -1))
415 (if (<= (point) opoint)
416 (progn
417 (forward-char 1)
418 (if (< (point) (point-max))
419 (end-of-paragraph-text))))))
421 (defun forward-sentence (&optional arg)
422 "Move forward to next `sentence-end'. With argument, repeat.
423 With negative argument, move backward repeatedly to `sentence-beginning'.
425 The variable `sentence-end' is a regular expression that matches ends of
426 sentences. Also, every paragraph boundary terminates sentences as well."
427 (interactive "p")
428 (or arg (setq arg 1))
429 (let ((opoint (point))
430 (sentence-end (sentence-end)))
431 (while (< arg 0)
432 (let ((pos (point))
433 (par-beg (save-excursion (start-of-paragraph-text) (point))))
434 (if (and (re-search-backward sentence-end par-beg t)
435 (or (< (match-end 0) pos)
436 (re-search-backward sentence-end par-beg t)))
437 (goto-char (match-end 0))
438 (goto-char par-beg)))
439 (setq arg (1+ arg)))
440 (while (> arg 0)
441 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
442 (if (re-search-forward sentence-end par-end t)
443 (skip-chars-backward " \t\n")
444 (goto-char par-end)))
445 (setq arg (1- arg)))
446 (constrain-to-field nil opoint t)))
448 (defun repunctuate-sentences ()
449 "Put two spaces at the end of sentences from point to the end of buffer.
450 It works using `query-replace-regexp'."
451 (interactive)
452 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
453 "\\1\\2\\3 "))
456 (defun backward-sentence (&optional arg)
457 "Move backward to start of sentence. With arg, do it arg times.
458 See `forward-sentence' for more information."
459 (interactive "p")
460 (or arg (setq arg 1))
461 (forward-sentence (- arg)))
463 (defun kill-sentence (&optional arg)
464 "Kill from point to end of sentence.
465 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
466 (interactive "p")
467 (kill-region (point) (progn (forward-sentence arg) (point))))
469 (defun backward-kill-sentence (&optional arg)
470 "Kill back from point to start of sentence.
471 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
472 (interactive "p")
473 (kill-region (point) (progn (backward-sentence arg) (point))))
475 (defun mark-end-of-sentence (arg)
476 "Put mark at end of sentence. Arg works as in `forward-sentence'.
477 If this command is repeated, it marks the next ARG sentences after the
478 ones already marked."
479 (interactive "p")
480 (push-mark
481 (save-excursion
482 (if (and (eq last-command this-command) (mark t))
483 (goto-char (mark)))
484 (forward-sentence arg)
485 (point))
486 nil t))
488 (defun transpose-sentences (arg)
489 "Interchange this (next) and previous sentence."
490 (interactive "*p")
491 (transpose-subr 'forward-sentence arg))
493 ;;; Local Variables:
494 ;;; coding: iso-2022-7bit
495 ;;; End:
497 ;;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
498 ;;; paragraphs.el ends here