*** empty log message ***
[bpt/emacs.git] / lisp / textmodes / paragraphs.el
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'.
23 This 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.
27 With arg N, do it N times; negative arg -N means move forward N paragraphs.
28
29 A line which `paragraph-start' matches either separates paragraphs
30 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
31 A paragraph end is the beginning of a line which is not part of the paragraph
32 to 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.
90 With arg N, do it N times; negative arg -N means move forward N paragraphs.
91
92 A paragraph start is the beginning of a line which is a
93 `first-line-of-paragraph' or which is ordinary text and follows a
94 paragraph-separating line; except: if the first real line of a
95 paragraph is preceded by a blank line, the paragraph starts at that
96 blank line.
97
98 See `forward-paragraph' for more information."
99 (interactive "p")
100 (or arg (setq arg 1))
101 (forward-paragraph (- arg)))
102
103 (defun mark-paragraph ()
104 "Put point at beginning of this paragraph, mark at end.
105 The paragraph marked is the one that contains point or follows point."
106 (interactive)
107 (forward-paragraph 1)
108 (push-mark nil t)
109 (backward-paragraph 1))
110
111 (defun kill-paragraph (arg)
112 "Kill forward to end of paragraph.
113 With arg N, kill forward to Nth end of paragraph;
114 negative arg -N means kill backward to Nth start of paragraph."
115 (interactive "p")
116 (kill-region (point) (progn (forward-paragraph arg) (point))))
117
118 (defun backward-kill-paragraph (arg)
119 "Kill back to start of paragraph.
120 With arg N, kill back to Nth start of paragraph;
121 negative arg -N means kill forward to Nth end of paragraph."
122 (interactive "p")
123 (kill-region (point) (progn (backward-paragraph arg) (point))))
124
125 (defun transpose-paragraphs (arg)
126 "Interchange this (or next) paragraph with previous one."
127 (interactive "*p")
128 (transpose-subr 'forward-paragraph arg))
129
130 (defun start-of-paragraph-text ()
131 (let ((opoint (point)) npoint)
132 (forward-paragraph -1)
133 (setq npoint (point))
134 (skip-chars-forward " \t\n")
135 ;; If the range of blank lines found spans the original start point,
136 ;; try again from the beginning of it.
137 ;; Must be careful to avoid infinite loop
138 ;; when following a single return at start of buffer.
139 (if (and (>= (point) opoint) (< npoint opoint))
140 (progn
141 (goto-char npoint)
142 (if (> npoint (point-min))
143 (start-of-paragraph-text))))))
144
145 (defun end-of-paragraph-text ()
146 (let ((opoint (point)))
147 (forward-paragraph 1)
148 (if (eq (preceding-char) ?\n) (forward-char -1))
149 (if (<= (point) opoint)
150 (progn
151 (forward-char 1)
152 (if (< (point) (point-max))
153 (end-of-paragraph-text))))))
154
155 (defun forward-sentence (&optional arg)
156 "Move forward to next`sentence-end'. With argument, repeat.
157 With negative argument, move backward repeatedly to `sentence-beginning'.
158
159 The variable `sentence-end' is a regular expression that matches ends of
160 sentences. Also, every paragraph boundary terminates sentences as well."
161 (interactive "p")
162 (or arg (setq arg 1))
163 (while (< arg 0)
164 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
165 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
166 (goto-char (1- (match-end 0)))
167 (goto-char par-beg)))
168 (setq arg (1+ arg)))
169 (while (> arg 0)
170 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
171 (if (re-search-forward sentence-end par-end t)
172 (skip-chars-backward " \t\n")
173 (goto-char par-end)))
174 (setq arg (1- arg))))
175
176 (defun backward-sentence (&optional arg)
177 "Move backward to start of sentence. With arg, do it arg times.
178 See `forward-sentence' for more information."
179 (interactive "p")
180 (or arg (setq arg 1))
181 (forward-sentence (- arg)))
182
183 (defun kill-sentence (&optional arg)
184 "Kill from point to end of sentence.
185 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
186 (interactive "*p")
187 (let ((beg (point)))
188 (forward-sentence arg)
189 (kill-region beg (point))))
190
191 (defun backward-kill-sentence (&optional arg)
192 "Kill back from point to start of sentence.
193 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
194 (interactive "*p")
195 (let ((beg (point)))
196 (backward-sentence arg)
197 (kill-region beg (point))))
198
199 (defun mark-end-of-sentence (arg)
200 "Put mark at end of sentence. Arg works as in `forward-sentence'."
201 (interactive "p")
202 (push-mark
203 (save-excursion
204 (forward-sentence arg)
205 (point))))
206
207 (defun transpose-sentences (arg)
208 "Interchange this (next) and previous sentence."
209 (interactive "*p")
210 (transpose-subr 'forward-sentence arg))