Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / textmodes / paragraphs.el
blob4f1bcefa90e491c3c52524392841d768fba24c5f
1 ;;; paragraphs.el --- paragraph and sentence parsing
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1994, 1995, 1996, 1997, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: wp
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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)
101 (put 'paragraph-start 'safe-local-variable 'stringp)
103 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
104 ;; It is assumed that paragraph-separate is distinctive enough to be believed
105 ;; whenever it occurs, while it is reasonable to set paragraph-start to
106 ;; something very minimal, even including "." (which makes every hard newline
107 ;; start a new paragraph).
109 (defcustom paragraph-separate "[ \t\f]*$"
110 "Regexp for beginning of a line that separates paragraphs.
111 If you change this, you may have to change `paragraph-start' also.
113 This is matched against the text at the left margin, which is not necessarily
114 the beginning of the line, so it should not use \"^\" as an anchor. This
115 ensures that the paragraph functions will work equally within a region of
116 text indented by a margin setting."
117 :group 'paragraphs
118 :type 'regexp)
119 (put 'paragraph-separate 'safe-local-variable 'stringp)
121 (defcustom sentence-end-double-space t
122 "Non-nil means a single space does not end a sentence.
123 This is relevant for filling. See also `sentence-end-without-period'
124 and `colon-double-space'.
126 This value is used by the function `sentence-end' to construct the
127 regexp describing the end of a sentence, when the value of the variable
128 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
129 :type 'boolean
130 :group 'fill)
131 (put 'sentence-end-double-space 'safe-local-variable 'booleanp)
133 (defcustom sentence-end-without-period nil
134 "Non-nil means a sentence will end without a period.
135 For example, a sentence in Thai text ends with double space but
136 without a period.
138 This value is used by the function `sentence-end' to construct the
139 regexp describing the end of a sentence, when the value of the variable
140 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
141 :type 'boolean
142 :group 'fill)
143 (put 'sentence-end-without-period 'safe-local-variable 'booleanp)
145 (defcustom sentence-end-without-space
146 "。.?!"
147 "String of characters that end sentence without following spaces.
149 This value is used by the function `sentence-end' to construct the
150 regexp describing the end of a sentence, when the value of the variable
151 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
152 :group 'paragraphs
153 :type 'string)
154 (put 'sentence-end-without-space 'safe-local-variable 'stringp)
156 (defcustom sentence-end nil
157 "Regexp describing the end of a sentence.
158 The value includes the whitespace following the sentence.
159 All paragraph boundaries also end sentences, regardless.
161 The value nil means to use the default value defined by the
162 function `sentence-end'. You should always use this function
163 to obtain the value of this variable."
164 :group 'paragraphs
165 :type '(choice regexp (const :tag "Use default value" nil)))
166 (put 'sentence-end 'safe-local-variable 'string-or-null-p)
168 (defcustom sentence-end-base "[.?!][]\"'”)}]*"
169 "Regexp matching the basic end of a sentence, not including following space."
170 :group 'paragraphs
171 :type 'string
172 :version "22.1")
173 (put 'sentence-end-base 'safe-local-variable 'stringp)
175 (defun sentence-end ()
176 "Return the regexp describing the end of a sentence.
178 This function returns either the value of the variable `sentence-end'
179 if it is non-nil, or the default value constructed from the
180 variables `sentence-end-base', `sentence-end-double-space',
181 `sentence-end-without-period' and `sentence-end-without-space'.
183 The default value specifies that in order to be recognized as the
184 end of a sentence, the ending period, question mark, or exclamation point
185 must be followed by two spaces, with perhaps some closing delimiters
186 in between. See Info node `(elisp)Standard Regexps'."
187 (or sentence-end
188 ;; We accept non-break space along with space.
189 (concat (if sentence-end-without-period "\\w[ \u00a0][ \u00a0]\\|")
190 "\\("
191 sentence-end-base
192 (if sentence-end-double-space
193 "\\($\\|[ \u00a0]$\\|\t\\|[ \u00a0][ \u00a0]\\)" "\\($\\|[\t \u00a0]\\)")
194 "\\|[" sentence-end-without-space "]+"
195 "\\)"
196 "[ \u00a0\t\n]*")))
198 (defcustom page-delimiter "^\014"
199 "Regexp describing line-beginnings that separate pages."
200 :group 'paragraphs
201 :type 'regexp)
202 (put 'page-delimiter 'safe-local-variable 'stringp)
204 (defcustom paragraph-ignore-fill-prefix nil
205 "Non-nil means the paragraph commands are not affected by `fill-prefix'.
206 This is desirable in modes where blank lines are the paragraph delimiters."
207 :group 'paragraphs
208 :type 'boolean)
209 (put 'paragraph-ignore-fill-prefix 'safe-local-variable 'booleanp)
211 (defun forward-paragraph (&optional arg)
212 "Move forward to end of paragraph.
213 With argument ARG, do it ARG times;
214 a negative argument ARG = -N means move backward N paragraphs.
216 A line which `paragraph-start' matches either separates paragraphs
217 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
218 A paragraph end is the beginning of a line which is not part of the paragraph
219 to which the end of the previous line belongs, or the end of the buffer.
220 Returns the count of paragraphs left to move."
221 (interactive "^p")
222 (or arg (setq arg 1))
223 (let* ((opoint (point))
224 (fill-prefix-regexp
225 (and fill-prefix (not (equal fill-prefix ""))
226 (not paragraph-ignore-fill-prefix)
227 (regexp-quote fill-prefix)))
228 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
229 ;; These regexps shouldn't be anchored, because we look for them
230 ;; starting at the left-margin. This allows paragraph commands to
231 ;; work normally with indented text.
232 ;; This hack will not find problem cases like "whatever\\|^something".
233 (parstart (if (and (not (equal "" paragraph-start))
234 (equal ?^ (aref paragraph-start 0)))
235 (substring paragraph-start 1)
236 paragraph-start))
237 (parsep (if (and (not (equal "" paragraph-separate))
238 (equal ?^ (aref paragraph-separate 0)))
239 (substring paragraph-separate 1)
240 paragraph-separate))
241 (parsep
242 (if fill-prefix-regexp
243 (concat parsep "\\|"
244 fill-prefix-regexp "[ \t]*$")
245 parsep))
246 ;; This is used for searching.
247 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
248 start found-start)
249 (while (and (< arg 0) (not (bobp)))
250 (if (and (not (looking-at parsep))
251 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
252 (looking-at parsep))
253 (setq arg (1+ arg))
254 (setq start (point))
255 ;; Move back over paragraph-separating lines.
256 (forward-char -1) (beginning-of-line)
257 (while (and (not (bobp))
258 (progn (move-to-left-margin)
259 (looking-at parsep)))
260 (forward-line -1))
261 (if (bobp)
263 (setq arg (1+ arg))
264 ;; Go to end of the previous (non-separating) line.
265 (end-of-line)
266 ;; Search back for line that starts or separates paragraphs.
267 (if (if fill-prefix-regexp
268 ;; There is a fill prefix; it overrides parstart.
269 (let (multiple-lines)
270 (while (and (progn (beginning-of-line) (not (bobp)))
271 (progn (move-to-left-margin)
272 (not (looking-at parsep)))
273 (looking-at fill-prefix-regexp))
274 (unless (= (point) start)
275 (setq multiple-lines t))
276 (forward-line -1))
277 (move-to-left-margin)
278 ;; This deleted code caused a long hanging-indent line
279 ;; not to be filled together with the following lines.
280 ;; ;; Don't move back over a line before the paragraph
281 ;; ;; which doesn't start with fill-prefix
282 ;; ;; unless that is the only line we've moved over.
283 ;; (and (not (looking-at fill-prefix-regexp))
284 ;; multiple-lines
285 ;; (forward-line 1))
286 (not (bobp)))
287 (while (and (re-search-backward sp-parstart nil 1)
288 (setq found-start t)
289 ;; Found a candidate, but need to check if it is a
290 ;; REAL parstart.
291 (progn (setq start (point))
292 (move-to-left-margin)
293 (not (looking-at parsep)))
294 (not (and (looking-at parstart)
295 (or (not use-hard-newlines)
296 (bobp)
297 (get-text-property
298 (1- start) 'hard)))))
299 (setq found-start nil)
300 (goto-char start))
301 found-start)
302 ;; Found one.
303 (progn
304 ;; Move forward over paragraph separators.
305 ;; We know this cannot reach the place we started
306 ;; because we know we moved back over a non-separator.
307 (while (and (not (eobp))
308 (progn (move-to-left-margin)
309 (looking-at parsep)))
310 (forward-line 1))
311 ;; If line before paragraph is just margin, back up to there.
312 (end-of-line 0)
313 (if (> (current-column) (current-left-margin))
314 (forward-char 1)
315 (skip-chars-backward " \t")
316 (if (not (bolp))
317 (forward-line 1))))
318 ;; No starter or separator line => use buffer beg.
319 (goto-char (point-min))))))
321 (while (and (> arg 0) (not (eobp)))
322 ;; Move forward over separator lines...
323 (while (and (not (eobp))
324 (progn (move-to-left-margin) (not (eobp)))
325 (looking-at parsep))
326 (forward-line 1))
327 (unless (eobp) (setq arg (1- arg)))
328 ;; ... and one more line.
329 (forward-line 1)
330 (if fill-prefix-regexp
331 ;; There is a fill prefix; it overrides parstart.
332 (while (and (not (eobp))
333 (progn (move-to-left-margin) (not (eobp)))
334 (not (looking-at parsep))
335 (looking-at fill-prefix-regexp))
336 (forward-line 1))
337 (while (and (re-search-forward sp-parstart nil 1)
338 (progn (setq start (match-beginning 0))
339 (goto-char start)
340 (not (eobp)))
341 (progn (move-to-left-margin)
342 (not (looking-at parsep)))
343 (or (not (looking-at parstart))
344 (and use-hard-newlines
345 (not (get-text-property (1- start) 'hard)))))
346 (forward-char 1))
347 (if (< (point) (point-max))
348 (goto-char start))))
349 (constrain-to-field nil opoint t)
350 ;; Return the number of steps that could not be done.
351 arg))
353 (defun backward-paragraph (&optional arg)
354 "Move backward to start of paragraph.
355 With argument ARG, do it ARG times;
356 a negative argument ARG = -N means move forward N paragraphs.
358 A paragraph start is the beginning of a line which is a
359 `paragraph-start' or which is ordinary text and follows a
360 `paragraph-separate'ing line; except: if the first real line of a
361 paragraph is preceded by a blank line, the paragraph starts at that
362 blank line.
364 See `forward-paragraph' for more information."
365 (interactive "^p")
366 (or arg (setq arg 1))
367 (forward-paragraph (- arg)))
369 (defun mark-paragraph (&optional arg allow-extend)
370 "Put point at beginning of this paragraph, mark at end.
371 The paragraph marked is the one that contains point or follows point.
373 With argument ARG, puts mark at end of a following paragraph, so that
374 the number of paragraphs marked equals ARG.
376 If ARG is negative, point is put at end of this paragraph, mark is put
377 at beginning of this or a previous paragraph.
379 Interactively, if this command is repeated
380 or (in Transient Mark mode) if the mark is active,
381 it marks the next ARG paragraphs after the ones already marked."
382 (interactive "p\np")
383 (unless arg (setq arg 1))
384 (when (zerop arg)
385 (error "Cannot mark zero paragraphs"))
386 (cond ((and allow-extend
387 (or (and (eq last-command this-command) (mark t))
388 (and transient-mark-mode mark-active)))
389 (set-mark
390 (save-excursion
391 (goto-char (mark))
392 (forward-paragraph arg)
393 (point))))
395 (forward-paragraph arg)
396 (push-mark nil t t)
397 (backward-paragraph arg))))
399 (defun kill-paragraph (arg)
400 "Kill forward to end of paragraph.
401 With arg N, kill forward to Nth end of paragraph;
402 negative arg -N means kill backward to Nth start of paragraph."
403 (interactive "p")
404 (kill-region (point) (progn (forward-paragraph arg) (point))))
406 (defun backward-kill-paragraph (arg)
407 "Kill back to start of paragraph.
408 With arg N, kill back to Nth start of paragraph;
409 negative arg -N means kill forward to Nth end of paragraph."
410 (interactive "p")
411 (kill-region (point) (progn (backward-paragraph arg) (point))))
413 (defun transpose-paragraphs (arg)
414 "Interchange the current paragraph with the next one.
415 With prefix argument ARG a non-zero integer, moves the current
416 paragraph past ARG paragraphs, leaving point after the current paragraph.
417 If ARG is positive, moves the current paragraph forwards, if
418 ARG is negative moves it backwards. If ARG is zero, exchanges
419 the current paragraph with the one containing the mark."
420 (interactive "*p")
421 (transpose-subr 'forward-paragraph arg))
423 (defun start-of-paragraph-text ()
424 (let ((opoint (point)) npoint)
425 (forward-paragraph -1)
426 (setq npoint (point))
427 (skip-chars-forward " \t\n")
428 ;; If the range of blank lines found spans the original start point,
429 ;; try again from the beginning of it.
430 ;; Must be careful to avoid infinite loop
431 ;; when following a single return at start of buffer.
432 (if (and (>= (point) opoint) (< npoint opoint))
433 (progn
434 (goto-char npoint)
435 (if (> npoint (point-min))
436 (start-of-paragraph-text))))))
438 (defun end-of-paragraph-text ()
439 (let ((opoint (point)))
440 (forward-paragraph 1)
441 (if (eq (preceding-char) ?\n) (forward-char -1))
442 (if (<= (point) opoint)
443 (progn
444 (forward-char 1)
445 (if (< (point) (point-max))
446 (end-of-paragraph-text))))))
448 (defun forward-sentence (&optional arg)
449 "Move forward to next end of sentence. With argument, repeat.
450 With negative argument, move backward repeatedly to start of sentence.
452 The variable `sentence-end' is a regular expression that matches ends of
453 sentences. Also, every paragraph boundary terminates sentences as well."
454 (interactive "^p")
455 (or arg (setq arg 1))
456 (let ((opoint (point))
457 (sentence-end (sentence-end)))
458 (while (< arg 0)
459 (let ((pos (point))
460 ;; We used to use (start-of-paragraph-text), but this can
461 ;; prevent sentence-end from matching if it is anchored at
462 ;; BOL and the paragraph starts indented.
463 (par-beg (save-excursion (backward-paragraph) (point))))
464 (if (and (re-search-backward sentence-end par-beg t)
465 (or (< (match-end 0) pos)
466 (re-search-backward sentence-end par-beg t)))
467 (goto-char (match-end 0))
468 (goto-char par-beg)))
469 (setq arg (1+ arg)))
470 (while (> arg 0)
471 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
472 (if (re-search-forward sentence-end par-end t)
473 (skip-chars-backward " \t\n")
474 (goto-char par-end)))
475 (setq arg (1- arg)))
476 (constrain-to-field nil opoint t)))
478 (defun repunctuate-sentences ()
479 "Put two spaces at the end of sentences from point to the end of buffer.
480 It works using `query-replace-regexp'."
481 (interactive)
482 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
483 "\\1\\2\\3 "))
486 (defun backward-sentence (&optional arg)
487 "Move backward to start of sentence. With arg, do it arg times.
488 See `forward-sentence' for more information."
489 (interactive "^p")
490 (or arg (setq arg 1))
491 (forward-sentence (- arg)))
493 (defun kill-sentence (&optional arg)
494 "Kill from point to end of sentence.
495 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
496 (interactive "p")
497 (kill-region (point) (progn (forward-sentence arg) (point))))
499 (defun backward-kill-sentence (&optional arg)
500 "Kill back from point to start of sentence.
501 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
502 (interactive "p")
503 (kill-region (point) (progn (backward-sentence arg) (point))))
505 (defun mark-end-of-sentence (arg)
506 "Put mark at end of sentence. Arg works as in `forward-sentence'.
507 If this command is repeated, it marks the next ARG sentences after the
508 ones already marked."
509 (interactive "p")
510 (push-mark
511 (save-excursion
512 (if (and (eq last-command this-command) (mark t))
513 (goto-char (mark)))
514 (forward-sentence arg)
515 (point))
516 nil t))
518 (defun transpose-sentences (arg)
519 "Interchange the current sentence with the next one.
520 With prefix argument ARG a non-zero integer, moves the current
521 sentence past ARG sentences, leaving point after the current sentence.
522 If ARG is positive, moves the current sentence forwards, if
523 ARG is negative moves it backwards. If ARG is zero, exchanges
524 the current sentence with the one containing the mark."
525 (interactive "*p")
526 (transpose-subr 'forward-sentence arg))
528 ;; Local Variables:
529 ;; coding: utf-8
530 ;; End:
532 ;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
533 ;;; paragraphs.el ends here