*** empty log message ***
[bpt/emacs.git] / lisp / textmodes / paragraphs.el
CommitLineData
6594deb0
ER
1;;; paragraphs.el --- paragraph and sentence parsing.
2
77176e73 3;; Copyright (C) 1985-1991 Free Software Foundation, Inc.
a2535589
JA
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 1, or (at your option)
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING. If not, write to
19;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
6503cec3
JB
22(defconst paragraph-start "^[ \t\n\f]" "\
23*Regexp for beginning of a line that starts OR separates paragraphs.")
24
6503cec3
JB
25(defconst paragraph-separate "^[ \t\f]*$" "\
26*Regexp for beginning of a line that separates paragraphs.
27If you change this, you may have to change paragraph-start also.")
28
6503cec3
JB
29(defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
30*Regexp describing the end of a sentence.
31All paragraph boundaries also end sentences, regardless.")
32
6503cec3
JB
33(defconst page-delimiter "^\014" "\
34*Regexp describing line-beginnings that separate pages.")
35
6503cec3
JB
36(defvar paragraph-ignore-fill-prefix nil "\
37Non-nil means the paragraph commands are not affected by `fill-prefix'.
38This is desirable in modes where blank lines are the paragraph delimiters.")
77176e73 39
a2535589
JA
40
41(defun forward-paragraph (&optional arg)
42 "Move forward to end of paragraph.
43With arg N, do it N times; negative arg -N means move forward N paragraphs.
44
45A line which `paragraph-start' matches either separates paragraphs
46\(if `paragraph-separate' matches it also) or is the first line of a paragraph.
47A paragraph end is the beginning of a line which is not part of the paragraph
48to which the end of the previous line belongs, or the end of the buffer."
49 (interactive "p")
50 (or arg (setq arg 1))
51 (let* ((fill-prefix-regexp
52 (and fill-prefix (not (equal fill-prefix ""))
53 (not paragraph-ignore-fill-prefix)
54 (regexp-quote fill-prefix)))
55 (paragraph-separate
56 (if fill-prefix-regexp
57 (concat paragraph-separate "\\|^"
58 fill-prefix-regexp "[ \t]*$")
59 paragraph-separate)))
60 (while (< arg 0)
61 (if (and (not (looking-at paragraph-separate))
62 (re-search-backward "^\n" (max (1- (point)) (point-min)) t))
63 nil
64 (forward-char -1) (beginning-of-line)
65 (while (and (not (bobp)) (looking-at paragraph-separate))
66 (forward-line -1))
67 (end-of-line)
68 ;; Search back for line that starts or separates paragraphs.
69 (if (if fill-prefix-regexp
70 ;; There is a fill prefix; it overrides paragraph-start.
71 (progn
72 (while (progn (beginning-of-line)
73 (and (not (bobp))
74 (not (looking-at paragraph-separate))
75 (looking-at fill-prefix-regexp)))
76 (forward-line -1))
77 (not (bobp)))
78 (re-search-backward paragraph-start nil t))
79 ;; Found one.
80 (progn
81 (while (and (not (eobp)) (looking-at paragraph-separate))
82 (forward-line 1))
83 (if (eq (char-after (- (point) 2)) ?\n)
84 (forward-line -1)))
85 ;; No starter or separator line => use buffer beg.
86 (goto-char (point-min))))
87 (setq arg (1+ arg)))
88 (while (> arg 0)
89 (beginning-of-line)
90 (while (prog1 (and (not (eobp))
91 (looking-at paragraph-separate))
92 (forward-line 1)))
93 (if fill-prefix-regexp
94 ;; There is a fill prefix; it overrides paragraph-start.
95 (while (and (not (eobp))
96 (not (looking-at paragraph-separate))
97 (looking-at fill-prefix-regexp))
98 (forward-line 1))
99 (if (re-search-forward paragraph-start nil t)
100 (goto-char (match-beginning 0))
101 (goto-char (point-max))))
102 (setq arg (1- arg)))))
103
104(defun backward-paragraph (&optional arg)
105 "Move backward to start of paragraph.
106With arg N, do it N times; negative arg -N means move forward N paragraphs.
107
23b34992
BP
108A paragraph start is the beginning of a line which is a
109`first-line-of-paragraph' or which is ordinary text and follows a
110paragraph-separating line; except: if the first real line of a
111paragraph is preceded by a blank line, the paragraph starts at that
112blank line.
113
114See `forward-paragraph' for more information."
a2535589
JA
115 (interactive "p")
116 (or arg (setq arg 1))
117 (forward-paragraph (- arg)))
118
119(defun mark-paragraph ()
120 "Put point at beginning of this paragraph, mark at end.
121The paragraph marked is the one that contains point or follows point."
122 (interactive)
123 (forward-paragraph 1)
124 (push-mark nil t)
125 (backward-paragraph 1))
126
127(defun kill-paragraph (arg)
128 "Kill forward to end of paragraph.
129With arg N, kill forward to Nth end of paragraph;
130negative arg -N means kill backward to Nth start of paragraph."
23b34992 131 (interactive "p")
a2535589
JA
132 (kill-region (point) (progn (forward-paragraph arg) (point))))
133
134(defun backward-kill-paragraph (arg)
135 "Kill back to start of paragraph.
136With arg N, kill back to Nth start of paragraph;
137negative arg -N means kill forward to Nth end of paragraph."
23b34992 138 (interactive "p")
a2535589
JA
139 (kill-region (point) (progn (backward-paragraph arg) (point))))
140
141(defun transpose-paragraphs (arg)
142 "Interchange this (or next) paragraph with previous one."
143 (interactive "*p")
144 (transpose-subr 'forward-paragraph arg))
145
146(defun start-of-paragraph-text ()
147 (let ((opoint (point)) npoint)
148 (forward-paragraph -1)
149 (setq npoint (point))
150 (skip-chars-forward " \t\n")
b4e6c391
RS
151 ;; If the range of blank lines found spans the original start point,
152 ;; try again from the beginning of it.
153 ;; Must be careful to avoid infinite loop
154 ;; when following a single return at start of buffer.
155 (if (and (>= (point) opoint) (< npoint opoint))
a2535589
JA
156 (progn
157 (goto-char npoint)
158 (if (> npoint (point-min))
159 (start-of-paragraph-text))))))
160
161(defun end-of-paragraph-text ()
162 (let ((opoint (point)))
163 (forward-paragraph 1)
164 (if (eq (preceding-char) ?\n) (forward-char -1))
165 (if (<= (point) opoint)
166 (progn
167 (forward-char 1)
168 (if (< (point) (point-max))
169 (end-of-paragraph-text))))))
170
171(defun forward-sentence (&optional arg)
23b34992
BP
172 "Move forward to next`sentence-end'. With argument, repeat.
173With negative argument, move backward repeatedly to `sentence-beginning'.
a2535589 174
23b34992
BP
175The variable `sentence-end' is a regular expression that matches ends of
176sentences. Also, every paragraph boundary terminates sentences as well."
a2535589
JA
177 (interactive "p")
178 (or arg (setq arg 1))
179 (while (< arg 0)
180 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
181 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
182 (goto-char (1- (match-end 0)))
183 (goto-char par-beg)))
184 (setq arg (1+ arg)))
185 (while (> arg 0)
186 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
187 (if (re-search-forward sentence-end par-end t)
188 (skip-chars-backward " \t\n")
189 (goto-char par-end)))
190 (setq arg (1- arg))))
191
192(defun backward-sentence (&optional arg)
193 "Move backward to start of sentence. With arg, do it arg times.
23b34992 194See `forward-sentence' for more information."
a2535589
JA
195 (interactive "p")
196 (or arg (setq arg 1))
197 (forward-sentence (- arg)))
198
199(defun kill-sentence (&optional arg)
200 "Kill from point to end of sentence.
201With arg, repeat; negative arg -N means kill back to Nth start of sentence."
202 (interactive "*p")
203 (let ((beg (point)))
204 (forward-sentence arg)
205 (kill-region beg (point))))
206
207(defun backward-kill-sentence (&optional arg)
208 "Kill back from point to start of sentence.
209With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
210 (interactive "*p")
211 (let ((beg (point)))
212 (backward-sentence arg)
213 (kill-region beg (point))))
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