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