Some fixes to follow coding conventions in files maintained by FSF.
[bpt/emacs.git] / lisp / textmodes / paragraphs.el
1 ;;; paragraphs.el --- paragraph and sentence parsing
2
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96, 1997, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: wp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This package provides the paragraph-oriented commands documented in the
29 ;; Emacs manual.
30
31 ;;; Code:
32
33 (defgroup paragraphs nil
34 "Paragraph and sentence parsing."
35 :group 'editing)
36
37 ;; It isn't useful to use defcustom for this variable
38 ;; because it is always buffer-local.
39 (defvar use-hard-newlines nil
40 "Non-nil means to distinguish hard and soft newlines.
41 See also the documentation for the function `use-hard-newlines'.")
42 (make-variable-buffer-local 'use-hard-newlines)
43
44 (defun use-hard-newlines (&optional arg insert)
45 "Minor mode to distinguish hard and soft newlines.
46 When active, the functions `newline' and `open-line' add the
47 text-property `hard' to newlines that they insert, and a line is
48 only considered as a candidate to match `paragraph-start' or
49 `paragraph-separate' if it follows a hard newline.
50
51 Prefix argument says to turn mode on if positive, off if negative.
52 When the mode is turned on, if there are newlines in the buffer but no hard
53 newlines, ask the user whether to mark as hard any newlines preceeding a
54 `paragraph-start' line. From a program, second arg INSERT specifies whether
55 to do this; it can be `never' to change nothing, t or `always' to force
56 marking, `guess' to try to do the right thing with no questions, nil
57 or anything else to ask the user.
58
59 Newlines not marked hard are called \"soft\", and are always internal
60 to paragraphs. The fill functions insert and delete only soft newlines."
61 (interactive (list current-prefix-arg nil))
62 (if (or (<= (prefix-numeric-value arg) 0)
63 (and use-hard-newlines (null arg)))
64 ;; Turn mode off
65 (setq use-hard-newlines nil)
66 ;; Turn mode on
67 ;; Intuit hard newlines --
68 ;; mark as hard any newlines preceding a paragraph-start line.
69 (if (or (eq insert t) (eq insert 'always)
70 (and (not (eq 'never insert))
71 (not use-hard-newlines)
72 (not (text-property-any (point-min) (point-max) 'hard t))
73 (save-excursion
74 (goto-char (point-min))
75 (search-forward "\n" nil t))
76 (or (eq insert 'guess)
77 (y-or-n-p "Make newlines between paragraphs hard? "))))
78 (save-excursion
79 (goto-char (point-min))
80 (while (search-forward "\n" nil t)
81 (let ((pos (point)))
82 (move-to-left-margin)
83 (if (looking-at paragraph-start)
84 (progn
85 (set-hard-newline-properties (1- pos) pos)
86 ;; If paragraph-separate, newline after it is hard too.
87 (if (looking-at paragraph-separate)
88 (progn
89 (end-of-line)
90 (if (not (eobp))
91 (set-hard-newline-properties
92 (point) (1+ (point))))))))))))
93 (setq use-hard-newlines t)))
94
95 (defcustom paragraph-start "[ \t\n\f]" "\
96 *Regexp for beginning of a line that starts OR separates paragraphs.
97 This regexp should match lines that separate paragraphs
98 and should also match lines that start a paragraph
99 \(and are part of that paragraph).
100
101 This is matched against the text at the left margin, which is not necessarily
102 the beginning of the line, so it should never use \"^\" as an anchor. This
103 ensures that the paragraph functions will work equally well within a region
104 of text indented by a margin setting.
105
106 The variable `paragraph-separate' specifies how to distinguish
107 lines that start paragraphs from lines that separate them.
108
109 If the variable `use-hard-newlines' is non-nil, then only lines following a
110 hard newline are considered to match."
111 :group 'paragraphs
112 :type 'regexp)
113
114 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
115 ;; It is assumed that paragraph-separate is distinctive enough to be believed
116 ;; whenever it occurs, while it is reasonable to set paragraph-start to
117 ;; something very minimal, even including "." (which makes every hard newline
118 ;; start a new paragraph).
119
120 (defcustom paragraph-separate "[ \t\f]*$"
121 "*Regexp for beginning of a line that separates paragraphs.
122 If you change this, you may have to change `paragraph-start' also.
123
124 This is matched against the text at the left margin, which is not necessarily
125 the beginning of the line, so it should not use \"^\" as an anchor. This
126 ensures that the paragraph functions will work equally within a region of
127 text indented by a margin setting."
128 :group 'paragraphs
129 :type 'regexp)
130
131 (defcustom sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*")
132 "*Regexp describing the end of a sentence.
133 The value includes the whitespace following the sentence.
134 All paragraph boundaries also end sentences, regardless.
135
136 The default value specifies that in order to be recognized as the end
137 of a sentence, the ending period, question mark, or exclamation point
138 must be followed by two spaces, unless it's inside some sort of quotes
139 or parenthesis.
140
141 See also the variable `sentence-end-double-space', the variable
142 `sentence-end-without-period' and Info node `Sentences'."
143 :group 'paragraphs
144 :type 'regexp)
145
146 (defcustom page-delimiter "^\014"
147 "*Regexp describing line-beginnings that separate pages."
148 :group 'paragraphs
149 :type 'regexp)
150
151 (defcustom paragraph-ignore-fill-prefix nil
152 "*Non-nil means the paragraph commands are not affected by `fill-prefix'.
153 This is desirable in modes where blank lines are the paragraph delimiters."
154 :group 'paragraphs
155 :type 'boolean)
156
157 (defun forward-paragraph (&optional arg)
158 "Move forward to end of paragraph.
159 With argument ARG, do it ARG times;
160 a negative argument ARG = -N means move backward N paragraphs.
161
162 A line which `paragraph-start' matches either separates paragraphs
163 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
164 A paragraph end is the beginning of a line which is not part of the paragraph
165 to which the end of the previous line belongs, or the end of the buffer."
166 (interactive "p")
167 (or arg (setq arg 1))
168 (let* ((opoint (point))
169 (fill-prefix-regexp
170 (and fill-prefix (not (equal fill-prefix ""))
171 (not paragraph-ignore-fill-prefix)
172 (regexp-quote fill-prefix)))
173 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
174 ;; These regexps shouldn't be anchored, because we look for them
175 ;; starting at the left-margin. This allows paragraph commands to
176 ;; work normally with indented text.
177 ;; This hack will not find problem cases like "whatever\\|^something".
178 (paragraph-start (if (and (not (equal "" paragraph-start))
179 (equal ?^ (aref paragraph-start 0)))
180 (substring paragraph-start 1)
181 paragraph-start))
182 (paragraph-separate (if (and (not (equal "" paragraph-separate))
183 (equal ?^ (aref paragraph-separate 0)))
184 (substring paragraph-separate 1)
185 paragraph-separate))
186 (paragraph-separate
187 (if fill-prefix-regexp
188 (concat paragraph-separate "\\|"
189 fill-prefix-regexp "[ \t]*$")
190 paragraph-separate))
191 ;; This is used for searching.
192 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
193 start found-start)
194 (while (and (< arg 0) (not (bobp)))
195 (if (and (not (looking-at paragraph-separate))
196 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
197 (looking-at paragraph-separate))
198 nil
199 (setq start (point))
200 ;; Move back over paragraph-separating lines.
201 (forward-char -1) (beginning-of-line)
202 (while (and (not (bobp))
203 (progn (move-to-left-margin)
204 (looking-at paragraph-separate)))
205 (forward-line -1))
206 (if (bobp)
207 nil
208 ;; Go to end of the previous (non-separating) line.
209 (end-of-line)
210 ;; Search back for line that starts or separates paragraphs.
211 (if (if fill-prefix-regexp
212 ;; There is a fill prefix; it overrides paragraph-start.
213 (let (multiple-lines)
214 (while (and (progn (beginning-of-line) (not (bobp)))
215 (progn (move-to-left-margin)
216 (not (looking-at paragraph-separate)))
217 (looking-at fill-prefix-regexp))
218 (unless (= (point) start)
219 (setq multiple-lines t))
220 (forward-line -1))
221 (move-to-left-margin)
222 ;;; This deleted code caused a long hanging-indent line
223 ;;; not to be filled together with the following lines.
224 ;;; ;; Don't move back over a line before the paragraph
225 ;;; ;; which doesn't start with fill-prefix
226 ;;; ;; unless that is the only line we've moved over.
227 ;;; (and (not (looking-at fill-prefix-regexp))
228 ;;; multiple-lines
229 ;;; (forward-line 1))
230 (not (bobp)))
231 (while (and (re-search-backward sp-paragraph-start nil 1)
232 (setq found-start t)
233 ;; Found a candidate, but need to check if it is a
234 ;; REAL paragraph-start.
235 (progn (setq start (point))
236 (move-to-left-margin)
237 (not (looking-at paragraph-separate)))
238 (not (and (looking-at paragraph-start)
239 (not
240 (and use-hard-newlines
241 (not (bobp))
242 (not (get-text-property (1- start)
243 'hard)))))))
244 (setq found-start nil)
245 (goto-char start))
246 found-start)
247 ;; Found one.
248 (progn
249 ;; Move forward over paragraph separators.
250 ;; We know this cannot reach the place we started
251 ;; because we know we moved back over a non-separator.
252 (while (and (not (eobp))
253 (progn (move-to-left-margin)
254 (looking-at paragraph-separate)))
255 (forward-line 1))
256 ;; If line before paragraph is just margin, back up to there.
257 (end-of-line 0)
258 (if (> (current-column) (current-left-margin))
259 (forward-char 1)
260 (skip-chars-backward " \t")
261 (if (not (bolp))
262 (forward-line 1))))
263 ;; No starter or separator line => use buffer beg.
264 (goto-char (point-min)))))
265 (setq arg (1+ arg)))
266 (while (and (> arg 0) (not (eobp)))
267 ;; Move forward over separator lines, and one more line.
268 (while (prog1 (and (not (eobp))
269 (progn (move-to-left-margin) (not (eobp)))
270 (looking-at paragraph-separate))
271 (forward-line 1)))
272 (if fill-prefix-regexp
273 ;; There is a fill prefix; it overrides paragraph-start.
274 (while (and (not (eobp))
275 (progn (move-to-left-margin) (not (eobp)))
276 (not (looking-at paragraph-separate))
277 (looking-at fill-prefix-regexp))
278 (forward-line 1))
279 (while (and (re-search-forward sp-paragraph-start nil 1)
280 (progn (setq start (match-beginning 0))
281 (goto-char start)
282 (not (eobp)))
283 (progn (move-to-left-margin)
284 (not (looking-at paragraph-separate)))
285 (or (not (looking-at paragraph-start))
286 (and use-hard-newlines
287 (not (get-text-property (1- start) 'hard)))))
288 (forward-char 1))
289 (if (< (point) (point-max))
290 (goto-char start)))
291 (setq arg (1- arg)))
292 (constrain-to-field nil opoint t)))
293
294 (defun backward-paragraph (&optional arg)
295 "Move backward to start of paragraph.
296 With argument ARG, do it ARG times;
297 a negative argument ARG = -N means move forward N paragraphs.
298
299 A paragraph start is the beginning of a line which is a
300 `first-line-of-paragraph' or which is ordinary text and follows a
301 paragraph-separating line; except: if the first real line of a
302 paragraph is preceded by a blank line, the paragraph starts at that
303 blank line.
304
305 See `forward-paragraph' for more information."
306 (interactive "p")
307 (or arg (setq arg 1))
308 (forward-paragraph (- arg)))
309
310 (defun mark-paragraph ()
311 "Put point at beginning of this paragraph, mark at end.
312 The paragraph marked is the one that contains point or follows point."
313 (interactive)
314 (forward-paragraph 1)
315 (push-mark nil t t)
316 (backward-paragraph 1))
317
318 (defun kill-paragraph (arg)
319 "Kill forward to end of paragraph.
320 With arg N, kill forward to Nth end of paragraph;
321 negative arg -N means kill backward to Nth start of paragraph."
322 (interactive "p")
323 (kill-region (point) (progn (forward-paragraph arg) (point))))
324
325 (defun backward-kill-paragraph (arg)
326 "Kill back to start of paragraph.
327 With arg N, kill back to Nth start of paragraph;
328 negative arg -N means kill forward to Nth end of paragraph."
329 (interactive "p")
330 (kill-region (point) (progn (backward-paragraph arg) (point))))
331
332 (defun transpose-paragraphs (arg)
333 "Interchange this (or next) paragraph with previous one."
334 (interactive "*p")
335 (transpose-subr 'forward-paragraph arg))
336
337 (defun start-of-paragraph-text ()
338 (let ((opoint (point)) npoint)
339 (forward-paragraph -1)
340 (setq npoint (point))
341 (skip-chars-forward " \t\n")
342 ;; If the range of blank lines found spans the original start point,
343 ;; try again from the beginning of it.
344 ;; Must be careful to avoid infinite loop
345 ;; when following a single return at start of buffer.
346 (if (and (>= (point) opoint) (< npoint opoint))
347 (progn
348 (goto-char npoint)
349 (if (> npoint (point-min))
350 (start-of-paragraph-text))))))
351
352 (defun end-of-paragraph-text ()
353 (let ((opoint (point)))
354 (forward-paragraph 1)
355 (if (eq (preceding-char) ?\n) (forward-char -1))
356 (if (<= (point) opoint)
357 (progn
358 (forward-char 1)
359 (if (< (point) (point-max))
360 (end-of-paragraph-text))))))
361
362 (defun forward-sentence (&optional arg)
363 "Move forward to next `sentence-end'. With argument, repeat.
364 With negative argument, move backward repeatedly to `sentence-beginning'.
365
366 The variable `sentence-end' is a regular expression that matches ends of
367 sentences. Also, every paragraph boundary terminates sentences as well."
368 (interactive "p")
369 (or arg (setq arg 1))
370 (let ((opoint (point)))
371 (while (< arg 0)
372 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
373 (if (re-search-backward (concat "\\(" sentence-end "\\)[^ \t\n]")
374 par-beg t)
375 (goto-char (1- (match-end 0)))
376 (goto-char par-beg)))
377 (setq arg (1+ arg)))
378 (while (> arg 0)
379 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
380 (if (re-search-forward sentence-end par-end t)
381 (skip-chars-backward " \t\n")
382 (goto-char par-end)))
383 (setq arg (1- arg)))
384 (constrain-to-field nil opoint t)))
385
386 (defun backward-sentence (&optional arg)
387 "Move backward to start of sentence. With arg, do it arg times.
388 See `forward-sentence' for more information."
389 (interactive "p")
390 (or arg (setq arg 1))
391 (forward-sentence (- arg)))
392
393 (defun kill-sentence (&optional arg)
394 "Kill from point to end of sentence.
395 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
396 (interactive "p")
397 (kill-region (point) (progn (forward-sentence arg) (point))))
398
399 (defun backward-kill-sentence (&optional arg)
400 "Kill back from point to start of sentence.
401 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
402 (interactive "p")
403 (kill-region (point) (progn (backward-sentence arg) (point))))
404
405 (defun mark-end-of-sentence (arg)
406 "Put mark at end of sentence. Arg works as in `forward-sentence'."
407 (interactive "p")
408 (push-mark
409 (save-excursion
410 (forward-sentence arg)
411 (point))
412 nil t))
413
414 (defun transpose-sentences (arg)
415 "Interchange this (next) and previous sentence."
416 (interactive "*p")
417 (transpose-subr 'forward-sentence arg))
418
419 ;;; paragraphs.el ends here