1 ;;; paragraphs.el --- paragraph and sentence parsing.
3 ;; Copyright (C) 1985-1991 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 (defconst paragraph-start
"^[ \t\n\f]" "\
24 *Regexp for beginning of a line that starts OR separates paragraphs.")
27 (defconst paragraph-separate
"^[ \t\f]*$" "\
28 *Regexp for beginning of a line that separates paragraphs.
29 If you change this, you may have to change paragraph-start also.")
32 (defconst sentence-end
(purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
33 *Regexp describing the end of a sentence.
34 All paragraph boundaries also end sentences, regardless.")
37 (defconst page-delimiter
"^\014" "\
38 *Regexp describing line-beginnings that separate pages.")
41 (defvar paragraph-ignore-fill-prefix nil
"\
42 Non-nil means the paragraph commands are not affected by `fill-prefix'.
43 This is desirable in modes where blank lines are the paragraph delimiters.")
46 (defun forward-paragraph (&optional arg
)
47 "Move forward to end of paragraph.
48 With arg N, do it N times; negative arg -N means move forward N paragraphs.
50 A line which `paragraph-start' matches either separates paragraphs
51 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
52 A paragraph end is the beginning of a line which is not part of the paragraph
53 to which the end of the previous line belongs, or the end of the buffer."
56 (let* ((fill-prefix-regexp
57 (and fill-prefix
(not (equal fill-prefix
""))
58 (not paragraph-ignore-fill-prefix
)
59 (regexp-quote fill-prefix
)))
61 (if fill-prefix-regexp
62 (concat paragraph-separate
"\\|^"
63 fill-prefix-regexp
"[ \t]*$")
66 (if (and (not (looking-at paragraph-separate
))
67 (re-search-backward "^\n" (max (1- (point)) (point-min)) t
))
69 (forward-char -
1) (beginning-of-line)
70 (while (and (not (bobp)) (looking-at paragraph-separate
))
73 ;; Search back for line that starts or separates paragraphs.
74 (if (if fill-prefix-regexp
75 ;; There is a fill prefix; it overrides paragraph-start.
77 (while (progn (beginning-of-line)
79 (not (looking-at paragraph-separate
))
80 (looking-at fill-prefix-regexp
)))
83 (re-search-backward paragraph-start nil t
))
86 (while (and (not (eobp)) (looking-at paragraph-separate
))
88 (if (eq (char-after (- (point) 2)) ?
\n)
90 ;; No starter or separator line => use buffer beg.
91 (goto-char (point-min))))
95 (while (prog1 (and (not (eobp))
96 (looking-at paragraph-separate
))
98 (if fill-prefix-regexp
99 ;; There is a fill prefix; it overrides paragraph-start.
100 (while (and (not (eobp))
101 (not (looking-at paragraph-separate
))
102 (looking-at fill-prefix-regexp
))
104 (if (re-search-forward paragraph-start nil t
)
105 (goto-char (match-beginning 0))
106 (goto-char (point-max))))
107 (setq arg
(1- arg
)))))
109 (defun backward-paragraph (&optional arg
)
110 "Move backward to start of paragraph.
111 With arg N, do it N times; negative arg -N means move forward N paragraphs.
113 A paragraph start is the beginning of a line which is a
114 `first-line-of-paragraph' or which is ordinary text and follows a
115 paragraph-separating line; except: if the first real line of a
116 paragraph is preceded by a blank line, the paragraph starts at that
119 See `forward-paragraph' for more information."
121 (or arg
(setq arg
1))
122 (forward-paragraph (- arg
)))
124 (defun mark-paragraph ()
125 "Put point at beginning of this paragraph, mark at end.
126 The paragraph marked is the one that contains point or follows point."
128 (forward-paragraph 1)
130 (backward-paragraph 1))
132 (defun kill-paragraph (arg)
133 "Kill forward to end of paragraph.
134 With arg N, kill forward to Nth end of paragraph;
135 negative arg -N means kill backward to Nth start of paragraph."
137 (kill-region (point) (progn (forward-paragraph arg
) (point))))
139 (defun backward-kill-paragraph (arg)
140 "Kill back to start of paragraph.
141 With arg N, kill back to Nth start of paragraph;
142 negative arg -N means kill forward to Nth end of paragraph."
144 (kill-region (point) (progn (backward-paragraph arg
) (point))))
146 (defun transpose-paragraphs (arg)
147 "Interchange this (or next) paragraph with previous one."
149 (transpose-subr 'forward-paragraph arg
))
151 (defun start-of-paragraph-text ()
152 (let ((opoint (point)) npoint
)
153 (forward-paragraph -
1)
154 (setq npoint
(point))
155 (skip-chars-forward " \t\n")
156 ;; If the range of blank lines found spans the original start point,
157 ;; try again from the beginning of it.
158 ;; Must be careful to avoid infinite loop
159 ;; when following a single return at start of buffer.
160 (if (and (>= (point) opoint
) (< npoint opoint
))
163 (if (> npoint
(point-min))
164 (start-of-paragraph-text))))))
166 (defun end-of-paragraph-text ()
167 (let ((opoint (point)))
168 (forward-paragraph 1)
169 (if (eq (preceding-char) ?
\n) (forward-char -
1))
170 (if (<= (point) opoint
)
173 (if (< (point) (point-max))
174 (end-of-paragraph-text))))))
176 (defun forward-sentence (&optional arg
)
177 "Move forward to next`sentence-end'. With argument, repeat.
178 With negative argument, move backward repeatedly to `sentence-beginning'.
180 The variable `sentence-end' is a regular expression that matches ends of
181 sentences. Also, every paragraph boundary terminates sentences as well."
183 (or arg
(setq arg
1))
185 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
186 (if (re-search-backward (concat sentence-end
"[^ \t\n]") par-beg t
)
187 (goto-char (1- (match-end 0)))
188 (goto-char par-beg
)))
191 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
192 (if (re-search-forward sentence-end par-end t
)
193 (skip-chars-backward " \t\n")
194 (goto-char par-end
)))
195 (setq arg
(1- arg
))))
197 (defun backward-sentence (&optional arg
)
198 "Move backward to start of sentence. With arg, do it arg times.
199 See `forward-sentence' for more information."
201 (or arg
(setq arg
1))
202 (forward-sentence (- arg
)))
204 (defun kill-sentence (&optional arg
)
205 "Kill from point to end of sentence.
206 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
209 (forward-sentence arg
)
210 (kill-region beg
(point))))
212 (defun backward-kill-sentence (&optional arg
)
213 "Kill back from point to start of sentence.
214 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
217 (backward-sentence arg
)
218 (kill-region beg
(point))))
220 (defun mark-end-of-sentence (arg)
221 "Put mark at end of sentence. Arg works as in `forward-sentence'."
225 (forward-sentence arg
)
228 (defun transpose-sentences (arg)
229 "Interchange this (next) and previous sentence."
231 (transpose-subr 'forward-sentence arg
))
233 ;;; paragraphs.el ends here