Initial revision
[bpt/emacs.git] / lisp / textmodes / paragraphs.el
CommitLineData
6594deb0
ER
1;;; paragraphs.el --- paragraph and sentence parsing.
2
9750e079
ER
3;; Copyright (C) 1985, 86, 87, 1991 Free Software Foundation, Inc.
4
4821e2af 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: wp
4821e2af 7
a2535589
JA
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
29add8b9 12;; the Free Software Foundation; either version 2, or (at your option)
a2535589
JA
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
4821e2af 24;;; Code:
a2535589 25
6503cec3
JB
26(defconst paragraph-start "^[ \t\n\f]" "\
27*Regexp for beginning of a line that starts OR separates paragraphs.")
28
6503cec3
JB
29(defconst paragraph-separate "^[ \t\f]*$" "\
30*Regexp for beginning of a line that separates paragraphs.
31If you change this, you may have to change paragraph-start also.")
32
6503cec3
JB
33(defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
34*Regexp describing the end of a sentence.
35All paragraph boundaries also end sentences, regardless.")
36
6503cec3
JB
37(defconst page-delimiter "^\014" "\
38*Regexp describing line-beginnings that separate pages.")
39
6503cec3
JB
40(defvar paragraph-ignore-fill-prefix nil "\
41Non-nil means the paragraph commands are not affected by `fill-prefix'.
42This is desirable in modes where blank lines are the paragraph delimiters.")
77176e73 43
a2535589
JA
44
45(defun forward-paragraph (&optional arg)
46 "Move forward to end of paragraph.
47With arg N, do it N times; negative arg -N means move forward N paragraphs.
48
49A line which `paragraph-start' matches either separates paragraphs
50\(if `paragraph-separate' matches it also) or is the first line of a paragraph.
51A paragraph end is the beginning of a line which is not part of the paragraph
52to 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)))))
107
108(defun backward-paragraph (&optional arg)
109 "Move backward to start of paragraph.
110With arg N, do it N times; negative arg -N means move forward N paragraphs.
111
23b34992
BP
112A paragraph start is the beginning of a line which is a
113`first-line-of-paragraph' or which is ordinary text and follows a
114paragraph-separating line; except: if the first real line of a
115paragraph is preceded by a blank line, the paragraph starts at that
116blank line.
117
118See `forward-paragraph' for more information."
a2535589
JA
119 (interactive "p")
120 (or arg (setq arg 1))
121 (forward-paragraph (- arg)))
122
123(defun mark-paragraph ()
124 "Put point at beginning of this paragraph, mark at end.
125The 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))
130
131(defun kill-paragraph (arg)
132 "Kill forward to end of paragraph.
133With arg N, kill forward to Nth end of paragraph;
134negative arg -N means kill backward to Nth start of paragraph."
23b34992 135 (interactive "p")
2247b0fc 136 (kill-region (point) (save-excursion (forward-paragraph arg) (point))))
a2535589
JA
137
138(defun backward-kill-paragraph (arg)
139 "Kill back to start of paragraph.
140With arg N, kill back to Nth start of paragraph;
141negative arg -N means kill forward to Nth end of paragraph."
23b34992 142 (interactive "p")
2247b0fc 143 (kill-region (point) (save-excursion (backward-paragraph arg) (point))))
a2535589
JA
144
145(defun transpose-paragraphs (arg)
146 "Interchange this (or next) paragraph with previous one."
147 (interactive "*p")
148 (transpose-subr 'forward-paragraph arg))
149
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")
b4e6c391
RS
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))
a2535589
JA
160 (progn
161 (goto-char npoint)
162 (if (> npoint (point-min))
163 (start-of-paragraph-text))))))
164
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))))))
174
175(defun forward-sentence (&optional arg)
23b34992
BP
176 "Move forward to next`sentence-end'. With argument, repeat.
177With negative argument, move backward repeatedly to `sentence-beginning'.
a2535589 178
23b34992
BP
179The variable `sentence-end' is a regular expression that matches ends of
180sentences. Also, every paragraph boundary terminates sentences as well."
a2535589
JA
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))))
195
196(defun backward-sentence (&optional arg)
197 "Move backward to start of sentence. With arg, do it arg times.
23b34992 198See `forward-sentence' for more information."
a2535589
JA
199 (interactive "p")
200 (or arg (setq arg 1))
201 (forward-sentence (- arg)))
202
203(defun kill-sentence (&optional arg)
204 "Kill from point to end of sentence.
205With arg, repeat; negative arg -N means kill back to Nth start of sentence."
206 (interactive "*p")
2247b0fc 207 (kill-region (point) (save-excursion (forward-sentence arg) (point))))
a2535589
JA
208
209(defun backward-kill-sentence (&optional arg)
210 "Kill back from point to start of sentence.
211With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
212 (interactive "*p")
7270bdb0 213 (kill-region (point) (save-excursion (backward-sentence arg) (point))))
a2535589
JA
214
215(defun mark-end-of-sentence (arg)
23b34992 216 "Put mark at end of sentence. Arg works as in `forward-sentence'."
a2535589
JA
217 (interactive "p")
218 (push-mark
219 (save-excursion
220 (forward-sentence arg)
221 (point))))
222
223(defun transpose-sentences (arg)
224 "Interchange this (next) and previous sentence."
225 (interactive "*p")
226 (transpose-subr 'forward-sentence arg))
6594deb0
ER
227
228;;; paragraphs.el ends here