1 ;;; paragraphs.el --- paragraph and sentence parsing
3 ;; Copyright (C) 1985, 1986, 1987, 1991, 1994, 1995, 1996, 1997, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package provides the paragraph-oriented commands documented in the
31 (defgroup paragraphs nil
32 "Paragraph and sentence parsing."
35 (put 'use-hard-newlines
'permanent-local t
)
36 (define-minor-mode use-hard-newlines
37 "Minor mode to distinguish hard and soft newlines.
38 When active, the functions `newline' and `open-line' add the
39 text-property `hard' to newlines that they insert, and a line is
40 only considered as a candidate to match `paragraph-start' or
41 `paragraph-separate' if it follows a hard newline.
43 Prefix argument says to turn mode on if positive, off if negative.
44 When the mode is turned on, if there are newlines in the buffer but no hard
45 newlines, ask the user whether to mark as hard any newlines preceeding a
46 `paragraph-start' line. From a program, second arg INSERT specifies whether
47 to do this; it can be `never' to change nothing, t or `always' to force
48 marking, `guess' to try to do the right thing with no questions, nil
49 or anything else to ask the user.
51 Newlines not marked hard are called \"soft\", and are always internal
52 to paragraphs. The fill functions insert and delete only soft newlines."
55 (when use-hard-newlines
57 ;; Intuit hard newlines --
58 ;; mark as hard any newlines preceding a paragraph-start line.
59 (if (or (eq insert t
) (eq insert
'always
)
60 (and (not (eq 'never insert
))
61 (not (text-property-any (point-min) (point-max) 'hard t
))
63 (goto-char (point-min))
64 (search-forward "\n" nil t
))
65 (or (eq insert
'guess
)
66 (y-or-n-p "Make newlines between paragraphs hard? "))))
68 (goto-char (point-min))
69 (while (search-forward "\n" nil t
)
72 (when (looking-at paragraph-start
)
73 (set-hard-newline-properties (1- pos
) pos
))
74 ;; If paragraph-separate, newline after it is hard too.
75 (when (looking-at paragraph-separate
)
76 (set-hard-newline-properties (1- pos
) pos
)
79 (set-hard-newline-properties (point) (1+ (point)))))))))))
81 (defcustom paragraph-start
"\f\\|[ \t]*$" "\
82 Regexp for beginning of a line that starts OR separates paragraphs.
83 This regexp should match lines that separate paragraphs
84 and should also match lines that start a paragraph
85 \(and are part of that paragraph).
87 This is matched against the text at the left margin, which is not necessarily
88 the beginning of the line, so it should never use \"^\" as an anchor. This
89 ensures that the paragraph functions will work equally well within a region
90 of text indented by a margin setting.
92 The variable `paragraph-separate' specifies how to distinguish
93 lines that start paragraphs from lines that separate them.
95 If the variable `use-hard-newlines' is non-nil, then only lines following a
96 hard newline are considered to match."
99 (put 'paragraph-start
'safe-local-variable
'stringp
)
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."
117 (put 'paragraph-separate
'safe-local-variable
'stringp
)
119 (defcustom sentence-end-double-space t
120 "Non-nil means a single space does not end a sentence.
121 This is relevant for filling. See also `sentence-end-without-period'
122 and `colon-double-space'.
124 This value is used by the function `sentence-end' to construct the
125 regexp describing the end of a sentence, when the value of the variable
126 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
129 (put 'sentence-end-double-space
'safe-local-variable
'booleanp
)
131 (defcustom sentence-end-without-period nil
132 "Non-nil means a sentence will end without a period.
133 For example, a sentence in Thai text ends with double space but
136 This value is used by the function `sentence-end' to construct the
137 regexp describing the end of a sentence, when the value of the variable
138 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
141 (put 'sentence-end-without-period
'safe-local-variable
'booleanp
)
143 (defcustom sentence-end-without-space
144 "\e$B!#!%!)!*\e$A!##.#?#!\e$(0!$!%!)!*\e$(G!$!%!)!*\e(B"
145 "String of characters that end sentence without following spaces.
147 This value is used by the function `sentence-end' to construct the
148 regexp describing the end of a sentence, when the value of the variable
149 `sentence-end' is nil. See Info node `(elisp)Standard Regexps'."
152 (put 'sentence-end-without-space
'safe-local-variable
'stringp
)
154 (defcustom sentence-end nil
155 "Regexp describing the end of a sentence.
156 The value includes the whitespace following the sentence.
157 All paragraph boundaries also end sentences, regardless.
159 The value nil means to use the default value defined by the
160 function `sentence-end'. You should always use this function
161 to obtain the value of this variable."
163 :type
'(choice regexp
(const :tag
"Use default value" nil
)))
164 (put 'sentence-end
'safe-local-variable
'string-or-null-p
)
166 (defcustom sentence-end-base
"[.?!][]\"'\e$B!I\e$,1r}\e(B)}]*"
167 "Regexp matching the basic end of a sentence, not including following space."
171 (put 'sentence-end-base
'safe-local-variable
'stringp
)
173 (defun sentence-end ()
174 "Return the regexp describing the end of a sentence.
176 This function returns either the value of the variable `sentence-end'
177 if it is non-nil, or the default value constructed from the
178 variables `sentence-end-base', `sentence-end-double-space',
179 `sentence-end-without-period' and `sentence-end-without-space'.
181 The default value specifies that in order to be recognized as the
182 end of a sentence, the ending period, question mark, or exclamation point
183 must be followed by two spaces, with perhaps some closing delimiters
184 in between. See Info node `(elisp)Standard Regexps'."
186 (concat (if sentence-end-without-period
"\\w \\|")
189 (if sentence-end-double-space
190 "\\($\\| $\\|\t\\| \\)" "\\($\\|[\t ]\\)")
191 "\\|[" sentence-end-without-space
"]+"
195 (defcustom page-delimiter
"^\014"
196 "Regexp describing line-beginnings that separate pages."
199 (put 'page-delimiter
'safe-local-variable
'stringp
)
201 (defcustom paragraph-ignore-fill-prefix nil
202 "Non-nil means the paragraph commands are not affected by `fill-prefix'.
203 This is desirable in modes where blank lines are the paragraph delimiters."
206 (put 'paragraph-ignore-fill-prefix
'safe-local-variable
'booleanp
)
208 (defun forward-paragraph (&optional arg
)
209 "Move forward to end of paragraph.
210 With argument ARG, do it ARG times;
211 a negative argument ARG = -N means move backward N paragraphs.
213 A line which `paragraph-start' matches either separates paragraphs
214 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
215 A paragraph end is the beginning of a line which is not part of the paragraph
216 to which the end of the previous line belongs, or the end of the buffer.
217 Returns the count of paragraphs left to move."
219 (or arg
(setq arg
1))
220 (let* ((opoint (point))
222 (and fill-prefix
(not (equal fill-prefix
""))
223 (not paragraph-ignore-fill-prefix
)
224 (regexp-quote fill-prefix
)))
225 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
226 ;; These regexps shouldn't be anchored, because we look for them
227 ;; starting at the left-margin. This allows paragraph commands to
228 ;; work normally with indented text.
229 ;; This hack will not find problem cases like "whatever\\|^something".
230 (parstart (if (and (not (equal "" paragraph-start
))
231 (equal ?^
(aref paragraph-start
0)))
232 (substring paragraph-start
1)
234 (parsep (if (and (not (equal "" paragraph-separate
))
235 (equal ?^
(aref paragraph-separate
0)))
236 (substring paragraph-separate
1)
239 (if fill-prefix-regexp
241 fill-prefix-regexp
"[ \t]*$")
243 ;; This is used for searching.
244 (sp-parstart (concat "^[ \t]*\\(?:" parstart
"\\|" parsep
"\\)"))
246 (while (and (< arg
0) (not (bobp)))
247 (if (and (not (looking-at parsep
))
248 (re-search-backward "^\n" (max (1- (point)) (point-min)) t
)
252 ;; Move back over paragraph-separating lines.
253 (forward-char -
1) (beginning-of-line)
254 (while (and (not (bobp))
255 (progn (move-to-left-margin)
256 (looking-at parsep
)))
261 ;; Go to end of the previous (non-separating) line.
263 ;; Search back for line that starts or separates paragraphs.
264 (if (if fill-prefix-regexp
265 ;; There is a fill prefix; it overrides parstart.
266 (let (multiple-lines)
267 (while (and (progn (beginning-of-line) (not (bobp)))
268 (progn (move-to-left-margin)
269 (not (looking-at parsep
)))
270 (looking-at fill-prefix-regexp
))
271 (unless (= (point) start
)
272 (setq multiple-lines t
))
274 (move-to-left-margin)
275 ;; This deleted code caused a long hanging-indent line
276 ;; not to be filled together with the following lines.
277 ;; ;; Don't move back over a line before the paragraph
278 ;; ;; which doesn't start with fill-prefix
279 ;; ;; unless that is the only line we've moved over.
280 ;; (and (not (looking-at fill-prefix-regexp))
284 (while (and (re-search-backward sp-parstart nil
1)
286 ;; Found a candidate, but need to check if it is a
288 (progn (setq start
(point))
289 (move-to-left-margin)
290 (not (looking-at parsep
)))
291 (not (and (looking-at parstart
)
292 (or (not use-hard-newlines
)
295 (1- start
) 'hard
)))))
296 (setq found-start nil
)
301 ;; Move forward over paragraph separators.
302 ;; We know this cannot reach the place we started
303 ;; because we know we moved back over a non-separator.
304 (while (and (not (eobp))
305 (progn (move-to-left-margin)
306 (looking-at parsep
)))
308 ;; If line before paragraph is just margin, back up to there.
310 (if (> (current-column) (current-left-margin))
312 (skip-chars-backward " \t")
315 ;; No starter or separator line => use buffer beg.
316 (goto-char (point-min))))))
318 (while (and (> arg
0) (not (eobp)))
319 ;; Move forward over separator lines...
320 (while (and (not (eobp))
321 (progn (move-to-left-margin) (not (eobp)))
324 (unless (eobp) (setq arg
(1- arg
)))
325 ;; ... and one more line.
327 (if fill-prefix-regexp
328 ;; There is a fill prefix; it overrides parstart.
329 (while (and (not (eobp))
330 (progn (move-to-left-margin) (not (eobp)))
331 (not (looking-at parsep
))
332 (looking-at fill-prefix-regexp
))
334 (while (and (re-search-forward sp-parstart nil
1)
335 (progn (setq start
(match-beginning 0))
338 (progn (move-to-left-margin)
339 (not (looking-at parsep
)))
340 (or (not (looking-at parstart
))
341 (and use-hard-newlines
342 (not (get-text-property (1- start
) 'hard
)))))
344 (if (< (point) (point-max))
346 (constrain-to-field nil opoint t
)
347 ;; Return the number of steps that could not be done.
350 (defun backward-paragraph (&optional arg
)
351 "Move backward to start of paragraph.
352 With argument ARG, do it ARG times;
353 a negative argument ARG = -N means move forward N paragraphs.
355 A paragraph start is the beginning of a line which is a
356 `paragraph-start' or which is ordinary text and follows a
357 `paragraph-separate'ing line; except: if the first real line of a
358 paragraph is preceded by a blank line, the paragraph starts at that
361 See `forward-paragraph' for more information."
363 (or arg
(setq arg
1))
364 (forward-paragraph (- arg
)))
366 (defun mark-paragraph (&optional arg allow-extend
)
367 "Put point at beginning of this paragraph, mark at end.
368 The paragraph marked is the one that contains point or follows point.
370 With argument ARG, puts mark at end of a following paragraph, so that
371 the number of paragraphs marked equals ARG.
373 If ARG is negative, point is put at end of this paragraph, mark is put
374 at beginning of this or a previous paragraph.
376 Interactively, if this command is repeated
377 or (in Transient Mark mode) if the mark is active,
378 it marks the next ARG paragraphs after the ones already marked."
380 (unless arg
(setq arg
1))
382 (error "Cannot mark zero paragraphs"))
383 (cond ((and allow-extend
384 (or (and (eq last-command this-command
) (mark t
))
385 (and transient-mark-mode mark-active
)))
389 (forward-paragraph arg
)
392 (forward-paragraph arg
)
394 (backward-paragraph arg
))))
396 (defun kill-paragraph (arg)
397 "Kill forward to end of paragraph.
398 With arg N, kill forward to Nth end of paragraph;
399 negative arg -N means kill backward to Nth start of paragraph."
401 (kill-region (point) (progn (forward-paragraph arg
) (point))))
403 (defun backward-kill-paragraph (arg)
404 "Kill back to start of paragraph.
405 With arg N, kill back to Nth start of paragraph;
406 negative arg -N means kill forward to Nth end of paragraph."
408 (kill-region (point) (progn (backward-paragraph arg
) (point))))
410 (defun transpose-paragraphs (arg)
411 "Interchange this (or next) paragraph with previous one."
413 (transpose-subr 'forward-paragraph arg
))
415 (defun start-of-paragraph-text ()
416 (let ((opoint (point)) npoint
)
417 (forward-paragraph -
1)
418 (setq npoint
(point))
419 (skip-chars-forward " \t\n")
420 ;; If the range of blank lines found spans the original start point,
421 ;; try again from the beginning of it.
422 ;; Must be careful to avoid infinite loop
423 ;; when following a single return at start of buffer.
424 (if (and (>= (point) opoint
) (< npoint opoint
))
427 (if (> npoint
(point-min))
428 (start-of-paragraph-text))))))
430 (defun end-of-paragraph-text ()
431 (let ((opoint (point)))
432 (forward-paragraph 1)
433 (if (eq (preceding-char) ?
\n) (forward-char -
1))
434 (if (<= (point) opoint
)
437 (if (< (point) (point-max))
438 (end-of-paragraph-text))))))
440 (defun forward-sentence (&optional arg
)
441 "Move forward to next end of sentence. With argument, repeat.
442 With negative argument, move backward repeatedly to start of sentence.
444 The variable `sentence-end' is a regular expression that matches ends of
445 sentences. Also, every paragraph boundary terminates sentences as well."
447 (or arg
(setq arg
1))
448 (let ((opoint (point))
449 (sentence-end (sentence-end)))
452 (par-beg (save-excursion (start-of-paragraph-text) (point))))
453 (if (and (re-search-backward sentence-end par-beg t
)
454 (or (< (match-end 0) pos
)
455 (re-search-backward sentence-end par-beg t
)))
456 (goto-char (match-end 0))
457 (goto-char par-beg
)))
460 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
461 (if (re-search-forward sentence-end par-end t
)
462 (skip-chars-backward " \t\n")
463 (goto-char par-end
)))
465 (constrain-to-field nil opoint t
)))
467 (defun repunctuate-sentences ()
468 "Put two spaces at the end of sentences from point to the end of buffer.
469 It works using `query-replace-regexp'."
471 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
475 (defun backward-sentence (&optional arg
)
476 "Move backward to start of sentence. With arg, do it arg times.
477 See `forward-sentence' for more information."
479 (or arg
(setq arg
1))
480 (forward-sentence (- arg
)))
482 (defun kill-sentence (&optional arg
)
483 "Kill from point to end of sentence.
484 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
486 (kill-region (point) (progn (forward-sentence arg
) (point))))
488 (defun backward-kill-sentence (&optional arg
)
489 "Kill back from point to start of sentence.
490 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
492 (kill-region (point) (progn (backward-sentence arg
) (point))))
494 (defun mark-end-of-sentence (arg)
495 "Put mark at end of sentence. Arg works as in `forward-sentence'.
496 If this command is repeated, it marks the next ARG sentences after the
497 ones already marked."
501 (if (and (eq last-command this-command
) (mark t
))
503 (forward-sentence arg
)
507 (defun transpose-sentences (arg)
508 "Interchange this (next) and previous sentence."
510 (transpose-subr 'forward-sentence arg
))
513 ;; coding: iso-2022-7bit
516 ;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
517 ;;; paragraphs.el ends here