Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; paragraphs.el --- paragraph and sentence parsing. |
2 | ||
9750e079 ER |
3 | ;; Copyright (C) 1985, 86, 87, 1991 Free Software Foundation, Inc. |
4 | ||
4821e2af | 5 | ;; Maintainer: FSF |
d7b4d18f | 6 | ;; Keywords: wp |
4821e2af | 7 | |
a2535589 JA |
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 | |
29add8b9 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
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 | |
22 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | ||
edbd2f74 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; This package provides the paragraph-oriented commands documented in the | |
27 | ;; Emacs manual. | |
28 | ||
4821e2af | 29 | ;;; Code: |
a2535589 | 30 | |
6503cec3 JB |
31 | (defconst paragraph-start "^[ \t\n\f]" "\ |
32 | *Regexp for beginning of a line that starts OR separates paragraphs.") | |
33 | ||
6503cec3 JB |
34 | (defconst paragraph-separate "^[ \t\f]*$" "\ |
35 | *Regexp for beginning of a line that separates paragraphs. | |
36 | If you change this, you may have to change paragraph-start also.") | |
37 | ||
6503cec3 JB |
38 | (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\ |
39 | *Regexp describing the end of a sentence. | |
51534471 JB |
40 | All paragraph boundaries also end sentences, regardless. |
41 | ||
42 | In order to be recognized as the end of a sentence, the ending period, | |
43 | question mark, or exclamation point must be followed by two spaces, | |
44 | unless it's inside some sort of quotes or parenthesis.") | |
6503cec3 | 45 | |
6503cec3 JB |
46 | (defconst page-delimiter "^\014" "\ |
47 | *Regexp describing line-beginnings that separate pages.") | |
48 | ||
6503cec3 JB |
49 | (defvar paragraph-ignore-fill-prefix nil "\ |
50 | Non-nil means the paragraph commands are not affected by `fill-prefix'. | |
51 | This is desirable in modes where blank lines are the paragraph delimiters.") | |
77176e73 | 52 | |
a2535589 JA |
53 | |
54 | (defun forward-paragraph (&optional arg) | |
55 | "Move forward to end of paragraph. | |
56 | With arg N, do it N times; negative arg -N means move forward N paragraphs. | |
57 | ||
58 | A line which `paragraph-start' matches either separates paragraphs | |
59 | \(if `paragraph-separate' matches it also) or is the first line of a paragraph. | |
60 | A paragraph end is the beginning of a line which is not part of the paragraph | |
61 | to which the end of the previous line belongs, or the end of the buffer." | |
62 | (interactive "p") | |
63 | (or arg (setq arg 1)) | |
64 | (let* ((fill-prefix-regexp | |
65 | (and fill-prefix (not (equal fill-prefix "")) | |
66 | (not paragraph-ignore-fill-prefix) | |
67 | (regexp-quote fill-prefix))) | |
68 | (paragraph-separate | |
69 | (if fill-prefix-regexp | |
70 | (concat paragraph-separate "\\|^" | |
71 | fill-prefix-regexp "[ \t]*$") | |
72 | paragraph-separate))) | |
73 | (while (< arg 0) | |
74 | (if (and (not (looking-at paragraph-separate)) | |
75 | (re-search-backward "^\n" (max (1- (point)) (point-min)) t)) | |
76 | nil | |
77 | (forward-char -1) (beginning-of-line) | |
78 | (while (and (not (bobp)) (looking-at paragraph-separate)) | |
79 | (forward-line -1)) | |
80 | (end-of-line) | |
81 | ;; Search back for line that starts or separates paragraphs. | |
82 | (if (if fill-prefix-regexp | |
83 | ;; There is a fill prefix; it overrides paragraph-start. | |
84 | (progn | |
85 | (while (progn (beginning-of-line) | |
86 | (and (not (bobp)) | |
87 | (not (looking-at paragraph-separate)) | |
88 | (looking-at fill-prefix-regexp))) | |
89 | (forward-line -1)) | |
90 | (not (bobp))) | |
91 | (re-search-backward paragraph-start nil t)) | |
92 | ;; Found one. | |
93 | (progn | |
94 | (while (and (not (eobp)) (looking-at paragraph-separate)) | |
95 | (forward-line 1)) | |
96 | (if (eq (char-after (- (point) 2)) ?\n) | |
97 | (forward-line -1))) | |
98 | ;; No starter or separator line => use buffer beg. | |
99 | (goto-char (point-min)))) | |
100 | (setq arg (1+ arg))) | |
101 | (while (> arg 0) | |
102 | (beginning-of-line) | |
103 | (while (prog1 (and (not (eobp)) | |
104 | (looking-at paragraph-separate)) | |
105 | (forward-line 1))) | |
106 | (if fill-prefix-regexp | |
107 | ;; There is a fill prefix; it overrides paragraph-start. | |
108 | (while (and (not (eobp)) | |
109 | (not (looking-at paragraph-separate)) | |
110 | (looking-at fill-prefix-regexp)) | |
111 | (forward-line 1)) | |
112 | (if (re-search-forward paragraph-start nil t) | |
113 | (goto-char (match-beginning 0)) | |
114 | (goto-char (point-max)))) | |
115 | (setq arg (1- arg))))) | |
116 | ||
117 | (defun backward-paragraph (&optional arg) | |
118 | "Move backward to start of paragraph. | |
119 | With arg N, do it N times; negative arg -N means move forward N paragraphs. | |
120 | ||
23b34992 BP |
121 | A paragraph start is the beginning of a line which is a |
122 | `first-line-of-paragraph' or which is ordinary text and follows a | |
123 | paragraph-separating line; except: if the first real line of a | |
124 | paragraph is preceded by a blank line, the paragraph starts at that | |
125 | blank line. | |
126 | ||
127 | See `forward-paragraph' for more information." | |
a2535589 JA |
128 | (interactive "p") |
129 | (or arg (setq arg 1)) | |
130 | (forward-paragraph (- arg))) | |
131 | ||
132 | (defun mark-paragraph () | |
133 | "Put point at beginning of this paragraph, mark at end. | |
134 | The paragraph marked is the one that contains point or follows point." | |
135 | (interactive) | |
136 | (forward-paragraph 1) | |
0b108c01 | 137 | (push-mark nil t t) |
a2535589 JA |
138 | (backward-paragraph 1)) |
139 | ||
140 | (defun kill-paragraph (arg) | |
141 | "Kill forward to end of paragraph. | |
142 | With arg N, kill forward to Nth end of paragraph; | |
143 | negative arg -N means kill backward to Nth start of paragraph." | |
23b34992 | 144 | (interactive "p") |
2247b0fc | 145 | (kill-region (point) (save-excursion (forward-paragraph arg) (point)))) |
a2535589 JA |
146 | |
147 | (defun backward-kill-paragraph (arg) | |
148 | "Kill back to start of paragraph. | |
149 | With arg N, kill back to Nth start of paragraph; | |
150 | negative arg -N means kill forward to Nth end of paragraph." | |
23b34992 | 151 | (interactive "p") |
2247b0fc | 152 | (kill-region (point) (save-excursion (backward-paragraph arg) (point)))) |
a2535589 JA |
153 | |
154 | (defun transpose-paragraphs (arg) | |
155 | "Interchange this (or next) paragraph with previous one." | |
156 | (interactive "*p") | |
157 | (transpose-subr 'forward-paragraph arg)) | |
158 | ||
159 | (defun start-of-paragraph-text () | |
160 | (let ((opoint (point)) npoint) | |
161 | (forward-paragraph -1) | |
162 | (setq npoint (point)) | |
163 | (skip-chars-forward " \t\n") | |
b4e6c391 RS |
164 | ;; If the range of blank lines found spans the original start point, |
165 | ;; try again from the beginning of it. | |
166 | ;; Must be careful to avoid infinite loop | |
167 | ;; when following a single return at start of buffer. | |
168 | (if (and (>= (point) opoint) (< npoint opoint)) | |
a2535589 JA |
169 | (progn |
170 | (goto-char npoint) | |
171 | (if (> npoint (point-min)) | |
172 | (start-of-paragraph-text)))))) | |
173 | ||
174 | (defun end-of-paragraph-text () | |
175 | (let ((opoint (point))) | |
176 | (forward-paragraph 1) | |
177 | (if (eq (preceding-char) ?\n) (forward-char -1)) | |
178 | (if (<= (point) opoint) | |
179 | (progn | |
180 | (forward-char 1) | |
181 | (if (< (point) (point-max)) | |
182 | (end-of-paragraph-text)))))) | |
183 | ||
184 | (defun forward-sentence (&optional arg) | |
51534471 | 185 | "Move forward to next `sentence-end'. With argument, repeat. |
23b34992 | 186 | With negative argument, move backward repeatedly to `sentence-beginning'. |
a2535589 | 187 | |
23b34992 BP |
188 | The variable `sentence-end' is a regular expression that matches ends of |
189 | sentences. Also, every paragraph boundary terminates sentences as well." | |
a2535589 JA |
190 | (interactive "p") |
191 | (or arg (setq arg 1)) | |
192 | (while (< arg 0) | |
193 | (let ((par-beg (save-excursion (start-of-paragraph-text) (point)))) | |
194 | (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t) | |
195 | (goto-char (1- (match-end 0))) | |
196 | (goto-char par-beg))) | |
197 | (setq arg (1+ arg))) | |
198 | (while (> arg 0) | |
199 | (let ((par-end (save-excursion (end-of-paragraph-text) (point)))) | |
200 | (if (re-search-forward sentence-end par-end t) | |
201 | (skip-chars-backward " \t\n") | |
202 | (goto-char par-end))) | |
203 | (setq arg (1- arg)))) | |
204 | ||
205 | (defun backward-sentence (&optional arg) | |
206 | "Move backward to start of sentence. With arg, do it arg times. | |
23b34992 | 207 | See `forward-sentence' for more information." |
a2535589 JA |
208 | (interactive "p") |
209 | (or arg (setq arg 1)) | |
210 | (forward-sentence (- arg))) | |
211 | ||
212 | (defun kill-sentence (&optional arg) | |
213 | "Kill from point to end of sentence. | |
214 | With arg, repeat; negative arg -N means kill back to Nth start of sentence." | |
215 | (interactive "*p") | |
2247b0fc | 216 | (kill-region (point) (save-excursion (forward-sentence arg) (point)))) |
a2535589 JA |
217 | |
218 | (defun backward-kill-sentence (&optional arg) | |
219 | "Kill back from point to start of sentence. | |
220 | With arg, repeat, or kill forward to Nth end of sentence if negative arg -N." | |
221 | (interactive "*p") | |
7270bdb0 | 222 | (kill-region (point) (save-excursion (backward-sentence arg) (point)))) |
a2535589 JA |
223 | |
224 | (defun mark-end-of-sentence (arg) | |
23b34992 | 225 | "Put mark at end of sentence. Arg works as in `forward-sentence'." |
a2535589 JA |
226 | (interactive "p") |
227 | (push-mark | |
228 | (save-excursion | |
229 | (forward-sentence arg) | |
a524dc5b RS |
230 | (point)) |
231 | nil t)) | |
a2535589 JA |
232 | |
233 | (defun transpose-sentences (arg) | |
234 | "Interchange this (next) and previous sentence." | |
235 | (interactive "*p") | |
236 | (transpose-subr 'forward-sentence arg)) | |
6594deb0 ER |
237 | |
238 | ;;; paragraphs.el ends here |