1 ;; Paragraph and sentence parsing.
2 ;; Copyright (C) 1985-1991 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;;;###autoload (defconst paragraph-start "^[ \t\n\f]"
22 ;;;###autoload "*Regexp for beginning of a line that starts OR separates paragraphs.")
24 ;;;###autoload (defconst paragraph-separate "^[ \t\f]*$"
25 ;;;###autoload "*Regexp for beginning of a line that separates paragraphs.
26 ;;;###autoload If you change this, you may have to change paragraph-start also.")
28 ;;;###autoload (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
29 ;;;###autoload *Regexp describing the end of a sentence.
30 ;;;###autoload All paragraph boundaries also end sentences, regardless.")
32 ;;;###autoload (defconst page-delimiter "^\014" "\
33 ;;;###autoload *Regexp describing line-beginnings that separate pages.")
35 ;;;###autoload (defvar paragraph-ignore-fill-prefix nil
36 ;;;###autoload "Non-nil means the paragraph commands are not affected by `fill-prefix'.
37 ;;;###autoload This is desirable in modes where blank lines are the paragraph delimiters.")
40 (defun forward-paragraph (&optional arg
)
41 "Move forward to end of paragraph.
42 With arg N, do it N times; negative arg -N means move forward N paragraphs.
44 A line which `paragraph-start' matches either separates paragraphs
45 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
46 A paragraph end is the beginning of a line which is not part of the paragraph
47 to which the end of the previous line belongs, or the end of the buffer."
50 (let* ((fill-prefix-regexp
51 (and fill-prefix
(not (equal fill-prefix
""))
52 (not paragraph-ignore-fill-prefix
)
53 (regexp-quote fill-prefix
)))
55 (if fill-prefix-regexp
56 (concat paragraph-separate
"\\|^"
57 fill-prefix-regexp
"[ \t]*$")
60 (if (and (not (looking-at paragraph-separate
))
61 (re-search-backward "^\n" (max (1- (point)) (point-min)) t
))
63 (forward-char -
1) (beginning-of-line)
64 (while (and (not (bobp)) (looking-at paragraph-separate
))
67 ;; Search back for line that starts or separates paragraphs.
68 (if (if fill-prefix-regexp
69 ;; There is a fill prefix; it overrides paragraph-start.
71 (while (progn (beginning-of-line)
73 (not (looking-at paragraph-separate
))
74 (looking-at fill-prefix-regexp
)))
77 (re-search-backward paragraph-start nil t
))
80 (while (and (not (eobp)) (looking-at paragraph-separate
))
82 (if (eq (char-after (- (point) 2)) ?
\n)
84 ;; No starter or separator line => use buffer beg.
85 (goto-char (point-min))))
89 (while (prog1 (and (not (eobp))
90 (looking-at paragraph-separate
))
92 (if fill-prefix-regexp
93 ;; There is a fill prefix; it overrides paragraph-start.
94 (while (and (not (eobp))
95 (not (looking-at paragraph-separate
))
96 (looking-at fill-prefix-regexp
))
98 (if (re-search-forward paragraph-start nil t
)
99 (goto-char (match-beginning 0))
100 (goto-char (point-max))))
101 (setq arg
(1- arg
)))))
103 (defun backward-paragraph (&optional arg
)
104 "Move backward to start of paragraph.
105 With arg N, do it N times; negative arg -N means move forward N paragraphs.
107 A paragraph start is the beginning of a line which is a
108 `first-line-of-paragraph' or which is ordinary text and follows a
109 paragraph-separating line; except: if the first real line of a
110 paragraph is preceded by a blank line, the paragraph starts at that
113 See `forward-paragraph' for more information."
115 (or arg
(setq arg
1))
116 (forward-paragraph (- arg
)))
118 (defun mark-paragraph ()
119 "Put point at beginning of this paragraph, mark at end.
120 The paragraph marked is the one that contains point or follows point."
122 (forward-paragraph 1)
124 (backward-paragraph 1))
126 (defun kill-paragraph (arg)
127 "Kill forward to end of paragraph.
128 With arg N, kill forward to Nth end of paragraph;
129 negative arg -N means kill backward to Nth start of paragraph."
131 (kill-region (point) (progn (forward-paragraph arg
) (point))))
133 (defun backward-kill-paragraph (arg)
134 "Kill back to start of paragraph.
135 With arg N, kill back to Nth start of paragraph;
136 negative arg -N means kill forward to Nth end of paragraph."
138 (kill-region (point) (progn (backward-paragraph arg
) (point))))
140 (defun transpose-paragraphs (arg)
141 "Interchange this (or next) paragraph with previous one."
143 (transpose-subr 'forward-paragraph arg
))
145 (defun start-of-paragraph-text ()
146 (let ((opoint (point)) npoint
)
147 (forward-paragraph -
1)
148 (setq npoint
(point))
149 (skip-chars-forward " \t\n")
150 ;; If the range of blank lines found spans the original start point,
151 ;; try again from the beginning of it.
152 ;; Must be careful to avoid infinite loop
153 ;; when following a single return at start of buffer.
154 (if (and (>= (point) opoint
) (< npoint opoint
))
157 (if (> npoint
(point-min))
158 (start-of-paragraph-text))))))
160 (defun end-of-paragraph-text ()
161 (let ((opoint (point)))
162 (forward-paragraph 1)
163 (if (eq (preceding-char) ?
\n) (forward-char -
1))
164 (if (<= (point) opoint
)
167 (if (< (point) (point-max))
168 (end-of-paragraph-text))))))
170 (defun forward-sentence (&optional arg
)
171 "Move forward to next`sentence-end'. With argument, repeat.
172 With negative argument, move backward repeatedly to `sentence-beginning'.
174 The variable `sentence-end' is a regular expression that matches ends of
175 sentences. Also, every paragraph boundary terminates sentences as well."
177 (or arg
(setq arg
1))
179 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
180 (if (re-search-backward (concat sentence-end
"[^ \t\n]") par-beg t
)
181 (goto-char (1- (match-end 0)))
182 (goto-char par-beg
)))
185 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
186 (if (re-search-forward sentence-end par-end t
)
187 (skip-chars-backward " \t\n")
188 (goto-char par-end
)))
189 (setq arg
(1- arg
))))
191 (defun backward-sentence (&optional arg
)
192 "Move backward to start of sentence. With arg, do it arg times.
193 See `forward-sentence' for more information."
195 (or arg
(setq arg
1))
196 (forward-sentence (- arg
)))
198 (defun kill-sentence (&optional arg
)
199 "Kill from point to end of sentence.
200 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
203 (forward-sentence arg
)
204 (kill-region beg
(point))))
206 (defun backward-kill-sentence (&optional arg
)
207 "Kill back from point to start of sentence.
208 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
211 (backward-sentence arg
)
212 (kill-region beg
(point))))
214 (defun mark-end-of-sentence (arg)
215 "Put mark at end of sentence. Arg works as in `forward-sentence'."
219 (forward-sentence arg
)
222 (defun transpose-sentences (arg)
223 "Interchange this (next) and previous sentence."
225 (transpose-subr 'forward-sentence arg
))