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 Free Software Foundation, Inc.
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 3, or (at your option)
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This package provides the paragraph-oriented commands documented in the
33 (defgroup paragraphs nil
34 "Paragraph and sentence parsing."
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."
57 (when use-hard-newlines
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
))
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? "))))
70 (goto-char (point-min))
71 (while (search-forward "\n" nil t
)
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
)
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."
101 ;;;###autoload(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."
119 ;;;###autoload(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'."
131 ;;;###autoload(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
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'."
143 ;;;###autoload(put 'sentence-end-without-period 'safe-local-variable 'booleanp)
145 (defcustom sentence-end-without-space
146 "\e$B!#!%!)!*\e$A!##.#?#!\e$(0!$!%!)!*\e$(G!$!%!)!*\e(B"
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'."
154 ;;;###autoload(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."
165 :type
'(choice regexp
(const :tag
"Use default value" nil
)))
166 ;;;###autoload(put 'sentence-end 'safe-local-variable 'string-or-null-p)
168 (defcustom sentence-end-base
"[.?!][]\"'\e$B!I\e$,1r}\e(B)}]*"
169 "Regexp matching the basic end of a sentence, not including following space."
173 ;;;###autoload(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'."
188 (concat (if sentence-end-without-period
"\\w \\|")
191 (if sentence-end-double-space
192 "\\($\\| $\\|\t\\| \\)" "\\($\\|[\t ]\\)")
193 "\\|[" sentence-end-without-space
"]+"
197 (defcustom page-delimiter
"^\014"
198 "Regexp describing line-beginnings that separate pages."
201 ;;;###autoload(put 'page-delimiter 'safe-local-variable 'stringp)
203 (defcustom paragraph-ignore-fill-prefix nil
204 "Non-nil means the paragraph commands are not affected by `fill-prefix'.
205 This is desirable in modes where blank lines are the paragraph delimiters."
208 ;;;###autoload(put 'paragraph-ignore-fill-prefix 'safe-local-variable 'booleanp)
210 (defun forward-paragraph (&optional arg
)
211 "Move forward to end of paragraph.
212 With argument ARG, do it ARG times;
213 a negative argument ARG = -N means move backward N paragraphs.
215 A line which `paragraph-start' matches either separates paragraphs
216 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
217 A paragraph end is the beginning of a line which is not part of the paragraph
218 to which the end of the previous line belongs, or the end of the buffer.
219 Returns the count of paragraphs left to move."
221 (or arg
(setq arg
1))
222 (let* ((opoint (point))
224 (and fill-prefix
(not (equal fill-prefix
""))
225 (not paragraph-ignore-fill-prefix
)
226 (regexp-quote fill-prefix
)))
227 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
228 ;; These regexps shouldn't be anchored, because we look for them
229 ;; starting at the left-margin. This allows paragraph commands to
230 ;; work normally with indented text.
231 ;; This hack will not find problem cases like "whatever\\|^something".
232 (parstart (if (and (not (equal "" paragraph-start
))
233 (equal ?^
(aref paragraph-start
0)))
234 (substring paragraph-start
1)
236 (parsep (if (and (not (equal "" paragraph-separate
))
237 (equal ?^
(aref paragraph-separate
0)))
238 (substring paragraph-separate
1)
241 (if fill-prefix-regexp
243 fill-prefix-regexp
"[ \t]*$")
245 ;; This is used for searching.
246 (sp-parstart (concat "^[ \t]*\\(?:" parstart
"\\|" parsep
"\\)"))
248 (while (and (< arg
0) (not (bobp)))
249 (if (and (not (looking-at parsep
))
250 (re-search-backward "^\n" (max (1- (point)) (point-min)) t
)
254 ;; Move back over paragraph-separating lines.
255 (forward-char -
1) (beginning-of-line)
256 (while (and (not (bobp))
257 (progn (move-to-left-margin)
258 (looking-at parsep
)))
263 ;; Go to end of the previous (non-separating) line.
265 ;; Search back for line that starts or separates paragraphs.
266 (if (if fill-prefix-regexp
267 ;; There is a fill prefix; it overrides parstart.
268 (let (multiple-lines)
269 (while (and (progn (beginning-of-line) (not (bobp)))
270 (progn (move-to-left-margin)
271 (not (looking-at parsep
)))
272 (looking-at fill-prefix-regexp
))
273 (unless (= (point) start
)
274 (setq multiple-lines t
))
276 (move-to-left-margin)
277 ;; This deleted code caused a long hanging-indent line
278 ;; not to be filled together with the following lines.
279 ;; ;; Don't move back over a line before the paragraph
280 ;; ;; which doesn't start with fill-prefix
281 ;; ;; unless that is the only line we've moved over.
282 ;; (and (not (looking-at fill-prefix-regexp))
286 (while (and (re-search-backward sp-parstart nil
1)
288 ;; Found a candidate, but need to check if it is a
290 (progn (setq start
(point))
291 (move-to-left-margin)
292 (not (looking-at parsep
)))
293 (not (and (looking-at parstart
)
294 (or (not use-hard-newlines
)
297 (1- start
) 'hard
)))))
298 (setq found-start nil
)
303 ;; Move forward over paragraph separators.
304 ;; We know this cannot reach the place we started
305 ;; because we know we moved back over a non-separator.
306 (while (and (not (eobp))
307 (progn (move-to-left-margin)
308 (looking-at parsep
)))
310 ;; If line before paragraph is just margin, back up to there.
312 (if (> (current-column) (current-left-margin))
314 (skip-chars-backward " \t")
317 ;; No starter or separator line => use buffer beg.
318 (goto-char (point-min))))))
320 (while (and (> arg
0) (not (eobp)))
321 ;; Move forward over separator lines...
322 (while (and (not (eobp))
323 (progn (move-to-left-margin) (not (eobp)))
326 (unless (eobp) (setq arg
(1- arg
)))
327 ;; ... and one more line.
329 (if fill-prefix-regexp
330 ;; There is a fill prefix; it overrides parstart.
331 (while (and (not (eobp))
332 (progn (move-to-left-margin) (not (eobp)))
333 (not (looking-at parsep
))
334 (looking-at fill-prefix-regexp
))
336 (while (and (re-search-forward sp-parstart nil
1)
337 (progn (setq start
(match-beginning 0))
340 (progn (move-to-left-margin)
341 (not (looking-at parsep
)))
342 (or (not (looking-at parstart
))
343 (and use-hard-newlines
344 (not (get-text-property (1- start
) 'hard
)))))
346 (if (< (point) (point-max))
348 (constrain-to-field nil opoint t
)
349 ;; Return the number of steps that could not be done.
352 (defun backward-paragraph (&optional arg
)
353 "Move backward to start of paragraph.
354 With argument ARG, do it ARG times;
355 a negative argument ARG = -N means move forward N paragraphs.
357 A paragraph start is the beginning of a line which is a
358 `first-line-of-paragraph' or which is ordinary text and follows a
359 paragraph-separating line; except: if the first real line of a
360 paragraph is preceded by a blank line, the paragraph starts at that
363 See `forward-paragraph' for more information."
365 (or arg
(setq arg
1))
366 (forward-paragraph (- arg
)))
368 (defun mark-paragraph (&optional arg allow-extend
)
369 "Put point at beginning of this paragraph, mark at end.
370 The paragraph marked is the one that contains point or follows point.
372 With argument ARG, puts mark at end of a following paragraph, so that
373 the number of paragraphs marked equals ARG.
375 If ARG is negative, point is put at end of this paragraph, mark is put
376 at beginning of this or a previous paragraph.
378 Interactively, if this command is repeated
379 or (in Transient Mark mode) if the mark is active,
380 it marks the next ARG paragraphs after the ones already marked."
382 (unless arg
(setq arg
1))
384 (error "Cannot mark zero paragraphs"))
385 (cond ((and allow-extend
386 (or (and (eq last-command this-command
) (mark t
))
387 (and transient-mark-mode mark-active
)))
391 (forward-paragraph arg
)
394 (forward-paragraph arg
)
396 (backward-paragraph arg
))))
398 (defun kill-paragraph (arg)
399 "Kill forward to end of paragraph.
400 With arg N, kill forward to Nth end of paragraph;
401 negative arg -N means kill backward to Nth start of paragraph."
403 (kill-region (point) (progn (forward-paragraph arg
) (point))))
405 (defun backward-kill-paragraph (arg)
406 "Kill back to start of paragraph.
407 With arg N, kill back to Nth start of paragraph;
408 negative arg -N means kill forward to Nth end of paragraph."
410 (kill-region (point) (progn (backward-paragraph arg
) (point))))
412 (defun transpose-paragraphs (arg)
413 "Interchange this (or next) paragraph with previous one."
415 (transpose-subr 'forward-paragraph arg
))
417 (defun start-of-paragraph-text ()
418 (let ((opoint (point)) npoint
)
419 (forward-paragraph -
1)
420 (setq npoint
(point))
421 (skip-chars-forward " \t\n")
422 ;; If the range of blank lines found spans the original start point,
423 ;; try again from the beginning of it.
424 ;; Must be careful to avoid infinite loop
425 ;; when following a single return at start of buffer.
426 (if (and (>= (point) opoint
) (< npoint opoint
))
429 (if (> npoint
(point-min))
430 (start-of-paragraph-text))))))
432 (defun end-of-paragraph-text ()
433 (let ((opoint (point)))
434 (forward-paragraph 1)
435 (if (eq (preceding-char) ?
\n) (forward-char -
1))
436 (if (<= (point) opoint
)
439 (if (< (point) (point-max))
440 (end-of-paragraph-text))))))
442 (defun forward-sentence (&optional arg
)
443 "Move forward to next `sentence-end'. With argument, repeat.
444 With negative argument, move backward repeatedly to `sentence-beginning'.
446 The variable `sentence-end' is a regular expression that matches ends of
447 sentences. Also, every paragraph boundary terminates sentences as well."
449 (or arg
(setq arg
1))
450 (let ((opoint (point))
451 (sentence-end (sentence-end)))
454 (par-beg (save-excursion (start-of-paragraph-text) (point))))
455 (if (and (re-search-backward sentence-end par-beg t
)
456 (or (< (match-end 0) pos
)
457 (re-search-backward sentence-end par-beg t
)))
458 (goto-char (match-end 0))
459 (goto-char par-beg
)))
462 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
463 (if (re-search-forward sentence-end par-end t
)
464 (skip-chars-backward " \t\n")
465 (goto-char par-end
)))
467 (constrain-to-field nil opoint t
)))
469 (defun repunctuate-sentences ()
470 "Put two spaces at the end of sentences from point to the end of buffer.
471 It works using `query-replace-regexp'."
473 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
477 (defun backward-sentence (&optional arg
)
478 "Move backward to start of sentence. With arg, do it arg times.
479 See `forward-sentence' for more information."
481 (or arg
(setq arg
1))
482 (forward-sentence (- arg
)))
484 (defun kill-sentence (&optional arg
)
485 "Kill from point to end of sentence.
486 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
488 (kill-region (point) (progn (forward-sentence arg
) (point))))
490 (defun backward-kill-sentence (&optional arg
)
491 "Kill back from point to start of sentence.
492 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
494 (kill-region (point) (progn (backward-sentence arg
) (point))))
496 (defun mark-end-of-sentence (arg)
497 "Put mark at end of sentence. Arg works as in `forward-sentence'.
498 If this command is repeated, it marks the next ARG sentences after the
499 ones already marked."
503 (if (and (eq last-command this-command
) (mark t
))
505 (forward-sentence arg
)
509 (defun transpose-sentences (arg)
510 "Interchange this (next) and previous sentence."
512 (transpose-subr 'forward-sentence arg
))
515 ;; coding: iso-2022-7bit
518 ;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
519 ;;; paragraphs.el ends here