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