Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; paragraphs.el --- paragraph and sentence parsing. |
2 | ||
312cac0e | 3 | ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96 Free Software Foundation, Inc. |
9750e079 | 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 | |
b578f267 EN |
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. | |
a2535589 | 24 | |
edbd2f74 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; This package provides the paragraph-oriented commands documented in the | |
28 | ;; Emacs manual. | |
29 | ||
4821e2af | 30 | ;;; Code: |
a2535589 | 31 | |
55cc5677 BG |
32 | (defvar use-hard-newlines nil |
33 | "Non-nil means to distinguish hard and soft newlines. | |
965eb84a RS |
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 | |
55cc5677 | 41 | only considered as a candidate to match `paragraph-start' or |
965eb84a | 42 | `paragraph-separate' if it follows a hard newline. |
55cc5677 | 43 | |
965eb84a RS |
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))) | |
55cc5677 | 87 | |
d7fa5aa2 | 88 | (defvar paragraph-start "[ \t\n\f]" "\ |
1f2007b3 RS |
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). | |
a37669ec | 93 | |
55cc5677 BG |
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 | ||
1f2007b3 | 99 | The variable `paragraph-separate' specifies how to distinguish |
a37669ec RS |
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.") | |
6503cec3 | 104 | |
55cc5677 BG |
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 | ||
d7fa5aa2 | 111 | (defvar paragraph-separate "[ \t\f]*$" "\ |
6503cec3 | 112 | *Regexp for beginning of a line that separates paragraphs. |
a37669ec RS |
113 | If you change this, you may have to change paragraph-start also. |
114 | ||
55cc5677 BG |
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.") | |
6503cec3 | 119 | |
d7fa5aa2 | 120 | (defvar sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\ |
6503cec3 | 121 | *Regexp describing the end of a sentence. |
51534471 JB |
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.") | |
6503cec3 | 127 | |
d7fa5aa2 | 128 | (defvar page-delimiter "^\014" "\ |
6503cec3 JB |
129 | *Regexp describing line-beginnings that separate pages.") |
130 | ||
6503cec3 JB |
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.") | |
77176e73 | 134 | |
a2535589 JA |
135 | (defun forward-paragraph (&optional arg) |
136 | "Move forward to end of paragraph. | |
94d63a23 RS |
137 | With argument ARG, do it ARG times; |
138 | a negative argument ARG = -N means move backward N paragraphs. | |
a2535589 JA |
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))) | |
55cc5677 BG |
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-start)) | |
160 | (equal ?^ (aref paragraph-separate 0))) | |
161 | (substring paragraph-separate 1) | |
162 | paragraph-separate)) | |
a2535589 JA |
163 | (paragraph-separate |
164 | (if fill-prefix-regexp | |
55cc5677 | 165 | (concat paragraph-separate "\\|" |
a2535589 | 166 | fill-prefix-regexp "[ \t]*$") |
55cc5677 BG |
167 | paragraph-separate)) |
168 | ;; This is used for searching. | |
169 | (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)")) | |
170 | start) | |
8a2a4ced | 171 | (while (and (< arg 0) (not (bobp))) |
55cc5677 | 172 | (if (and (not (looking-at paragraph-separate)) |
a37669ec | 173 | (re-search-backward "^\n" (max (1- (point)) (point-min)) t) |
55cc5677 | 174 | (looking-at paragraph-separate)) |
a2535589 | 175 | nil |
2be01738 | 176 | (setq start (point)) |
8a2a4ced | 177 | ;; Move back over paragraph-separating lines. |
a2535589 | 178 | (forward-char -1) (beginning-of-line) |
a37669ec | 179 | (while (and (not (bobp)) |
55cc5677 BG |
180 | (progn (move-to-left-margin) |
181 | (looking-at paragraph-separate))) | |
182 | (forward-line -1)) | |
8a2a4ced RS |
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. | |
2be01738 | 190 | (let (multiple-lines) |
55cc5677 BG |
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)) | |
2be01738 RS |
195 | (if (not (= (point) start)) |
196 | (setq multiple-lines t)) | |
55cc5677 | 197 | (forward-line -1)) |
2be01738 | 198 | (move-to-left-margin) |
b53ce41d RS |
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)) | |
2be01738 | 207 | (not (bobp))) |
55cc5677 BG |
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. | |
a37669ec | 211 | (not (bobp)) |
55cc5677 BG |
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)) | |
a37669ec | 220 | (> (point) (point-min))) |
8a2a4ced RS |
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. | |
55cc5677 BG |
226 | (while (and (not (eobp)) |
227 | (progn (move-to-left-margin) | |
228 | (looking-at paragraph-separate))) | |
8a2a4ced | 229 | (forward-line 1)) |
55cc5677 BG |
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)))) | |
8a2a4ced RS |
237 | ;; No starter or separator line => use buffer beg. |
238 | (goto-char (point-min))))) | |
a2535589 | 239 | (setq arg (1+ arg))) |
8a2a4ced | 240 | (while (and (> arg 0) (not (eobp))) |
88c2fc2c | 241 | ;; Move forward over separator lines, and one more line. |
a2535589 | 242 | (while (prog1 (and (not (eobp)) |
55cc5677 BG |
243 | (progn (move-to-left-margin) (not (eobp))) |
244 | (looking-at paragraph-separate)) | |
a37669ec | 245 | (forward-line 1))) |
a2535589 JA |
246 | (if fill-prefix-regexp |
247 | ;; There is a fill prefix; it overrides paragraph-start. | |
248 | (while (and (not (eobp)) | |
55cc5677 BG |
249 | (progn (move-to-left-margin) (not (eobp))) |
250 | (not (looking-at paragraph-separate)) | |
a2535589 JA |
251 | (looking-at fill-prefix-regexp)) |
252 | (forward-line 1)) | |
55cc5677 | 253 | (while (and (re-search-forward sp-paragraph-start nil 1) |
55cc5677 BG |
254 | (progn (setq start (match-beginning 0)) |
255 | (goto-char start) | |
4669fb3c RS |
256 | (not (eobp))) |
257 | (progn (move-to-left-margin) | |
55cc5677 BG |
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))))) | |
a37669ec RS |
262 | (forward-char 1)) |
263 | (if (< (point) (point-max)) | |
55cc5677 | 264 | (goto-char start))) |
a2535589 JA |
265 | (setq arg (1- arg))))) |
266 | ||
267 | (defun backward-paragraph (&optional arg) | |
268 | "Move backward to start of paragraph. | |
94d63a23 RS |
269 | With argument ARG, do it ARG times; |
270 | a negative argument ARG = -N means move forward N paragraphs. | |
a2535589 | 271 | |
23b34992 BP |
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." | |
a2535589 JA |
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) | |
0b108c01 | 288 | (push-mark nil t t) |
a2535589 JA |
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." | |
23b34992 | 295 | (interactive "p") |
8d6eaa00 | 296 | (kill-region (point) (progn (forward-paragraph arg) (point)))) |
a2535589 JA |
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." | |
23b34992 | 302 | (interactive "p") |
8d6eaa00 | 303 | (kill-region (point) (progn (backward-paragraph arg) (point)))) |
a2535589 JA |
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") | |
b4e6c391 RS |
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)) | |
a2535589 JA |
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) | |
51534471 | 336 | "Move forward to next `sentence-end'. With argument, repeat. |
23b34992 | 337 | With negative argument, move backward repeatedly to `sentence-beginning'. |
a2535589 | 338 | |
23b34992 BP |
339 | The variable `sentence-end' is a regular expression that matches ends of |
340 | sentences. Also, every paragraph boundary terminates sentences as well." | |
a2535589 JA |
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. | |
23b34992 | 358 | See `forward-sentence' for more information." |
a2535589 JA |
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." | |
b0e1e38f | 366 | (interactive "p") |
8d6eaa00 | 367 | (kill-region (point) (progn (forward-sentence arg) (point)))) |
a2535589 JA |
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." | |
b0e1e38f | 372 | (interactive "p") |
8d6eaa00 | 373 | (kill-region (point) (progn (backward-sentence arg) (point)))) |
a2535589 JA |
374 | |
375 | (defun mark-end-of-sentence (arg) | |
23b34992 | 376 | "Put mark at end of sentence. Arg works as in `forward-sentence'." |
a2535589 JA |
377 | (interactive "p") |
378 | (push-mark | |
379 | (save-excursion | |
380 | (forward-sentence arg) | |
a524dc5b RS |
381 | (point)) |
382 | nil t)) | |
a2535589 JA |
383 | |
384 | (defun transpose-sentences (arg) | |
385 | "Interchange this (next) and previous sentence." | |
386 | (interactive "*p") | |
387 | (transpose-subr 'forward-sentence arg)) | |
6594deb0 ER |
388 | |
389 | ;;; paragraphs.el ends here |