]> code.delx.au - gnu-emacs/blob - lisp/textmodes/paragraphs.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / textmodes / paragraphs.el
1 ;;; paragraphs.el --- paragraph and sentence parsing
2
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1994, 1995, 1996, 1997, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
5 ;; Free Software Foundation, Inc.
6
7 ;; Maintainer: FSF
8 ;; Keywords: wp
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides the paragraph-oriented commands documented in the
28 ;; Emacs manual.
29
30 ;;; Code:
31
32 (defgroup paragraphs nil
33 "Paragraph and sentence parsing."
34 :group 'editing)
35
36 (put 'use-hard-newlines 'permanent-local t)
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.
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 preceding 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 :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)))))))))))
81
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).
87
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.
92
93 The variable `paragraph-separate' specifies how to distinguish
94 lines that start paragraphs from lines that separate them.
95
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)
100 (put 'paragraph-start 'safe-local-variable 'stringp)
101
102 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
103 ;; It is assumed that paragraph-separate is distinctive enough to be believed
104 ;; whenever it occurs, while it is reasonable to set paragraph-start to
105 ;; something very minimal, even including "." (which makes every hard newline
106 ;; start a new paragraph).
107
108 (defcustom paragraph-separate "[ \t\f]*$"
109 "Regexp for beginning of a line that separates paragraphs.
110 If you change this, you may have to change `paragraph-start' also.
111
112 This is matched against the text at the left margin, which is not necessarily
113 the beginning of the line, so it should not use \"^\" as an anchor. This
114 ensures that the paragraph functions will work equally within a region of
115 text indented by a margin setting."
116 :group 'paragraphs
117 :type 'regexp)
118 (put 'paragraph-separate 'safe-local-variable 'stringp)
119
120 (defcustom sentence-end-double-space t
121 "Non-nil means a single space does not end a sentence.
122 This is relevant for filling. See also `sentence-end-without-period'
123 and `colon-double-space'.
124
125 This value is used by the function `sentence-end' to construct the
126 regexp describing the end of a sentence, when the value of the variable
127 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
128 :type 'boolean
129 :group 'fill)
130 (put 'sentence-end-double-space 'safe-local-variable 'booleanp)
131
132 (defcustom sentence-end-without-period nil
133 "Non-nil means a sentence will end without a period.
134 For example, a sentence in Thai text ends with double space but
135 without a period.
136
137 This value is used by the function `sentence-end' to construct the
138 regexp describing the end of a sentence, when the value of the variable
139 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
140 :type 'boolean
141 :group 'fill)
142 (put 'sentence-end-without-period 'safe-local-variable 'booleanp)
143
144 (defcustom sentence-end-without-space
145 "。.?!"
146 "String of characters that end sentence without following spaces.
147
148 This value is used by the function `sentence-end' to construct the
149 regexp describing the end of a sentence, when the value of the variable
150 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
151 :group 'paragraphs
152 :type 'string)
153 (put 'sentence-end-without-space 'safe-local-variable 'stringp)
154
155 (defcustom sentence-end nil
156 "Regexp describing the end of a sentence.
157 The value includes the whitespace following the sentence.
158 All paragraph boundaries also end sentences, regardless.
159
160 The value nil means to use the default value defined by the
161 function `sentence-end'. You should always use this function
162 to obtain the value of this variable."
163 :group 'paragraphs
164 :type '(choice regexp (const :tag "Use default value" nil)))
165 (put 'sentence-end 'safe-local-variable 'string-or-null-p)
166
167 (defcustom sentence-end-base "[.?!][]\"'”)}]*"
168 "Regexp matching the basic end of a sentence, not including following space."
169 :group 'paragraphs
170 :type 'string
171 :version "22.1")
172 (put 'sentence-end-base 'safe-local-variable 'stringp)
173
174 (defun sentence-end ()
175 "Return the regexp describing the end of a sentence.
176
177 This function returns either the value of the variable `sentence-end'
178 if it is non-nil, or the default value constructed from the
179 variables `sentence-end-base', `sentence-end-double-space',
180 `sentence-end-without-period' and `sentence-end-without-space'.
181
182 The default value specifies that in order to be recognized as the
183 end of a sentence, the ending period, question mark, or exclamation point
184 must be followed by two spaces, with perhaps some closing delimiters
185 in between. See Info node `(elisp)Standard Regexps'."
186 (or sentence-end
187 ;; We accept non-break space along with space.
188 (concat (if sentence-end-without-period "\\w[ \u00a0][ \u00a0]\\|")
189 "\\("
190 sentence-end-base
191 (if sentence-end-double-space
192 "\\($\\|[ \u00a0]$\\|\t\\|[ \u00a0][ \u00a0]\\)" "\\($\\|[\t \u00a0]\\)")
193 "\\|[" sentence-end-without-space "]+"
194 "\\)"
195 "[ \u00a0\t\n]*")))
196
197 (defcustom page-delimiter "^\014"
198 "Regexp describing line-beginnings that separate pages."
199 :group 'paragraphs
200 :type 'regexp)
201 (put 'page-delimiter 'safe-local-variable 'stringp)
202
203 (defcustom paragraph-ignore-fill-prefix nil
204 "Non-nil means the paragraph commands are not affected by `fill-prefix'.
205 This is desirable in modes where blank lines are the paragraph delimiters."
206 :group 'paragraphs
207 :type 'boolean)
208 (put 'paragraph-ignore-fill-prefix 'safe-local-variable 'booleanp)
209
210 (defun forward-paragraph (&optional arg)
211 "Move forward to end of paragraph.
212 With argument ARG, do it ARG times;
213 a negative argument ARG = -N means move backward N paragraphs.
214
215 A line which `paragraph-start' matches either separates paragraphs
216 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
217 A paragraph end is the beginning of a line which is not part of the paragraph
218 to which the end of the previous line belongs, or the end of the buffer.
219 Returns the count of paragraphs left to move."
220 (interactive "^p")
221 (or arg (setq arg 1))
222 (let* ((opoint (point))
223 (fill-prefix-regexp
224 (and fill-prefix (not (equal fill-prefix ""))
225 (not paragraph-ignore-fill-prefix)
226 (regexp-quote fill-prefix)))
227 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
228 ;; These regexps shouldn't be anchored, because we look for them
229 ;; starting at the left-margin. This allows paragraph commands to
230 ;; work normally with indented text.
231 ;; This hack will not find problem cases like "whatever\\|^something".
232 (parstart (if (and (not (equal "" paragraph-start))
233 (equal ?^ (aref paragraph-start 0)))
234 (substring paragraph-start 1)
235 paragraph-start))
236 (parsep (if (and (not (equal "" paragraph-separate))
237 (equal ?^ (aref paragraph-separate 0)))
238 (substring paragraph-separate 1)
239 paragraph-separate))
240 (parsep
241 (if fill-prefix-regexp
242 (concat parsep "\\|"
243 fill-prefix-regexp "[ \t]*$")
244 parsep))
245 ;; This is used for searching.
246 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
247 start found-start)
248 (while (and (< arg 0) (not (bobp)))
249 (if (and (not (looking-at parsep))
250 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
251 (looking-at parsep))
252 (setq arg (1+ arg))
253 (setq start (point))
254 ;; Move back over paragraph-separating lines.
255 (forward-char -1) (beginning-of-line)
256 (while (and (not (bobp))
257 (progn (move-to-left-margin)
258 (looking-at parsep)))
259 (forward-line -1))
260 (if (bobp)
261 nil
262 (setq arg (1+ arg))
263 ;; Go to end of the previous (non-separating) line.
264 (end-of-line)
265 ;; Search back for line that starts or separates paragraphs.
266 (if (if fill-prefix-regexp
267 ;; There is a fill prefix; it overrides parstart.
268 (let (multiple-lines)
269 (while (and (progn (beginning-of-line) (not (bobp)))
270 (progn (move-to-left-margin)
271 (not (looking-at parsep)))
272 (looking-at fill-prefix-regexp))
273 (unless (= (point) start)
274 (setq multiple-lines t))
275 (forward-line -1))
276 (move-to-left-margin)
277 ;; This deleted code caused a long hanging-indent line
278 ;; not to be filled together with the following lines.
279 ;; ;; Don't move back over a line before the paragraph
280 ;; ;; which doesn't start with fill-prefix
281 ;; ;; unless that is the only line we've moved over.
282 ;; (and (not (looking-at fill-prefix-regexp))
283 ;; multiple-lines
284 ;; (forward-line 1))
285 (not (bobp)))
286 (while (and (re-search-backward sp-parstart nil 1)
287 (setq found-start t)
288 ;; Found a candidate, but need to check if it is a
289 ;; REAL parstart.
290 (progn (setq start (point))
291 (move-to-left-margin)
292 (not (looking-at parsep)))
293 (not (and (looking-at parstart)
294 (or (not use-hard-newlines)
295 (bobp)
296 (get-text-property
297 (1- start) 'hard)))))
298 (setq found-start nil)
299 (goto-char start))
300 found-start)
301 ;; Found one.
302 (progn
303 ;; Move forward over paragraph separators.
304 ;; We know this cannot reach the place we started
305 ;; because we know we moved back over a non-separator.
306 (while (and (not (eobp))
307 (progn (move-to-left-margin)
308 (looking-at parsep)))
309 (forward-line 1))
310 ;; If line before paragraph is just margin, back up to there.
311 (end-of-line 0)
312 (if (> (current-column) (current-left-margin))
313 (forward-char 1)
314 (skip-chars-backward " \t")
315 (if (not (bolp))
316 (forward-line 1))))
317 ;; No starter or separator line => use buffer beg.
318 (goto-char (point-min))))))
319
320 (while (and (> arg 0) (not (eobp)))
321 ;; Move forward over separator lines...
322 (while (and (not (eobp))
323 (progn (move-to-left-margin) (not (eobp)))
324 (looking-at parsep))
325 (forward-line 1))
326 (unless (eobp) (setq arg (1- arg)))
327 ;; ... and one more line.
328 (forward-line 1)
329 (if fill-prefix-regexp
330 ;; There is a fill prefix; it overrides parstart.
331 (while (and (not (eobp))
332 (progn (move-to-left-margin) (not (eobp)))
333 (not (looking-at parsep))
334 (looking-at fill-prefix-regexp))
335 (forward-line 1))
336 (while (and (re-search-forward sp-parstart nil 1)
337 (progn (setq start (match-beginning 0))
338 (goto-char start)
339 (not (eobp)))
340 (progn (move-to-left-margin)
341 (not (looking-at parsep)))
342 (or (not (looking-at parstart))
343 (and use-hard-newlines
344 (not (get-text-property (1- start) 'hard)))))
345 (forward-char 1))
346 (if (< (point) (point-max))
347 (goto-char start))))
348 (constrain-to-field nil opoint t)
349 ;; Return the number of steps that could not be done.
350 arg))
351
352 (defun backward-paragraph (&optional arg)
353 "Move backward to start of paragraph.
354 With argument ARG, do it ARG times;
355 a negative argument ARG = -N means move forward N paragraphs.
356
357 A paragraph start is the beginning of a line which is a
358 `paragraph-start' or which is ordinary text and follows a
359 `paragraph-separate'ing line; except: if the first real line of a
360 paragraph is preceded by a blank line, the paragraph starts at that
361 blank line.
362
363 See `forward-paragraph' for more information."
364 (interactive "^p")
365 (or arg (setq arg 1))
366 (forward-paragraph (- arg)))
367
368 (defun mark-paragraph (&optional arg allow-extend)
369 "Put point at beginning of this paragraph, mark at end.
370 The paragraph marked is the one that contains point or follows point.
371
372 With argument ARG, puts mark at end of a following paragraph, so that
373 the number of paragraphs marked equals ARG.
374
375 If ARG is negative, point is put at end of this paragraph, mark is put
376 at beginning of this or a previous paragraph.
377
378 Interactively, if this command is repeated
379 or (in Transient Mark mode) if the mark is active,
380 it marks the next ARG paragraphs after the ones already marked."
381 (interactive "p\np")
382 (unless arg (setq arg 1))
383 (when (zerop arg)
384 (error "Cannot mark zero paragraphs"))
385 (cond ((and allow-extend
386 (or (and (eq last-command this-command) (mark t))
387 (and transient-mark-mode mark-active)))
388 (set-mark
389 (save-excursion
390 (goto-char (mark))
391 (forward-paragraph arg)
392 (point))))
393 (t
394 (forward-paragraph arg)
395 (push-mark nil t t)
396 (backward-paragraph arg))))
397
398 (defun kill-paragraph (arg)
399 "Kill forward to end of paragraph.
400 With arg N, kill forward to Nth end of paragraph;
401 negative arg -N means kill backward to Nth start of paragraph."
402 (interactive "p")
403 (kill-region (point) (progn (forward-paragraph arg) (point))))
404
405 (defun backward-kill-paragraph (arg)
406 "Kill back to start of paragraph.
407 With arg N, kill back to Nth start of paragraph;
408 negative arg -N means kill forward to Nth end of paragraph."
409 (interactive "p")
410 (kill-region (point) (progn (backward-paragraph arg) (point))))
411
412 (defun transpose-paragraphs (arg)
413 "Interchange the current paragraph with the next one.
414 With prefix argument ARG a non-zero integer, moves the current
415 paragraph past ARG paragraphs, leaving point after the current paragraph.
416 If ARG is positive, moves the current paragraph forwards, if
417 ARG is negative moves it backwards. If ARG is zero, exchanges
418 the current paragraph with the one containing the mark."
419 (interactive "*p")
420 (transpose-subr 'forward-paragraph arg))
421
422 (defun start-of-paragraph-text ()
423 (let ((opoint (point)) npoint)
424 (forward-paragraph -1)
425 (setq npoint (point))
426 (skip-chars-forward " \t\n")
427 ;; If the range of blank lines found spans the original start point,
428 ;; try again from the beginning of it.
429 ;; Must be careful to avoid infinite loop
430 ;; when following a single return at start of buffer.
431 (if (and (>= (point) opoint) (< npoint opoint))
432 (progn
433 (goto-char npoint)
434 (if (> npoint (point-min))
435 (start-of-paragraph-text))))))
436
437 (defun end-of-paragraph-text ()
438 (let ((opoint (point)))
439 (forward-paragraph 1)
440 (if (eq (preceding-char) ?\n) (forward-char -1))
441 (if (<= (point) opoint)
442 (progn
443 (forward-char 1)
444 (if (< (point) (point-max))
445 (end-of-paragraph-text))))))
446
447 (defun forward-sentence (&optional arg)
448 "Move forward to next end of sentence. With argument, repeat.
449 With negative argument, move backward repeatedly to start of sentence.
450
451 The variable `sentence-end' is a regular expression that matches ends of
452 sentences. Also, every paragraph boundary terminates sentences as well."
453 (interactive "^p")
454 (or arg (setq arg 1))
455 (let ((opoint (point))
456 (sentence-end (sentence-end)))
457 (while (< arg 0)
458 (let ((pos (point))
459 ;; We used to use (start-of-paragraph-text), but this can
460 ;; prevent sentence-end from matching if it is anchored at
461 ;; BOL and the paragraph starts indented.
462 (par-beg (save-excursion (backward-paragraph) (point))))
463 (if (and (re-search-backward sentence-end par-beg t)
464 (or (< (match-end 0) pos)
465 (re-search-backward sentence-end par-beg t)))
466 (goto-char (match-end 0))
467 (goto-char par-beg)))
468 (setq arg (1+ arg)))
469 (while (> arg 0)
470 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
471 (if (re-search-forward sentence-end par-end t)
472 (skip-chars-backward " \t\n")
473 (goto-char par-end)))
474 (setq arg (1- arg)))
475 (constrain-to-field nil opoint t)))
476
477 (defun repunctuate-sentences ()
478 "Put two spaces at the end of sentences from point to the end of buffer.
479 It works using `query-replace-regexp'."
480 (interactive)
481 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
482 "\\1\\2\\3 "))
483
484
485 (defun backward-sentence (&optional arg)
486 "Move backward to start of sentence. With arg, do it arg times.
487 See `forward-sentence' for more information."
488 (interactive "^p")
489 (or arg (setq arg 1))
490 (forward-sentence (- arg)))
491
492 (defun kill-sentence (&optional arg)
493 "Kill from point to end of sentence.
494 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
495 (interactive "p")
496 (kill-region (point) (progn (forward-sentence arg) (point))))
497
498 (defun backward-kill-sentence (&optional arg)
499 "Kill back from point to start of sentence.
500 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
501 (interactive "p")
502 (kill-region (point) (progn (backward-sentence arg) (point))))
503
504 (defun mark-end-of-sentence (arg)
505 "Put mark at end of sentence. Arg works as in `forward-sentence'.
506 If this command is repeated, it marks the next ARG sentences after the
507 ones already marked."
508 (interactive "p")
509 (push-mark
510 (save-excursion
511 (if (and (eq last-command this-command) (mark t))
512 (goto-char (mark)))
513 (forward-sentence arg)
514 (point))
515 nil t))
516
517 (defun transpose-sentences (arg)
518 "Interchange the current sentence with the next one.
519 With prefix argument ARG a non-zero integer, moves the current
520 sentence past ARG sentences, leaving point after the current sentence.
521 If ARG is positive, moves the current sentence forwards, if
522 ARG is negative moves it backwards. If ARG is zero, exchanges
523 the current sentence with the one containing the mark."
524 (interactive "*p")
525 (transpose-subr 'forward-sentence arg))
526
527 ;; Local Variables:
528 ;; coding: utf-8
529 ;; End:
530
531 ;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
532 ;;; paragraphs.el ends here