1 ;; Paragraph and sentence parsing.
2 ;; Copyright (C) 1985 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 (defvar paragraph-ignore-fill-prefix nil
22 "Non-nil means the paragraph commands are not affected by `fill-prefix'.
23 This is desirable in modes where blank lines are the paragraph delimiters.")
25 (defun forward-paragraph (&optional arg
)
26 "Move forward to end of paragraph.
27 With arg N, do it N times; negative arg -N means move forward N paragraphs.
29 A line which `paragraph-start' matches either separates paragraphs
30 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
31 A paragraph end is the beginning of a line which is not part of the paragraph
32 to which the end of the previous line belongs, or the end of the buffer."
35 (let* ((fill-prefix-regexp
36 (and fill-prefix
(not (equal fill-prefix
""))
37 (not paragraph-ignore-fill-prefix
)
38 (regexp-quote fill-prefix
)))
40 (if fill-prefix-regexp
41 (concat paragraph-separate
"\\|^"
42 fill-prefix-regexp
"[ \t]*$")
45 (if (and (not (looking-at paragraph-separate
))
46 (re-search-backward "^\n" (max (1- (point)) (point-min)) t
))
48 (forward-char -
1) (beginning-of-line)
49 (while (and (not (bobp)) (looking-at paragraph-separate
))
52 ;; Search back for line that starts or separates paragraphs.
53 (if (if fill-prefix-regexp
54 ;; There is a fill prefix; it overrides paragraph-start.
56 (while (progn (beginning-of-line)
58 (not (looking-at paragraph-separate
))
59 (looking-at fill-prefix-regexp
)))
62 (re-search-backward paragraph-start nil t
))
65 (while (and (not (eobp)) (looking-at paragraph-separate
))
67 (if (eq (char-after (- (point) 2)) ?
\n)
69 ;; No starter or separator line => use buffer beg.
70 (goto-char (point-min))))
74 (while (prog1 (and (not (eobp))
75 (looking-at paragraph-separate
))
77 (if fill-prefix-regexp
78 ;; There is a fill prefix; it overrides paragraph-start.
79 (while (and (not (eobp))
80 (not (looking-at paragraph-separate
))
81 (looking-at fill-prefix-regexp
))
83 (if (re-search-forward paragraph-start nil t
)
84 (goto-char (match-beginning 0))
85 (goto-char (point-max))))
86 (setq arg
(1- arg
)))))
88 (defun backward-paragraph (&optional arg
)
89 "Move backward to start of paragraph.
90 With arg N, do it N times; negative arg -N means move forward N paragraphs.
92 A paragraph start is the beginning of a line which is a
93 `first-line-of-paragraph' or which is ordinary text and follows a
94 paragraph-separating line; except: if the first real line of a
95 paragraph is preceded by a blank line, the paragraph starts at that
98 See `forward-paragraph' for more information."
100 (or arg
(setq arg
1))
101 (forward-paragraph (- arg
)))
103 (defun mark-paragraph ()
104 "Put point at beginning of this paragraph, mark at end.
105 The paragraph marked is the one that contains point or follows point."
107 (forward-paragraph 1)
109 (backward-paragraph 1))
111 (defun kill-paragraph (arg)
112 "Kill forward to end of paragraph.
113 With arg N, kill forward to Nth end of paragraph;
114 negative arg -N means kill backward to Nth start of paragraph."
116 (kill-region (point) (progn (forward-paragraph arg
) (point))))
118 (defun backward-kill-paragraph (arg)
119 "Kill back to start of paragraph.
120 With arg N, kill back to Nth start of paragraph;
121 negative arg -N means kill forward to Nth end of paragraph."
123 (kill-region (point) (progn (backward-paragraph arg
) (point))))
125 (defun transpose-paragraphs (arg)
126 "Interchange this (or next) paragraph with previous one."
128 (transpose-subr 'forward-paragraph arg
))
130 (defun start-of-paragraph-text ()
131 (let ((opoint (point)) npoint
)
132 (forward-paragraph -
1)
133 (setq npoint
(point))
134 (skip-chars-forward " \t\n")
135 ;; If the range of blank lines found spans the original start point,
136 ;; try again from the beginning of it.
137 ;; Must be careful to avoid infinite loop
138 ;; when following a single return at start of buffer.
139 (if (and (>= (point) opoint
) (< npoint opoint
))
142 (if (> npoint
(point-min))
143 (start-of-paragraph-text))))))
145 (defun end-of-paragraph-text ()
146 (let ((opoint (point)))
147 (forward-paragraph 1)
148 (if (eq (preceding-char) ?
\n) (forward-char -
1))
149 (if (<= (point) opoint
)
152 (if (< (point) (point-max))
153 (end-of-paragraph-text))))))
155 (defun forward-sentence (&optional arg
)
156 "Move forward to next`sentence-end'. With argument, repeat.
157 With negative argument, move backward repeatedly to `sentence-beginning'.
159 The variable `sentence-end' is a regular expression that matches ends of
160 sentences. Also, every paragraph boundary terminates sentences as well."
162 (or arg
(setq arg
1))
164 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
165 (if (re-search-backward (concat sentence-end
"[^ \t\n]") par-beg t
)
166 (goto-char (1- (match-end 0)))
167 (goto-char par-beg
)))
170 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
171 (if (re-search-forward sentence-end par-end t
)
172 (skip-chars-backward " \t\n")
173 (goto-char par-end
)))
174 (setq arg
(1- arg
))))
176 (defun backward-sentence (&optional arg
)
177 "Move backward to start of sentence. With arg, do it arg times.
178 See `forward-sentence' for more information."
180 (or arg
(setq arg
1))
181 (forward-sentence (- arg
)))
183 (defun kill-sentence (&optional arg
)
184 "Kill from point to end of sentence.
185 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
188 (forward-sentence arg
)
189 (kill-region beg
(point))))
191 (defun backward-kill-sentence (&optional arg
)
192 "Kill back from point to start of sentence.
193 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
196 (backward-sentence arg
)
197 (kill-region beg
(point))))
199 (defun mark-end-of-sentence (arg)
200 "Put mark at end of sentence. Arg works as in `forward-sentence'."
204 (forward-sentence arg
)
207 (defun transpose-sentences (arg)
208 "Interchange this (next) and previous sentence."
210 (transpose-subr 'forward-sentence arg
))