Trailing whitespace deleted.
[emacs.git] / lisp / textmodes / paragraphs.el
blobf1e486138cf13cd380affdedc885652f88598297
1 ;;; paragraphs.el --- paragraph and sentence parsing
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96, 1997, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; This package provides the paragraph-oriented commands documented in the
29 ;; Emacs manual.
31 ;;; Code:
33 (defgroup paragraphs nil
34 "Paragraph and sentence parsing."
35 :group 'editing)
37 (define-minor-mode use-hard-newlines
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.
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.
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 :group 'paragraphs
55 :extra-args (insert)
56 (when use-hard-newlines
57 ;; Turn mode on
58 ;; Intuit hard newlines --
59 ;; mark as hard any newlines preceding a paragraph-start line.
60 (if (or (eq insert t) (eq insert 'always)
61 (and (not (eq 'never insert))
62 (not (text-property-any (point-min) (point-max) 'hard t))
63 (save-excursion
64 (goto-char (point-min))
65 (search-forward "\n" nil t))
66 (or (eq insert 'guess)
67 (y-or-n-p "Make newlines between paragraphs hard? "))))
68 (save-excursion
69 (goto-char (point-min))
70 (while (search-forward "\n" nil t)
71 (let ((pos (point)))
72 (move-to-left-margin)
73 (when (looking-at paragraph-start)
74 (set-hard-newline-properties (1- pos) pos))
75 ;; If paragraph-separate, newline after it is hard too.
76 (when (looking-at paragraph-separate)
77 (set-hard-newline-properties (1- pos) pos)
78 (end-of-line)
79 (unless (eobp)
80 (set-hard-newline-properties (point) (1+ (point)))))))))))
82 (defcustom paragraph-start "\f\\|[ \t]*$" "\
83 *Regexp for beginning of a line that starts OR separates paragraphs.
84 This regexp should match lines that separate paragraphs
85 and should also match lines that start a paragraph
86 \(and are part of that paragraph).
88 This is matched against the text at the left margin, which is not necessarily
89 the beginning of the line, so it should never use \"^\" as an anchor. This
90 ensures that the paragraph functions will work equally well within a region
91 of text indented by a margin setting.
93 The variable `paragraph-separate' specifies how to distinguish
94 lines that start paragraphs from lines that separate them.
96 If the variable `use-hard-newlines' is non-nil, then only lines following a
97 hard newline are considered to match."
98 :group 'paragraphs
99 :type 'regexp)
101 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
102 ;; It is assumed that paragraph-separate is distinctive enough to be believed
103 ;; whenever it occurs, while it is reasonable to set paragraph-start to
104 ;; something very minimal, even including "." (which makes every hard newline
105 ;; start a new paragraph).
107 (defcustom paragraph-separate "[ \t\f]*$"
108 "*Regexp for beginning of a line that separates paragraphs.
109 If you change this, you may have to change `paragraph-start' also.
111 This is matched against the text at the left margin, which is not necessarily
112 the beginning of the line, so it should not use \"^\" as an anchor. This
113 ensures that the paragraph functions will work equally within a region of
114 text indented by a margin setting."
115 :group 'paragraphs
116 :type 'regexp)
118 (defcustom sentence-end-double-space t
119 "*Non-nil means a single space does not end a sentence.
120 This is relevant for filling. See also `sentence-end-without-period'
121 and `colon-double-space'.
123 If you change this, you should also change `sentence-end'. See Info
124 node `Sentences'."
125 :type 'boolean
126 :group 'fill)
128 (defcustom sentence-end-without-period nil
129 "*Non-nil means a sentence will end without a period.
130 For example, a sentence in Thai text ends with double space but
131 without a period."
132 :type 'boolean
133 :group 'fill)
135 (defcustom sentence-end
136 (purecopy
137 ;; This is a bit stupid since it's not auto-updated when the
138 ;; other variables are changes, but it's still useful info.
139 (concat (if sentence-end-without-period "\\w \\|")
140 "[.?!][]\"')}]*"
141 (if sentence-end-double-space
142 "\\($\\| $\\|\t\\| \\)" "\\($\\|[\t ]\\)")
143 "[ \t\n]*"))
144 "*Regexp describing the end of a sentence.
145 The value includes the whitespace following the sentence.
146 All paragraph boundaries also end sentences, regardless.
148 The default value specifies that in order to be recognized as the end
149 of a sentence, the ending period, question mark, or exclamation point
150 must be followed by two spaces, unless it's inside some sort of quotes
151 or parenthesis.
153 See also the variable `sentence-end-double-space', the variable
154 `sentence-end-without-period' and Info node `Sentences'."
155 :group 'paragraphs
156 :type 'regexp)
158 (defcustom page-delimiter "^\014"
159 "*Regexp describing line-beginnings that separate pages."
160 :group 'paragraphs
161 :type 'regexp)
163 (defcustom paragraph-ignore-fill-prefix nil
164 "*Non-nil means the paragraph commands are not affected by `fill-prefix'.
165 This is desirable in modes where blank lines are the paragraph delimiters."
166 :group 'paragraphs
167 :type 'boolean)
169 (defun forward-paragraph (&optional arg)
170 "Move forward to end of paragraph.
171 With argument ARG, do it ARG times;
172 a negative argument ARG = -N means move backward N paragraphs.
174 A line which `paragraph-start' matches either separates paragraphs
175 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
176 A paragraph end is the beginning of a line which is not part of the paragraph
177 to which the end of the previous line belongs, or the end of the buffer.
178 Returns the count of paragraphs left to move."
179 (interactive "p")
180 (or arg (setq arg 1))
181 (let* ((opoint (point))
182 (fill-prefix-regexp
183 (and fill-prefix (not (equal fill-prefix ""))
184 (not paragraph-ignore-fill-prefix)
185 (regexp-quote fill-prefix)))
186 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
187 ;; These regexps shouldn't be anchored, because we look for them
188 ;; starting at the left-margin. This allows paragraph commands to
189 ;; work normally with indented text.
190 ;; This hack will not find problem cases like "whatever\\|^something".
191 (parstart (if (and (not (equal "" paragraph-start))
192 (equal ?^ (aref paragraph-start 0)))
193 (substring paragraph-start 1)
194 paragraph-start))
195 (parsep (if (and (not (equal "" paragraph-separate))
196 (equal ?^ (aref paragraph-separate 0)))
197 (substring paragraph-separate 1)
198 paragraph-separate))
199 (parsep
200 (if fill-prefix-regexp
201 (concat parsep "\\|"
202 fill-prefix-regexp "[ \t]*$")
203 parsep))
204 ;; This is used for searching.
205 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
206 start found-start)
207 (while (and (< arg 0) (not (bobp)))
208 (if (and (not (looking-at parsep))
209 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
210 (looking-at parsep))
211 (setq arg (1+ arg))
212 (setq start (point))
213 ;; Move back over paragraph-separating lines.
214 (forward-char -1) (beginning-of-line)
215 (while (and (not (bobp))
216 (progn (move-to-left-margin)
217 (looking-at parsep)))
218 (forward-line -1))
219 (if (bobp)
221 (setq arg (1+ arg))
222 ;; Go to end of the previous (non-separating) line.
223 (end-of-line)
224 ;; Search back for line that starts or separates paragraphs.
225 (if (if fill-prefix-regexp
226 ;; There is a fill prefix; it overrides parstart.
227 (let (multiple-lines)
228 (while (and (progn (beginning-of-line) (not (bobp)))
229 (progn (move-to-left-margin)
230 (not (looking-at parsep)))
231 (looking-at fill-prefix-regexp))
232 (unless (= (point) start)
233 (setq multiple-lines t))
234 (forward-line -1))
235 (move-to-left-margin)
236 ;; This deleted code caused a long hanging-indent line
237 ;; not to be filled together with the following lines.
238 ;; ;; Don't move back over a line before the paragraph
239 ;; ;; which doesn't start with fill-prefix
240 ;; ;; unless that is the only line we've moved over.
241 ;; (and (not (looking-at fill-prefix-regexp))
242 ;; multiple-lines
243 ;; (forward-line 1))
244 (not (bobp)))
245 (while (and (re-search-backward sp-parstart nil 1)
246 (setq found-start t)
247 ;; Found a candidate, but need to check if it is a
248 ;; REAL parstart.
249 (progn (setq start (point))
250 (move-to-left-margin)
251 (not (looking-at parsep)))
252 (not (and (looking-at parstart)
253 (or (not use-hard-newlines)
254 (get-text-property (1- start) 'hard)
255 (bobp)))))
256 (setq found-start nil)
257 (goto-char start))
258 found-start)
259 ;; Found one.
260 (progn
261 ;; Move forward over paragraph separators.
262 ;; We know this cannot reach the place we started
263 ;; because we know we moved back over a non-separator.
264 (while (and (not (eobp))
265 (progn (move-to-left-margin)
266 (looking-at parsep)))
267 (forward-line 1))
268 ;; If line before paragraph is just margin, back up to there.
269 (end-of-line 0)
270 (if (> (current-column) (current-left-margin))
271 (forward-char 1)
272 (skip-chars-backward " \t")
273 (if (not (bolp))
274 (forward-line 1))))
275 ;; No starter or separator line => use buffer beg.
276 (goto-char (point-min))))))
278 (while (and (> arg 0) (not (eobp)))
279 ;; Move forward over separator lines...
280 (while (and (not (eobp))
281 (progn (move-to-left-margin) (not (eobp)))
282 (looking-at parsep))
283 (forward-line 1))
284 (unless (eobp) (setq arg (1- arg)))
285 ;; ... and one more line.
286 (forward-line 1)
287 (if fill-prefix-regexp
288 ;; There is a fill prefix; it overrides parstart.
289 (while (and (not (eobp))
290 (progn (move-to-left-margin) (not (eobp)))
291 (not (looking-at parsep))
292 (looking-at fill-prefix-regexp))
293 (forward-line 1))
294 (while (and (re-search-forward sp-parstart nil 1)
295 (progn (setq start (match-beginning 0))
296 (goto-char start)
297 (not (eobp)))
298 (progn (move-to-left-margin)
299 (not (looking-at parsep)))
300 (or (not (looking-at parstart))
301 (and use-hard-newlines
302 (not (get-text-property (1- start) 'hard)))))
303 (forward-char 1))
304 (if (< (point) (point-max))
305 (goto-char start))))
306 (constrain-to-field nil opoint t)
307 ;; Return the number of steps that could not be done.
308 arg))
310 (defun backward-paragraph (&optional arg)
311 "Move backward to start of paragraph.
312 With argument ARG, do it ARG times;
313 a negative argument ARG = -N means move forward N paragraphs.
315 A paragraph start is the beginning of a line which is a
316 `first-line-of-paragraph' or which is ordinary text and follows a
317 paragraph-separating line; except: if the first real line of a
318 paragraph is preceded by a blank line, the paragraph starts at that
319 blank line.
321 See `forward-paragraph' for more information."
322 (interactive "p")
323 (or arg (setq arg 1))
324 (forward-paragraph (- arg)))
326 (defun mark-paragraph (&optional arg)
327 "Put point at beginning of this paragraph, mark at end.
328 The paragraph marked is the one that contains point or follows point.
330 With argument ARG, puts mark at end of a following paragraph, so that
331 the number of paragraphs marked equals ARG.
333 If ARG is negative, point is put at end of this paragraph, mark is put
334 at beginning of this or a previous paragraph.
336 If this command is repeated, it marks the next ARG paragraphs after (or
337 before, if arg is negative) the ones already marked."
338 (interactive "p")
339 (unless arg (setq arg 1))
340 (when (zerop arg)
341 (error "Cannot mark zero paragraphs"))
342 (cond ((and (eq last-command this-command) (mark t))
343 (set-mark
344 (save-excursion
345 (goto-char (mark))
346 (forward-paragraph arg)
347 (point))))
349 (forward-paragraph arg)
350 (push-mark nil t t)
351 (backward-paragraph arg))))
353 (defun kill-paragraph (arg)
354 "Kill forward to end of paragraph.
355 With arg N, kill forward to Nth end of paragraph;
356 negative arg -N means kill backward to Nth start of paragraph."
357 (interactive "p")
358 (kill-region (point) (progn (forward-paragraph arg) (point))))
360 (defun backward-kill-paragraph (arg)
361 "Kill back to start of paragraph.
362 With arg N, kill back to Nth start of paragraph;
363 negative arg -N means kill forward to Nth end of paragraph."
364 (interactive "p")
365 (kill-region (point) (progn (backward-paragraph arg) (point))))
367 (defun transpose-paragraphs (arg)
368 "Interchange this (or next) paragraph with previous one."
369 (interactive "*p")
370 (transpose-subr 'forward-paragraph arg))
372 (defun start-of-paragraph-text ()
373 (let ((opoint (point)) npoint)
374 (forward-paragraph -1)
375 (setq npoint (point))
376 (skip-chars-forward " \t\n")
377 ;; If the range of blank lines found spans the original start point,
378 ;; try again from the beginning of it.
379 ;; Must be careful to avoid infinite loop
380 ;; when following a single return at start of buffer.
381 (if (and (>= (point) opoint) (< npoint opoint))
382 (progn
383 (goto-char npoint)
384 (if (> npoint (point-min))
385 (start-of-paragraph-text))))))
387 (defun end-of-paragraph-text ()
388 (let ((opoint (point)))
389 (forward-paragraph 1)
390 (if (eq (preceding-char) ?\n) (forward-char -1))
391 (if (<= (point) opoint)
392 (progn
393 (forward-char 1)
394 (if (< (point) (point-max))
395 (end-of-paragraph-text))))))
397 (defun forward-sentence (&optional arg)
398 "Move forward to next `sentence-end'. With argument, repeat.
399 With negative argument, move backward repeatedly to `sentence-beginning'.
401 The variable `sentence-end' is a regular expression that matches ends of
402 sentences. Also, every paragraph boundary terminates sentences as well."
403 (interactive "p")
404 (or arg (setq arg 1))
405 (let ((opoint (point)))
406 (while (< arg 0)
407 (let ((pos (point))
408 (par-beg (save-excursion (start-of-paragraph-text) (point))))
409 (if (and (re-search-backward sentence-end par-beg t)
410 (or (< (match-end 0) pos)
411 (re-search-backward sentence-end par-beg t)))
412 (goto-char (match-end 0))
413 (goto-char par-beg)))
414 (setq arg (1+ arg)))
415 (while (> arg 0)
416 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
417 (if (re-search-forward sentence-end par-end t)
418 (skip-chars-backward " \t\n")
419 (goto-char par-end)))
420 (setq arg (1- arg)))
421 (constrain-to-field nil opoint t)))
423 (defun backward-sentence (&optional arg)
424 "Move backward to start of sentence. With arg, do it arg times.
425 See `forward-sentence' for more information."
426 (interactive "p")
427 (or arg (setq arg 1))
428 (forward-sentence (- arg)))
430 (defun kill-sentence (&optional arg)
431 "Kill from point to end of sentence.
432 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
433 (interactive "p")
434 (kill-region (point) (progn (forward-sentence arg) (point))))
436 (defun backward-kill-sentence (&optional arg)
437 "Kill back from point to start of sentence.
438 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
439 (interactive "p")
440 (kill-region (point) (progn (backward-sentence arg) (point))))
442 (defun mark-end-of-sentence (arg)
443 "Put mark at end of sentence. Arg works as in `forward-sentence'.
444 If this command is repeated, it marks the next ARG sentences after the
445 ones already marked."
446 (interactive "p")
447 (push-mark
448 (save-excursion
449 (if (and (eq last-command this-command) (mark t))
450 (goto-char (mark)))
451 (forward-sentence arg)
452 (point))
453 nil t))
455 (defun transpose-sentences (arg)
456 "Interchange this (next) and previous sentence."
457 (interactive "*p")
458 (transpose-subr 'forward-sentence arg))
460 ;;; paragraphs.el ends here