]> code.delx.au - gnu-emacs/blob - lisp/textmodes/paragraphs.el
(reporter-dont-compact-list): Doc fix.
[gnu-emacs] / lisp / textmodes / paragraphs.el
1 ;;; paragraphs.el --- paragraph and sentence parsing.
2
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: wp
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
23
24 ;;; Commentary:
25
26 ;; This package provides the paragraph-oriented commands documented in the
27 ;; Emacs manual.
28
29 ;;; Code:
30
31 (defvar use-hard-newlines nil
32 "Non-nil means to distinguish hard and soft newlines.
33 When this is non-nil, the functions `newline' and `open-line' add the
34 text-property `hard' to newlines that they insert. Also, a line is
35 only considered as a candidate to match `paragraph-start' or
36 `paragraph-separate' if it follows a hard newline. Newlines not
37 marked hard are called \"soft\", and are always internal to
38 paragraphs. The fill functions always insert soft newlines.
39
40 Each buffer has its own value of this variable.")
41 (make-variable-buffer-local 'use-hard-newlines)
42
43 (defconst paragraph-start "[ \t\n\f]" "\
44 *Regexp for beginning of a line that starts OR separates paragraphs.
45 This regexp should match lines that separate paragraphs
46 and should also match lines that start a paragraph
47 \(and are part of that paragraph).
48
49 This is matched against the text at the left margin, which is not necessarily
50 the beginning of the line, so it should never use \"^\" as an anchor. This
51 ensures that the paragraph functions will work equally well within a region
52 of text indented by a margin setting.
53
54 The variable `paragraph-separate' specifies how to distinguish
55 lines that start paragraphs from lines that separate them.
56
57 If the variable `use-hard-newlines' is nonnil, then only lines following a
58 hard newline are considered to match.")
59
60 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
61 ;; It is assumed that paragraph-separate is distinctive enough to be believed
62 ;; whenever it occurs, while it is reasonable to set paragraph-start to
63 ;; something very minimal, even including "." (which makes every hard newline
64 ;; start a new paragraph).
65
66 (defconst paragraph-separate "[ \t\f]*$" "\
67 *Regexp for beginning of a line that separates paragraphs.
68 If you change this, you may have to change paragraph-start also.
69
70 This is matched against the text at the left margin, which is not necessarily
71 the beginning of the line, so it should not use \"^\" as an anchor. This
72 ensures that the paragraph functions will work equally within a region of
73 text indented by a margin setting.")
74
75 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
76 *Regexp describing the end of a sentence.
77 All paragraph boundaries also end sentences, regardless.
78
79 In order to be recognized as the end of a sentence, the ending period,
80 question mark, or exclamation point must be followed by two spaces,
81 unless it's inside some sort of quotes or parenthesis.")
82
83 (defconst page-delimiter "^\014" "\
84 *Regexp describing line-beginnings that separate pages.")
85
86 (defvar paragraph-ignore-fill-prefix nil "\
87 Non-nil means the paragraph commands are not affected by `fill-prefix'.
88 This is desirable in modes where blank lines are the paragraph delimiters.")
89
90 (defun forward-paragraph (&optional arg)
91 "Move forward to end of paragraph.
92 With arg N, do it N times; negative arg -N means move backward N paragraphs.
93
94 A line which `paragraph-start' matches either separates paragraphs
95 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
96 A paragraph end is the beginning of a line which is not part of the paragraph
97 to which the end of the previous line belongs, or the end of the buffer."
98 (interactive "p")
99 (or arg (setq arg 1))
100 (let* ((fill-prefix-regexp
101 (and fill-prefix (not (equal fill-prefix ""))
102 (not paragraph-ignore-fill-prefix)
103 (regexp-quote fill-prefix)))
104 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
105 ;; These regexps shouldn't be anchored, because we look for them
106 ;; starting at the left-margin. This allows paragraph commands to
107 ;; work normally with indented text.
108 ;; This hack will not find problem cases like "whatever\\|^something".
109 (paragraph-start (if (and (not (equal "" paragraph-start))
110 (equal ?^ (aref paragraph-start 0)))
111 (substring paragraph-start 1)
112 paragraph-start))
113 (paragraph-separate (if (and (not (equal "" paragraph-start))
114 (equal ?^ (aref paragraph-separate 0)))
115 (substring paragraph-separate 1)
116 paragraph-separate))
117 (paragraph-separate
118 (if fill-prefix-regexp
119 (concat paragraph-separate "\\|"
120 fill-prefix-regexp "[ \t]*$")
121 paragraph-separate))
122 ;; This is used for searching.
123 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
124 start)
125 (while (and (< arg 0) (not (bobp)))
126 (if (and (not (looking-at paragraph-separate))
127 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
128 (looking-at paragraph-separate))
129 nil
130 (setq start (point))
131 ;; Move back over paragraph-separating lines.
132 (forward-char -1) (beginning-of-line)
133 (while (and (not (bobp))
134 (progn (move-to-left-margin)
135 (looking-at paragraph-separate)))
136 (forward-line -1))
137 (if (bobp)
138 nil
139 ;; Go to end of the previous (non-separating) line.
140 (end-of-line)
141 ;; Search back for line that starts or separates paragraphs.
142 (if (if fill-prefix-regexp
143 ;; There is a fill prefix; it overrides paragraph-start.
144 (let (multiple-lines)
145 (while (and (progn (beginning-of-line) (not (bobp)))
146 (progn (move-to-left-margin)
147 (not (looking-at paragraph-separate)))
148 (looking-at fill-prefix-regexp))
149 (if (not (= (point) start))
150 (setq multiple-lines t))
151 (forward-line -1))
152 (move-to-left-margin)
153 ;; Don't move back over a line before the paragraph
154 ;; which doesn't start with fill-prefix
155 ;; unless that is the only line we've moved over.
156 (and (not (looking-at fill-prefix-regexp))
157 multiple-lines
158 (forward-line 1))
159 (not (bobp)))
160 (while (and (re-search-backward sp-paragraph-start nil 1)
161 ;; Found a candidate, but need to check if it is a
162 ;; REAL paragraph-start.
163 (not (bobp))
164 (progn (setq start (point))
165 (move-to-left-margin)
166 (not (looking-at paragraph-separate)))
167 (or (not (looking-at paragraph-start))
168 (and use-hard-newlines
169 (not (get-text-property (1- start)
170 'hard)))))
171 (goto-char start))
172 (> (point) (point-min)))
173 ;; Found one.
174 (progn
175 ;; Move forward over paragraph separators.
176 ;; We know this cannot reach the place we started
177 ;; because we know we moved back over a non-separator.
178 (while (and (not (eobp))
179 (progn (move-to-left-margin)
180 (looking-at paragraph-separate)))
181 (forward-line 1))
182 ;; If line before paragraph is just margin, back up to there.
183 (end-of-line 0)
184 (if (> (current-column) (current-left-margin))
185 (forward-char 1)
186 (skip-chars-backward " \t")
187 (if (not (bolp))
188 (forward-line 1))))
189 ;; No starter or separator line => use buffer beg.
190 (goto-char (point-min)))))
191 (setq arg (1+ arg)))
192 (while (and (> arg 0) (not (eobp)))
193 (while (prog1 (and (not (eobp))
194 (progn (move-to-left-margin) (not (eobp)))
195 (looking-at paragraph-separate))
196 (forward-line 1)))
197 (if fill-prefix-regexp
198 ;; There is a fill prefix; it overrides paragraph-start.
199 (while (and (not (eobp))
200 (progn (move-to-left-margin) (not (eobp)))
201 (not (looking-at paragraph-separate))
202 (looking-at fill-prefix-regexp))
203 (forward-line 1))
204 (while (and (re-search-forward sp-paragraph-start nil 1)
205 (progn (setq start (match-beginning 0))
206 (goto-char start)
207 (not (eobp)))
208 (progn (move-to-left-margin)
209 (not (looking-at paragraph-separate)))
210 (or (not (looking-at paragraph-start))
211 (and use-hard-newlines
212 (not (get-text-property (1- start) 'hard)))))
213 (forward-char 1))
214 (if (< (point) (point-max))
215 (goto-char start)))
216 (setq arg (1- arg)))))
217
218 (defun backward-paragraph (&optional arg)
219 "Move backward to start of paragraph.
220 With arg N, do it N times; negative arg -N means move forward N paragraphs.
221
222 A paragraph start is the beginning of a line which is a
223 `first-line-of-paragraph' or which is ordinary text and follows a
224 paragraph-separating line; except: if the first real line of a
225 paragraph is preceded by a blank line, the paragraph starts at that
226 blank line.
227
228 See `forward-paragraph' for more information."
229 (interactive "p")
230 (or arg (setq arg 1))
231 (forward-paragraph (- arg)))
232
233 (defun mark-paragraph ()
234 "Put point at beginning of this paragraph, mark at end.
235 The paragraph marked is the one that contains point or follows point."
236 (interactive)
237 (forward-paragraph 1)
238 (push-mark nil t t)
239 (backward-paragraph 1))
240
241 (defun kill-paragraph (arg)
242 "Kill forward to end of paragraph.
243 With arg N, kill forward to Nth end of paragraph;
244 negative arg -N means kill backward to Nth start of paragraph."
245 (interactive "p")
246 (kill-region (point) (progn (forward-paragraph arg) (point))))
247
248 (defun backward-kill-paragraph (arg)
249 "Kill back to start of paragraph.
250 With arg N, kill back to Nth start of paragraph;
251 negative arg -N means kill forward to Nth end of paragraph."
252 (interactive "p")
253 (kill-region (point) (progn (backward-paragraph arg) (point))))
254
255 (defun transpose-paragraphs (arg)
256 "Interchange this (or next) paragraph with previous one."
257 (interactive "*p")
258 (transpose-subr 'forward-paragraph arg))
259
260 (defun start-of-paragraph-text ()
261 (let ((opoint (point)) npoint)
262 (forward-paragraph -1)
263 (setq npoint (point))
264 (skip-chars-forward " \t\n")
265 ;; If the range of blank lines found spans the original start point,
266 ;; try again from the beginning of it.
267 ;; Must be careful to avoid infinite loop
268 ;; when following a single return at start of buffer.
269 (if (and (>= (point) opoint) (< npoint opoint))
270 (progn
271 (goto-char npoint)
272 (if (> npoint (point-min))
273 (start-of-paragraph-text))))))
274
275 (defun end-of-paragraph-text ()
276 (let ((opoint (point)))
277 (forward-paragraph 1)
278 (if (eq (preceding-char) ?\n) (forward-char -1))
279 (if (<= (point) opoint)
280 (progn
281 (forward-char 1)
282 (if (< (point) (point-max))
283 (end-of-paragraph-text))))))
284
285 (defun forward-sentence (&optional arg)
286 "Move forward to next `sentence-end'. With argument, repeat.
287 With negative argument, move backward repeatedly to `sentence-beginning'.
288
289 The variable `sentence-end' is a regular expression that matches ends of
290 sentences. Also, every paragraph boundary terminates sentences as well."
291 (interactive "p")
292 (or arg (setq arg 1))
293 (while (< arg 0)
294 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
295 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
296 (goto-char (1- (match-end 0)))
297 (goto-char par-beg)))
298 (setq arg (1+ arg)))
299 (while (> arg 0)
300 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
301 (if (re-search-forward sentence-end par-end t)
302 (skip-chars-backward " \t\n")
303 (goto-char par-end)))
304 (setq arg (1- arg))))
305
306 (defun backward-sentence (&optional arg)
307 "Move backward to start of sentence. With arg, do it arg times.
308 See `forward-sentence' for more information."
309 (interactive "p")
310 (or arg (setq arg 1))
311 (forward-sentence (- arg)))
312
313 (defun kill-sentence (&optional arg)
314 "Kill from point to end of sentence.
315 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
316 (interactive "p")
317 (kill-region (point) (progn (forward-sentence arg) (point))))
318
319 (defun backward-kill-sentence (&optional arg)
320 "Kill back from point to start of sentence.
321 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
322 (interactive "p")
323 (kill-region (point) (progn (backward-sentence arg) (point))))
324
325 (defun mark-end-of-sentence (arg)
326 "Put mark at end of sentence. Arg works as in `forward-sentence'."
327 (interactive "p")
328 (push-mark
329 (save-excursion
330 (forward-sentence arg)
331 (point))
332 nil t))
333
334 (defun transpose-sentences (arg)
335 "Interchange this (next) and previous sentence."
336 (interactive "*p")
337 (transpose-subr 'forward-sentence arg))
338
339 ;;; paragraphs.el ends here