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