1 ;;; paragraphs.el --- paragraph and sentence parsing.
3 ;; Copyright (C) 1985, 86, 87, 1991 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 (defconst paragraph-start
"^[ \t\n\f]" "\
27 *Regexp for beginning of a line that starts OR separates paragraphs.")
29 (defconst paragraph-separate
"^[ \t\f]*$" "\
30 *Regexp for beginning of a line that separates paragraphs.
31 If you change this, you may have to change paragraph-start also.")
33 (defconst sentence-end
(purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
34 *Regexp describing the end of a sentence.
35 All paragraph boundaries also end sentences, regardless.")
37 (defconst page-delimiter
"^\014" "\
38 *Regexp describing line-beginnings that separate pages.")
40 (defvar paragraph-ignore-fill-prefix nil
"\
41 Non-nil means the paragraph commands are not affected by `fill-prefix'.
42 This is desirable in modes where blank lines are the paragraph delimiters.")
45 (defun forward-paragraph (&optional arg
)
46 "Move forward to end of paragraph.
47 With arg N, do it N times; negative arg -N means move forward N paragraphs.
49 A line which `paragraph-start' matches either separates paragraphs
50 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
51 A paragraph end is the beginning of a line which is not part of the paragraph
52 to which the end of the previous line belongs, or the end of the buffer."
55 (let* ((fill-prefix-regexp
56 (and fill-prefix
(not (equal fill-prefix
""))
57 (not paragraph-ignore-fill-prefix
)
58 (regexp-quote fill-prefix
)))
60 (if fill-prefix-regexp
61 (concat paragraph-separate
"\\|^"
62 fill-prefix-regexp
"[ \t]*$")
65 (if (and (not (looking-at paragraph-separate
))
66 (re-search-backward "^\n" (max (1- (point)) (point-min)) t
))
68 (forward-char -
1) (beginning-of-line)
69 (while (and (not (bobp)) (looking-at paragraph-separate
))
72 ;; Search back for line that starts or separates paragraphs.
73 (if (if fill-prefix-regexp
74 ;; There is a fill prefix; it overrides paragraph-start.
76 (while (progn (beginning-of-line)
78 (not (looking-at paragraph-separate
))
79 (looking-at fill-prefix-regexp
)))
82 (re-search-backward paragraph-start nil t
))
85 (while (and (not (eobp)) (looking-at paragraph-separate
))
87 (if (eq (char-after (- (point) 2)) ?
\n)
89 ;; No starter or separator line => use buffer beg.
90 (goto-char (point-min))))
94 (while (prog1 (and (not (eobp))
95 (looking-at paragraph-separate
))
97 (if fill-prefix-regexp
98 ;; There is a fill prefix; it overrides paragraph-start.
99 (while (and (not (eobp))
100 (not (looking-at paragraph-separate
))
101 (looking-at fill-prefix-regexp
))
103 (if (re-search-forward paragraph-start nil t
)
104 (goto-char (match-beginning 0))
105 (goto-char (point-max))))
106 (setq arg
(1- arg
)))))
108 (defun backward-paragraph (&optional arg
)
109 "Move backward to start of paragraph.
110 With arg N, do it N times; negative arg -N means move forward N paragraphs.
112 A paragraph start is the beginning of a line which is a
113 `first-line-of-paragraph' or which is ordinary text and follows a
114 paragraph-separating line; except: if the first real line of a
115 paragraph is preceded by a blank line, the paragraph starts at that
118 See `forward-paragraph' for more information."
120 (or arg
(setq arg
1))
121 (forward-paragraph (- arg
)))
123 (defun mark-paragraph ()
124 "Put point at beginning of this paragraph, mark at end.
125 The paragraph marked is the one that contains point or follows point."
127 (forward-paragraph 1)
129 (backward-paragraph 1))
131 (defun kill-paragraph (arg)
132 "Kill forward to end of paragraph.
133 With arg N, kill forward to Nth end of paragraph;
134 negative arg -N means kill backward to Nth start of paragraph."
136 (kill-region (point) (progn (forward-paragraph arg
) (point))))
138 (defun backward-kill-paragraph (arg)
139 "Kill back to start of paragraph.
140 With arg N, kill back to Nth start of paragraph;
141 negative arg -N means kill forward to Nth end of paragraph."
143 (kill-region (point) (progn (backward-paragraph arg
) (point))))
145 (defun transpose-paragraphs (arg)
146 "Interchange this (or next) paragraph with previous one."
148 (transpose-subr 'forward-paragraph arg
))
150 (defun start-of-paragraph-text ()
151 (let ((opoint (point)) npoint
)
152 (forward-paragraph -
1)
153 (setq npoint
(point))
154 (skip-chars-forward " \t\n")
155 ;; If the range of blank lines found spans the original start point,
156 ;; try again from the beginning of it.
157 ;; Must be careful to avoid infinite loop
158 ;; when following a single return at start of buffer.
159 (if (and (>= (point) opoint
) (< npoint opoint
))
162 (if (> npoint
(point-min))
163 (start-of-paragraph-text))))))
165 (defun end-of-paragraph-text ()
166 (let ((opoint (point)))
167 (forward-paragraph 1)
168 (if (eq (preceding-char) ?
\n) (forward-char -
1))
169 (if (<= (point) opoint
)
172 (if (< (point) (point-max))
173 (end-of-paragraph-text))))))
175 (defun forward-sentence (&optional arg
)
176 "Move forward to next`sentence-end'. With argument, repeat.
177 With negative argument, move backward repeatedly to `sentence-beginning'.
179 The variable `sentence-end' is a regular expression that matches ends of
180 sentences. Also, every paragraph boundary terminates sentences as well."
182 (or arg
(setq arg
1))
184 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
185 (if (re-search-backward (concat sentence-end
"[^ \t\n]") par-beg t
)
186 (goto-char (1- (match-end 0)))
187 (goto-char par-beg
)))
190 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
191 (if (re-search-forward sentence-end par-end t
)
192 (skip-chars-backward " \t\n")
193 (goto-char par-end
)))
194 (setq arg
(1- arg
))))
196 (defun backward-sentence (&optional arg
)
197 "Move backward to start of sentence. With arg, do it arg times.
198 See `forward-sentence' for more information."
200 (or arg
(setq arg
1))
201 (forward-sentence (- arg
)))
203 (defun kill-sentence (&optional arg
)
204 "Kill from point to end of sentence.
205 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
208 (forward-sentence arg
)
209 (kill-region beg
(point))))
211 (defun backward-kill-sentence (&optional arg
)
212 "Kill back from point to start of sentence.
213 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
216 (backward-sentence arg
)
217 (kill-region beg
(point))))
219 (defun mark-end-of-sentence (arg)
220 "Put mark at end of sentence. Arg works as in `forward-sentence'."
224 (forward-sentence arg
)
227 (defun transpose-sentences (arg)
228 "Interchange this (next) and previous sentence."
230 (transpose-subr 'forward-sentence arg
))
232 ;;; paragraphs.el ends here