Mercurial > emacs
annotate lisp/textmodes/paragraphs.el @ 5202:0580776d4578
Properly install previous change.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 27 Nov 1993 19:37:46 +0000 |
parents | 04e78c728252 |
children | c4c662362d39 |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
1 ;;; paragraphs.el --- paragraph and sentence parsing. |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
2 |
845 | 3 ;; Copyright (C) 1985, 86, 87, 1991 Free Software Foundation, Inc. |
4 | |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
5 ;; Maintainer: FSF |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
804
diff
changeset
|
6 ;; Keywords: wp |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
7 |
36 | 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 | |
804 | 12 ;; the Free Software Foundation; either version 2, or (at your option) |
36 | 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 | |
2308
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1462
diff
changeset
|
24 ;;; Commentary: |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1462
diff
changeset
|
25 |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1462
diff
changeset
|
26 ;; This package provides the paragraph-oriented commands documented in the |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1462
diff
changeset
|
27 ;; Emacs manual. |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1462
diff
changeset
|
28 |
789
71d052f72ac1
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
29 ;;; Code: |
36 | 30 |
269 | 31 (defconst paragraph-start "^[ \t\n\f]" "\ |
4585
04e78c728252
(paragraph-start): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
2828
diff
changeset
|
32 *Regexp for beginning of a line that starts OR separates paragraphs. |
04e78c728252
(paragraph-start): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
2828
diff
changeset
|
33 This regexp should match lines that separate paragraphs |
04e78c728252
(paragraph-start): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
2828
diff
changeset
|
34 and should also match lines that start a paragraph |
04e78c728252
(paragraph-start): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
2828
diff
changeset
|
35 \(and are part of that paragraph). |
04e78c728252
(paragraph-start): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
2828
diff
changeset
|
36 The variable `paragraph-separate' specifies how to distinguish |
04e78c728252
(paragraph-start): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
2828
diff
changeset
|
37 lines that start paragraphs from lines that separate them.") |
264 | 38 |
269 | 39 (defconst paragraph-separate "^[ \t\f]*$" "\ |
40 *Regexp for beginning of a line that separates paragraphs. | |
41 If you change this, you may have to change paragraph-start also.") | |
264 | 42 |
269 | 43 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\ |
44 *Regexp describing the end of a sentence. | |
2503
fb3509fdf8b7
Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
Jim Blandy <jimb@redhat.com>
parents:
2308
diff
changeset
|
45 All paragraph boundaries also end sentences, regardless. |
fb3509fdf8b7
Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
Jim Blandy <jimb@redhat.com>
parents:
2308
diff
changeset
|
46 |
fb3509fdf8b7
Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
Jim Blandy <jimb@redhat.com>
parents:
2308
diff
changeset
|
47 In order to be recognized as the end of a sentence, the ending period, |
fb3509fdf8b7
Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
Jim Blandy <jimb@redhat.com>
parents:
2308
diff
changeset
|
48 question mark, or exclamation point must be followed by two spaces, |
fb3509fdf8b7
Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
Jim Blandy <jimb@redhat.com>
parents:
2308
diff
changeset
|
49 unless it's inside some sort of quotes or parenthesis.") |
264 | 50 |
269 | 51 (defconst page-delimiter "^\014" "\ |
52 *Regexp describing line-beginnings that separate pages.") | |
264 | 53 |
269 | 54 (defvar paragraph-ignore-fill-prefix nil "\ |
55 Non-nil means the paragraph commands are not affected by `fill-prefix'. | |
56 This is desirable in modes where blank lines are the paragraph delimiters.") | |
264 | 57 |
36 | 58 |
59 (defun forward-paragraph (&optional arg) | |
60 "Move forward to end of paragraph. | |
61 With arg N, do it N times; negative arg -N means move forward N paragraphs. | |
62 | |
63 A line which `paragraph-start' matches either separates paragraphs | |
64 \(if `paragraph-separate' matches it also) or is the first line of a paragraph. | |
65 A paragraph end is the beginning of a line which is not part of the paragraph | |
66 to which the end of the previous line belongs, or the end of the buffer." | |
67 (interactive "p") | |
68 (or arg (setq arg 1)) | |
69 (let* ((fill-prefix-regexp | |
70 (and fill-prefix (not (equal fill-prefix "")) | |
71 (not paragraph-ignore-fill-prefix) | |
72 (regexp-quote fill-prefix))) | |
73 (paragraph-separate | |
74 (if fill-prefix-regexp | |
75 (concat paragraph-separate "\\|^" | |
76 fill-prefix-regexp "[ \t]*$") | |
77 paragraph-separate))) | |
78 (while (< arg 0) | |
79 (if (and (not (looking-at paragraph-separate)) | |
80 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)) | |
81 nil | |
82 (forward-char -1) (beginning-of-line) | |
83 (while (and (not (bobp)) (looking-at paragraph-separate)) | |
84 (forward-line -1)) | |
85 (end-of-line) | |
86 ;; Search back for line that starts or separates paragraphs. | |
87 (if (if fill-prefix-regexp | |
88 ;; There is a fill prefix; it overrides paragraph-start. | |
89 (progn | |
90 (while (progn (beginning-of-line) | |
91 (and (not (bobp)) | |
92 (not (looking-at paragraph-separate)) | |
93 (looking-at fill-prefix-regexp))) | |
94 (forward-line -1)) | |
95 (not (bobp))) | |
96 (re-search-backward paragraph-start nil t)) | |
97 ;; Found one. | |
98 (progn | |
99 (while (and (not (eobp)) (looking-at paragraph-separate)) | |
100 (forward-line 1)) | |
101 (if (eq (char-after (- (point) 2)) ?\n) | |
102 (forward-line -1))) | |
103 ;; No starter or separator line => use buffer beg. | |
104 (goto-char (point-min)))) | |
105 (setq arg (1+ arg))) | |
106 (while (> arg 0) | |
107 (beginning-of-line) | |
108 (while (prog1 (and (not (eobp)) | |
109 (looking-at paragraph-separate)) | |
110 (forward-line 1))) | |
111 (if fill-prefix-regexp | |
112 ;; There is a fill prefix; it overrides paragraph-start. | |
113 (while (and (not (eobp)) | |
114 (not (looking-at paragraph-separate)) | |
115 (looking-at fill-prefix-regexp)) | |
116 (forward-line 1)) | |
117 (if (re-search-forward paragraph-start nil t) | |
118 (goto-char (match-beginning 0)) | |
119 (goto-char (point-max)))) | |
120 (setq arg (1- arg))))) | |
121 | |
122 (defun backward-paragraph (&optional arg) | |
123 "Move backward to start of paragraph. | |
124 With arg N, do it N times; negative arg -N means move forward N paragraphs. | |
125 | |
237 | 126 A paragraph start is the beginning of a line which is a |
127 `first-line-of-paragraph' or which is ordinary text and follows a | |
128 paragraph-separating line; except: if the first real line of a | |
129 paragraph is preceded by a blank line, the paragraph starts at that | |
130 blank line. | |
131 | |
132 See `forward-paragraph' for more information." | |
36 | 133 (interactive "p") |
134 (or arg (setq arg 1)) | |
135 (forward-paragraph (- arg))) | |
136 | |
137 (defun mark-paragraph () | |
138 "Put point at beginning of this paragraph, mark at end. | |
139 The paragraph marked is the one that contains point or follows point." | |
140 (interactive) | |
141 (forward-paragraph 1) | |
2827
0daa9d777306
(mark-paragraph): Activate the mark.
Richard M. Stallman <rms@gnu.org>
parents:
2503
diff
changeset
|
142 (push-mark nil t t) |
36 | 143 (backward-paragraph 1)) |
144 | |
145 (defun kill-paragraph (arg) | |
146 "Kill forward to end of paragraph. | |
147 With arg N, kill forward to Nth end of paragraph; | |
148 negative arg -N means kill backward to Nth start of paragraph." | |
237 | 149 (interactive "p") |
1435
4fef92b213f6
(kill-sentence, backward-kill-sentence):
Richard M. Stallman <rms@gnu.org>
parents:
845
diff
changeset
|
150 (kill-region (point) (save-excursion (forward-paragraph arg) (point)))) |
36 | 151 |
152 (defun backward-kill-paragraph (arg) | |
153 "Kill back to start of paragraph. | |
154 With arg N, kill back to Nth start of paragraph; | |
155 negative arg -N means kill forward to Nth end of paragraph." | |
237 | 156 (interactive "p") |
1435
4fef92b213f6
(kill-sentence, backward-kill-sentence):
Richard M. Stallman <rms@gnu.org>
parents:
845
diff
changeset
|
157 (kill-region (point) (save-excursion (backward-paragraph arg) (point)))) |
36 | 158 |
159 (defun transpose-paragraphs (arg) | |
160 "Interchange this (or next) paragraph with previous one." | |
161 (interactive "*p") | |
162 (transpose-subr 'forward-paragraph arg)) | |
163 | |
164 (defun start-of-paragraph-text () | |
165 (let ((opoint (point)) npoint) | |
166 (forward-paragraph -1) | |
167 (setq npoint (point)) | |
168 (skip-chars-forward " \t\n") | |
68 | 169 ;; If the range of blank lines found spans the original start point, |
170 ;; try again from the beginning of it. | |
171 ;; Must be careful to avoid infinite loop | |
172 ;; when following a single return at start of buffer. | |
173 (if (and (>= (point) opoint) (< npoint opoint)) | |
36 | 174 (progn |
175 (goto-char npoint) | |
176 (if (> npoint (point-min)) | |
177 (start-of-paragraph-text)))))) | |
178 | |
179 (defun end-of-paragraph-text () | |
180 (let ((opoint (point))) | |
181 (forward-paragraph 1) | |
182 (if (eq (preceding-char) ?\n) (forward-char -1)) | |
183 (if (<= (point) opoint) | |
184 (progn | |
185 (forward-char 1) | |
186 (if (< (point) (point-max)) | |
187 (end-of-paragraph-text)))))) | |
188 | |
189 (defun forward-sentence (&optional arg) | |
2503
fb3509fdf8b7
Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
Jim Blandy <jimb@redhat.com>
parents:
2308
diff
changeset
|
190 "Move forward to next `sentence-end'. With argument, repeat. |
237 | 191 With negative argument, move backward repeatedly to `sentence-beginning'. |
36 | 192 |
237 | 193 The variable `sentence-end' is a regular expression that matches ends of |
194 sentences. Also, every paragraph boundary terminates sentences as well." | |
36 | 195 (interactive "p") |
196 (or arg (setq arg 1)) | |
197 (while (< arg 0) | |
198 (let ((par-beg (save-excursion (start-of-paragraph-text) (point)))) | |
199 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t) | |
200 (goto-char (1- (match-end 0))) | |
201 (goto-char par-beg))) | |
202 (setq arg (1+ arg))) | |
203 (while (> arg 0) | |
204 (let ((par-end (save-excursion (end-of-paragraph-text) (point)))) | |
205 (if (re-search-forward sentence-end par-end t) | |
206 (skip-chars-backward " \t\n") | |
207 (goto-char par-end))) | |
208 (setq arg (1- arg)))) | |
209 | |
210 (defun backward-sentence (&optional arg) | |
211 "Move backward to start of sentence. With arg, do it arg times. | |
237 | 212 See `forward-sentence' for more information." |
36 | 213 (interactive "p") |
214 (or arg (setq arg 1)) | |
215 (forward-sentence (- arg))) | |
216 | |
217 (defun kill-sentence (&optional arg) | |
218 "Kill from point to end of sentence. | |
219 With arg, repeat; negative arg -N means kill back to Nth start of sentence." | |
220 (interactive "*p") | |
1435
4fef92b213f6
(kill-sentence, backward-kill-sentence):
Richard M. Stallman <rms@gnu.org>
parents:
845
diff
changeset
|
221 (kill-region (point) (save-excursion (forward-sentence arg) (point)))) |
36 | 222 |
223 (defun backward-kill-sentence (&optional arg) | |
224 "Kill back from point to start of sentence. | |
225 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N." | |
226 (interactive "*p") | |
1462
876e54ad6936
(backward-kill-sentence): Fix typo in last change.
Richard M. Stallman <rms@gnu.org>
parents:
1435
diff
changeset
|
227 (kill-region (point) (save-excursion (backward-sentence arg) (point)))) |
36 | 228 |
229 (defun mark-end-of-sentence (arg) | |
237 | 230 "Put mark at end of sentence. Arg works as in `forward-sentence'." |
36 | 231 (interactive "p") |
232 (push-mark | |
233 (save-excursion | |
234 (forward-sentence arg) | |
2828
abc6df78588f
(mark-end-of-sentence): Activate the mark.
Richard M. Stallman <rms@gnu.org>
parents:
2827
diff
changeset
|
235 (point)) |
abc6df78588f
(mark-end-of-sentence): Activate the mark.
Richard M. Stallman <rms@gnu.org>
parents:
2827
diff
changeset
|
236 nil t)) |
36 | 237 |
238 (defun transpose-sentences (arg) | |
239 "Interchange this (next) and previous sentence." | |
240 (interactive "*p") | |
241 (transpose-subr 'forward-sentence arg)) | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
242 |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
243 ;;; paragraphs.el ends here |