]> code.delx.au - gnu-emacs/blob - lisp/textmodes/paragraphs.el
(enable-kinsoku): Name changed form do-kinsoku.
[gnu-emacs] / lisp / textmodes / paragraphs.el
1 ;;; paragraphs.el --- paragraph and sentence parsing.
2
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: wp
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This package provides the paragraph-oriented commands documented in the
28 ;; Emacs manual.
29
30 ;;; Code:
31
32 (defvar use-hard-newlines nil
33 "Non-nil means to distinguish hard and soft newlines.
34 See documentation for the `use-hard-newlines' function.")
35 (make-variable-buffer-local 'use-hard-newlines)
36
37 (defun use-hard-newlines (&optional arg insert)
38 "Minor mode to distinguish hard and soft newlines.
39 When active, the functions `newline' and `open-line' add the
40 text-property `hard' to newlines that they insert, and a line is
41 only considered as a candidate to match `paragraph-start' or
42 `paragraph-separate' if it follows a hard newline.
43
44 Prefix argument says to turn mode on if positive, off if negative.
45 When the mode is turned on, if there are newlines in the buffer but no hard
46 newlines, ask the user whether to mark as hard any newlines preceeding a
47 `paragraph-start' line. From a program, second arg INSERT specifies whether
48 to do this; it can be `never' to change nothing, t or `always' to force
49 marking, `guess' to try to do the right thing with no questions, nil
50 or anything else to ask the user.
51
52 Newlines not marked hard are called \"soft\", and are always internal
53 to paragraphs. The fill functions insert and delete only soft newlines."
54 (interactive (list current-prefix-arg nil))
55 (if (or (<= (prefix-numeric-value arg) 0)
56 (and use-hard-newlines (null arg)))
57 ;; Turn mode off
58 (setq use-hard-newlines nil)
59 ;; Turn mode on
60 ;; Intuit hard newlines --
61 ;; mark as hard any newlines preceding a paragraph-start line.
62 (if (or (eq insert t) (eq insert 'always)
63 (and (not (eq 'never insert))
64 (not use-hard-newlines)
65 (not (text-property-any (point-min) (point-max) 'hard t))
66 (save-excursion
67 (goto-char (point-min))
68 (search-forward "\n" nil t))
69 (or (eq insert 'guess)
70 (y-or-n-p "Make newlines between paragraphs hard? "))))
71 (save-excursion
72 (goto-char (point-min))
73 (while (search-forward "\n" nil t)
74 (let ((pos (point)))
75 (move-to-left-margin)
76 (if (looking-at paragraph-start)
77 (progn
78 (set-hard-newline-properties (1- pos) pos)
79 ;; If paragraph-separate, newline after it is hard too.
80 (if (looking-at paragraph-separate)
81 (progn
82 (end-of-line)
83 (if (not (eobp))
84 (set-hard-newline-properties
85 (point) (1+ (point))))))))))))
86 (setq use-hard-newlines t)))
87
88 (defvar paragraph-start "[ \t\n\f]" "\
89 *Regexp for beginning of a line that starts OR separates paragraphs.
90 This regexp should match lines that separate paragraphs
91 and should also match lines that start a paragraph
92 \(and are part of that paragraph).
93
94 This is matched against the text at the left margin, which is not necessarily
95 the beginning of the line, so it should never use \"^\" as an anchor. This
96 ensures that the paragraph functions will work equally well within a region
97 of text indented by a margin setting.
98
99 The variable `paragraph-separate' specifies how to distinguish
100 lines that start paragraphs from lines that separate them.
101
102 If the variable `use-hard-newlines' is nonnil, then only lines following a
103 hard newline are considered to match.")
104
105 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
106 ;; It is assumed that paragraph-separate is distinctive enough to be believed
107 ;; whenever it occurs, while it is reasonable to set paragraph-start to
108 ;; something very minimal, even including "." (which makes every hard newline
109 ;; start a new paragraph).
110
111 (defvar paragraph-separate "[ \t\f]*$" "\
112 *Regexp for beginning of a line that separates paragraphs.
113 If you change this, you may have to change paragraph-start also.
114
115 This is matched against the text at the left margin, which is not necessarily
116 the beginning of the line, so it should not use \"^\" as an anchor. This
117 ensures that the paragraph functions will work equally within a region of
118 text indented by a margin setting.")
119
120 (defvar sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
121 *Regexp describing the end of a sentence.
122 All paragraph boundaries also end sentences, regardless.
123
124 In order to be recognized as the end of a sentence, the ending period,
125 question mark, or exclamation point must be followed by two spaces,
126 unless it's inside some sort of quotes or parenthesis.")
127
128 (defvar page-delimiter "^\014" "\
129 *Regexp describing line-beginnings that separate pages.")
130
131 (defvar paragraph-ignore-fill-prefix nil "\
132 Non-nil means the paragraph commands are not affected by `fill-prefix'.
133 This is desirable in modes where blank lines are the paragraph delimiters.")
134
135 (defun forward-paragraph (&optional arg)
136 "Move forward to end of paragraph.
137 With argument ARG, do it ARG times;
138 a negative argument ARG = -N means move backward N paragraphs.
139
140 A line which `paragraph-start' matches either separates paragraphs
141 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
142 A paragraph end is the beginning of a line which is not part of the paragraph
143 to which the end of the previous line belongs, or the end of the buffer."
144 (interactive "p")
145 (or arg (setq arg 1))
146 (let* ((fill-prefix-regexp
147 (and fill-prefix (not (equal fill-prefix ""))
148 (not paragraph-ignore-fill-prefix)
149 (regexp-quote fill-prefix)))
150 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
151 ;; These regexps shouldn't be anchored, because we look for them
152 ;; starting at the left-margin. This allows paragraph commands to
153 ;; work normally with indented text.
154 ;; This hack will not find problem cases like "whatever\\|^something".
155 (paragraph-start (if (and (not (equal "" paragraph-start))
156 (equal ?^ (aref paragraph-start 0)))
157 (substring paragraph-start 1)
158 paragraph-start))
159 (paragraph-separate (if (and (not (equal "" paragraph-separate))
160 (equal ?^ (aref paragraph-separate 0)))
161 (substring paragraph-separate 1)
162 paragraph-separate))
163 (paragraph-separate
164 (if fill-prefix-regexp
165 (concat paragraph-separate "\\|"
166 fill-prefix-regexp "[ \t]*$")
167 paragraph-separate))
168 ;; This is used for searching.
169 (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)"))
170 start)
171 (while (and (< arg 0) (not (bobp)))
172 (if (and (not (looking-at paragraph-separate))
173 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
174 (looking-at paragraph-separate))
175 nil
176 (setq start (point))
177 ;; Move back over paragraph-separating lines.
178 (forward-char -1) (beginning-of-line)
179 (while (and (not (bobp))
180 (progn (move-to-left-margin)
181 (looking-at paragraph-separate)))
182 (forward-line -1))
183 (if (bobp)
184 nil
185 ;; Go to end of the previous (non-separating) line.
186 (end-of-line)
187 ;; Search back for line that starts or separates paragraphs.
188 (if (if fill-prefix-regexp
189 ;; There is a fill prefix; it overrides paragraph-start.
190 (let (multiple-lines)
191 (while (and (progn (beginning-of-line) (not (bobp)))
192 (progn (move-to-left-margin)
193 (not (looking-at paragraph-separate)))
194 (looking-at fill-prefix-regexp))
195 (if (not (= (point) start))
196 (setq multiple-lines t))
197 (forward-line -1))
198 (move-to-left-margin)
199 ;;; This deleted code caused a long hanging-indent line
200 ;;; not to be filled together with the following lines.
201 ;;; ;; Don't move back over a line before the paragraph
202 ;;; ;; which doesn't start with fill-prefix
203 ;;; ;; unless that is the only line we've moved over.
204 ;;; (and (not (looking-at fill-prefix-regexp))
205 ;;; multiple-lines
206 ;;; (forward-line 1))
207 (not (bobp)))
208 (while (and (re-search-backward sp-paragraph-start nil 1)
209 ;; Found a candidate, but need to check if it is a
210 ;; REAL paragraph-start.
211 (not (bobp))
212 (progn (setq start (point))
213 (move-to-left-margin)
214 (not (looking-at paragraph-separate)))
215 (or (not (looking-at paragraph-start))
216 (and use-hard-newlines
217 (not (get-text-property (1- start)
218 'hard)))))
219 (goto-char start))
220 (> (point) (point-min)))
221 ;; Found one.
222 (progn
223 ;; Move forward over paragraph separators.
224 ;; We know this cannot reach the place we started
225 ;; because we know we moved back over a non-separator.
226 (while (and (not (eobp))
227 (progn (move-to-left-margin)
228 (looking-at paragraph-separate)))
229 (forward-line 1))
230 ;; If line before paragraph is just margin, back up to there.
231 (end-of-line 0)
232 (if (> (current-column) (current-left-margin))
233 (forward-char 1)
234 (skip-chars-backward " \t")
235 (if (not (bolp))
236 (forward-line 1))))
237 ;; No starter or separator line => use buffer beg.
238 (goto-char (point-min)))))
239 (setq arg (1+ arg)))
240 (while (and (> arg 0) (not (eobp)))
241 ;; Move forward over separator lines, and one more line.
242 (while (prog1 (and (not (eobp))
243 (progn (move-to-left-margin) (not (eobp)))
244 (looking-at paragraph-separate))
245 (forward-line 1)))
246 (if fill-prefix-regexp
247 ;; There is a fill prefix; it overrides paragraph-start.
248 (while (and (not (eobp))
249 (progn (move-to-left-margin) (not (eobp)))
250 (not (looking-at paragraph-separate))
251 (looking-at fill-prefix-regexp))
252 (forward-line 1))
253 (while (and (re-search-forward sp-paragraph-start nil 1)
254 (progn (setq start (match-beginning 0))
255 (goto-char start)
256 (not (eobp)))
257 (progn (move-to-left-margin)
258 (not (looking-at paragraph-separate)))
259 (or (not (looking-at paragraph-start))
260 (and use-hard-newlines
261 (not (get-text-property (1- start) 'hard)))))
262 (forward-char 1))
263 (if (< (point) (point-max))
264 (goto-char start)))
265 (setq arg (1- arg)))))
266
267 (defun backward-paragraph (&optional arg)
268 "Move backward to start of paragraph.
269 With argument ARG, do it ARG times;
270 a negative argument ARG = -N means move forward N paragraphs.
271
272 A paragraph start is the beginning of a line which is a
273 `first-line-of-paragraph' or which is ordinary text and follows a
274 paragraph-separating line; except: if the first real line of a
275 paragraph is preceded by a blank line, the paragraph starts at that
276 blank line.
277
278 See `forward-paragraph' for more information."
279 (interactive "p")
280 (or arg (setq arg 1))
281 (forward-paragraph (- arg)))
282
283 (defun mark-paragraph ()
284 "Put point at beginning of this paragraph, mark at end.
285 The paragraph marked is the one that contains point or follows point."
286 (interactive)
287 (forward-paragraph 1)
288 (push-mark nil t t)
289 (backward-paragraph 1))
290
291 (defun kill-paragraph (arg)
292 "Kill forward to end of paragraph.
293 With arg N, kill forward to Nth end of paragraph;
294 negative arg -N means kill backward to Nth start of paragraph."
295 (interactive "p")
296 (kill-region (point) (progn (forward-paragraph arg) (point))))
297
298 (defun backward-kill-paragraph (arg)
299 "Kill back to start of paragraph.
300 With arg N, kill back to Nth start of paragraph;
301 negative arg -N means kill forward to Nth end of paragraph."
302 (interactive "p")
303 (kill-region (point) (progn (backward-paragraph arg) (point))))
304
305 (defun transpose-paragraphs (arg)
306 "Interchange this (or next) paragraph with previous one."
307 (interactive "*p")
308 (transpose-subr 'forward-paragraph arg))
309
310 (defun start-of-paragraph-text ()
311 (let ((opoint (point)) npoint)
312 (forward-paragraph -1)
313 (setq npoint (point))
314 (skip-chars-forward " \t\n")
315 ;; If the range of blank lines found spans the original start point,
316 ;; try again from the beginning of it.
317 ;; Must be careful to avoid infinite loop
318 ;; when following a single return at start of buffer.
319 (if (and (>= (point) opoint) (< npoint opoint))
320 (progn
321 (goto-char npoint)
322 (if (> npoint (point-min))
323 (start-of-paragraph-text))))))
324
325 (defun end-of-paragraph-text ()
326 (let ((opoint (point)))
327 (forward-paragraph 1)
328 (if (eq (preceding-char) ?\n) (forward-char -1))
329 (if (<= (point) opoint)
330 (progn
331 (forward-char 1)
332 (if (< (point) (point-max))
333 (end-of-paragraph-text))))))
334
335 (defun forward-sentence (&optional arg)
336 "Move forward to next `sentence-end'. With argument, repeat.
337 With negative argument, move backward repeatedly to `sentence-beginning'.
338
339 The variable `sentence-end' is a regular expression that matches ends of
340 sentences. Also, every paragraph boundary terminates sentences as well."
341 (interactive "p")
342 (or arg (setq arg 1))
343 (while (< arg 0)
344 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
345 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
346 (goto-char (1- (match-end 0)))
347 (goto-char par-beg)))
348 (setq arg (1+ arg)))
349 (while (> arg 0)
350 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
351 (if (re-search-forward sentence-end par-end t)
352 (skip-chars-backward " \t\n")
353 (goto-char par-end)))
354 (setq arg (1- arg))))
355
356 (defun backward-sentence (&optional arg)
357 "Move backward to start of sentence. With arg, do it arg times.
358 See `forward-sentence' for more information."
359 (interactive "p")
360 (or arg (setq arg 1))
361 (forward-sentence (- arg)))
362
363 (defun kill-sentence (&optional arg)
364 "Kill from point to end of sentence.
365 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
366 (interactive "p")
367 (kill-region (point) (progn (forward-sentence arg) (point))))
368
369 (defun backward-kill-sentence (&optional arg)
370 "Kill back from point to start of sentence.
371 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
372 (interactive "p")
373 (kill-region (point) (progn (backward-sentence arg) (point))))
374
375 (defun mark-end-of-sentence (arg)
376 "Put mark at end of sentence. Arg works as in `forward-sentence'."
377 (interactive "p")
378 (push-mark
379 (save-excursion
380 (forward-sentence arg)
381 (point))
382 nil t))
383
384 (defun transpose-sentences (arg)
385 "Interchange this (next) and previous sentence."
386 (interactive "*p")
387 (transpose-subr 'forward-sentence arg))
388
389 ;;; paragraphs.el ends here