entered into RCS
[emacs.git] / lisp / textmodes / paragraphs.el
blob66e48080bb4ade63f39209a44fe4408be2d2d3b6
1 ;;; paragraphs.el --- paragraph and sentence parsing.
3 ;; Copyright (C) 1985, 86, 87, 1991 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: wp
8 ;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Code:
26 (defconst paragraph-start "^[ \t\n\f]" "\
27 *Regexp for beginning of a line that starts OR separates paragraphs.")
29 (defconst paragraph-separate "^[ \t\f]*$" "\
30 *Regexp for beginning of a line that separates paragraphs.
31 If you change this, you may have to change paragraph-start also.")
33 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
34 *Regexp describing the end of a sentence.
35 All paragraph boundaries also end sentences, regardless.")
37 (defconst page-delimiter "^\014" "\
38 *Regexp describing line-beginnings that separate pages.")
40 (defvar paragraph-ignore-fill-prefix nil "\
41 Non-nil means the paragraph commands are not affected by `fill-prefix'.
42 This is desirable in modes where blank lines are the paragraph delimiters.")
45 (defun forward-paragraph (&optional arg)
46 "Move forward to end of paragraph.
47 With arg N, do it N times; negative arg -N means move forward N paragraphs.
49 A line which `paragraph-start' matches either separates paragraphs
50 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
51 A paragraph end is the beginning of a line which is not part of the paragraph
52 to which the end of the previous line belongs, or the end of the buffer."
53 (interactive "p")
54 (or arg (setq arg 1))
55 (let* ((fill-prefix-regexp
56 (and fill-prefix (not (equal fill-prefix ""))
57 (not paragraph-ignore-fill-prefix)
58 (regexp-quote fill-prefix)))
59 (paragraph-separate
60 (if fill-prefix-regexp
61 (concat paragraph-separate "\\|^"
62 fill-prefix-regexp "[ \t]*$")
63 paragraph-separate)))
64 (while (< arg 0)
65 (if (and (not (looking-at paragraph-separate))
66 (re-search-backward "^\n" (max (1- (point)) (point-min)) t))
67 nil
68 (forward-char -1) (beginning-of-line)
69 (while (and (not (bobp)) (looking-at paragraph-separate))
70 (forward-line -1))
71 (end-of-line)
72 ;; Search back for line that starts or separates paragraphs.
73 (if (if fill-prefix-regexp
74 ;; There is a fill prefix; it overrides paragraph-start.
75 (progn
76 (while (progn (beginning-of-line)
77 (and (not (bobp))
78 (not (looking-at paragraph-separate))
79 (looking-at fill-prefix-regexp)))
80 (forward-line -1))
81 (not (bobp)))
82 (re-search-backward paragraph-start nil t))
83 ;; Found one.
84 (progn
85 (while (and (not (eobp)) (looking-at paragraph-separate))
86 (forward-line 1))
87 (if (eq (char-after (- (point) 2)) ?\n)
88 (forward-line -1)))
89 ;; No starter or separator line => use buffer beg.
90 (goto-char (point-min))))
91 (setq arg (1+ arg)))
92 (while (> arg 0)
93 (beginning-of-line)
94 (while (prog1 (and (not (eobp))
95 (looking-at paragraph-separate))
96 (forward-line 1)))
97 (if fill-prefix-regexp
98 ;; There is a fill prefix; it overrides paragraph-start.
99 (while (and (not (eobp))
100 (not (looking-at paragraph-separate))
101 (looking-at fill-prefix-regexp))
102 (forward-line 1))
103 (if (re-search-forward paragraph-start nil t)
104 (goto-char (match-beginning 0))
105 (goto-char (point-max))))
106 (setq arg (1- arg)))))
108 (defun backward-paragraph (&optional arg)
109 "Move backward to start of paragraph.
110 With arg N, do it N times; negative arg -N means move forward N paragraphs.
112 A paragraph start is the beginning of a line which is a
113 `first-line-of-paragraph' or which is ordinary text and follows a
114 paragraph-separating line; except: if the first real line of a
115 paragraph is preceded by a blank line, the paragraph starts at that
116 blank line.
118 See `forward-paragraph' for more information."
119 (interactive "p")
120 (or arg (setq arg 1))
121 (forward-paragraph (- arg)))
123 (defun mark-paragraph ()
124 "Put point at beginning of this paragraph, mark at end.
125 The paragraph marked is the one that contains point or follows point."
126 (interactive)
127 (forward-paragraph 1)
128 (push-mark nil t)
129 (backward-paragraph 1))
131 (defun kill-paragraph (arg)
132 "Kill forward to end of paragraph.
133 With arg N, kill forward to Nth end of paragraph;
134 negative arg -N means kill backward to Nth start of paragraph."
135 (interactive "p")
136 (kill-region (point) (progn (forward-paragraph arg) (point))))
138 (defun backward-kill-paragraph (arg)
139 "Kill back to start of paragraph.
140 With arg N, kill back to Nth start of paragraph;
141 negative arg -N means kill forward to Nth end of paragraph."
142 (interactive "p")
143 (kill-region (point) (progn (backward-paragraph arg) (point))))
145 (defun transpose-paragraphs (arg)
146 "Interchange this (or next) paragraph with previous one."
147 (interactive "*p")
148 (transpose-subr 'forward-paragraph arg))
150 (defun start-of-paragraph-text ()
151 (let ((opoint (point)) npoint)
152 (forward-paragraph -1)
153 (setq npoint (point))
154 (skip-chars-forward " \t\n")
155 ;; If the range of blank lines found spans the original start point,
156 ;; try again from the beginning of it.
157 ;; Must be careful to avoid infinite loop
158 ;; when following a single return at start of buffer.
159 (if (and (>= (point) opoint) (< npoint opoint))
160 (progn
161 (goto-char npoint)
162 (if (> npoint (point-min))
163 (start-of-paragraph-text))))))
165 (defun end-of-paragraph-text ()
166 (let ((opoint (point)))
167 (forward-paragraph 1)
168 (if (eq (preceding-char) ?\n) (forward-char -1))
169 (if (<= (point) opoint)
170 (progn
171 (forward-char 1)
172 (if (< (point) (point-max))
173 (end-of-paragraph-text))))))
175 (defun forward-sentence (&optional arg)
176 "Move forward to next`sentence-end'. With argument, repeat.
177 With negative argument, move backward repeatedly to `sentence-beginning'.
179 The variable `sentence-end' is a regular expression that matches ends of
180 sentences. Also, every paragraph boundary terminates sentences as well."
181 (interactive "p")
182 (or arg (setq arg 1))
183 (while (< arg 0)
184 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
185 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
186 (goto-char (1- (match-end 0)))
187 (goto-char par-beg)))
188 (setq arg (1+ arg)))
189 (while (> arg 0)
190 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
191 (if (re-search-forward sentence-end par-end t)
192 (skip-chars-backward " \t\n")
193 (goto-char par-end)))
194 (setq arg (1- arg))))
196 (defun backward-sentence (&optional arg)
197 "Move backward to start of sentence. With arg, do it arg times.
198 See `forward-sentence' for more information."
199 (interactive "p")
200 (or arg (setq arg 1))
201 (forward-sentence (- arg)))
203 (defun kill-sentence (&optional arg)
204 "Kill from point to end of sentence.
205 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
206 (interactive "*p")
207 (let ((beg (point)))
208 (forward-sentence arg)
209 (kill-region beg (point))))
211 (defun backward-kill-sentence (&optional arg)
212 "Kill back from point to start of sentence.
213 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
214 (interactive "*p")
215 (let ((beg (point)))
216 (backward-sentence arg)
217 (kill-region beg (point))))
219 (defun mark-end-of-sentence (arg)
220 "Put mark at end of sentence. Arg works as in `forward-sentence'."
221 (interactive "p")
222 (push-mark
223 (save-excursion
224 (forward-sentence arg)
225 (point))))
227 (defun transpose-sentences (arg)
228 "Interchange this (next) and previous sentence."
229 (interactive "*p")
230 (transpose-subr 'forward-sentence arg))
232 ;;; paragraphs.el ends here