36
|
1 ;; Paragraph and sentence parsing.
|
264
|
2 ;; Copyright (C) 1985-1991 Free Software Foundation, Inc.
|
36
|
3
|
|
4 ;; This file is part of GNU Emacs.
|
|
5
|
|
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 ;; it under the terms of the GNU General Public License as published by
|
|
8 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
9 ;; any later version.
|
|
10
|
|
11 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 ;; GNU General Public License for more details.
|
|
15
|
|
16 ;; You should have received a copy of the GNU General Public License
|
|
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20
|
269
|
21 ;;;###autoload
|
|
22 (defconst paragraph-start "^[ \t\n\f]" "\
|
|
23 *Regexp for beginning of a line that starts OR separates paragraphs.")
|
264
|
24
|
269
|
25 ;;;###autoload
|
|
26 (defconst paragraph-separate "^[ \t\f]*$" "\
|
|
27 *Regexp for beginning of a line that separates paragraphs.
|
|
28 If you change this, you may have to change paragraph-start also.")
|
264
|
29
|
269
|
30 ;;;###autoload
|
|
31 (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") "\
|
|
32 *Regexp describing the end of a sentence.
|
|
33 All paragraph boundaries also end sentences, regardless.")
|
264
|
34
|
269
|
35 ;;;###autoload
|
|
36 (defconst page-delimiter "^\014" "\
|
|
37 *Regexp describing line-beginnings that separate pages.")
|
264
|
38
|
269
|
39 ;;;###autoload
|
|
40 (defvar paragraph-ignore-fill-prefix nil "\
|
|
41 Non-nil means the paragraph commands are not affected by `fill-prefix'.
|
|
42 This is desirable in modes where blank lines are the paragraph delimiters.")
|
264
|
43
|
36
|
44
|
|
45 (defun forward-paragraph (&optional arg)
|
|
46 "Move forward to end of paragraph.
|
|
47 With arg N, do it N times; negative arg -N means move forward N paragraphs.
|
|
48
|
|
49 A line which `paragraph-start' matches either separates paragraphs
|
|
50 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
|
|
51 A paragraph end is the beginning of a line which is not part of the paragraph
|
|
52 to which the end of the previous line belongs, or the end of the buffer."
|
|
53 (interactive "p")
|
|
54 (or arg (setq arg 1))
|
|
55 (let* ((fill-prefix-regexp
|
|
56 (and fill-prefix (not (equal fill-prefix ""))
|
|
57 (not paragraph-ignore-fill-prefix)
|
|
58 (regexp-quote fill-prefix)))
|
|
59 (paragraph-separate
|
|
60 (if fill-prefix-regexp
|
|
61 (concat paragraph-separate "\\|^"
|
|
62 fill-prefix-regexp "[ \t]*$")
|
|
63 paragraph-separate)))
|
|
64 (while (< arg 0)
|
|
65 (if (and (not (looking-at paragraph-separate))
|
|
66 (re-search-backward "^\n" (max (1- (point)) (point-min)) t))
|
|
67 nil
|
|
68 (forward-char -1) (beginning-of-line)
|
|
69 (while (and (not (bobp)) (looking-at paragraph-separate))
|
|
70 (forward-line -1))
|
|
71 (end-of-line)
|
|
72 ;; Search back for line that starts or separates paragraphs.
|
|
73 (if (if fill-prefix-regexp
|
|
74 ;; There is a fill prefix; it overrides paragraph-start.
|
|
75 (progn
|
|
76 (while (progn (beginning-of-line)
|
|
77 (and (not (bobp))
|
|
78 (not (looking-at paragraph-separate))
|
|
79 (looking-at fill-prefix-regexp)))
|
|
80 (forward-line -1))
|
|
81 (not (bobp)))
|
|
82 (re-search-backward paragraph-start nil t))
|
|
83 ;; Found one.
|
|
84 (progn
|
|
85 (while (and (not (eobp)) (looking-at paragraph-separate))
|
|
86 (forward-line 1))
|
|
87 (if (eq (char-after (- (point) 2)) ?\n)
|
|
88 (forward-line -1)))
|
|
89 ;; No starter or separator line => use buffer beg.
|
|
90 (goto-char (point-min))))
|
|
91 (setq arg (1+ arg)))
|
|
92 (while (> arg 0)
|
|
93 (beginning-of-line)
|
|
94 (while (prog1 (and (not (eobp))
|
|
95 (looking-at paragraph-separate))
|
|
96 (forward-line 1)))
|
|
97 (if fill-prefix-regexp
|
|
98 ;; There is a fill prefix; it overrides paragraph-start.
|
|
99 (while (and (not (eobp))
|
|
100 (not (looking-at paragraph-separate))
|
|
101 (looking-at fill-prefix-regexp))
|
|
102 (forward-line 1))
|
|
103 (if (re-search-forward paragraph-start nil t)
|
|
104 (goto-char (match-beginning 0))
|
|
105 (goto-char (point-max))))
|
|
106 (setq arg (1- arg)))))
|
|
107
|
|
108 (defun backward-paragraph (&optional arg)
|
|
109 "Move backward to start of paragraph.
|
|
110 With arg N, do it N times; negative arg -N means move forward N paragraphs.
|
|
111
|
237
|
112 A paragraph start is the beginning of a line which is a
|
|
113 `first-line-of-paragraph' or which is ordinary text and follows a
|
|
114 paragraph-separating line; except: if the first real line of a
|
|
115 paragraph is preceded by a blank line, the paragraph starts at that
|
|
116 blank line.
|
|
117
|
|
118 See `forward-paragraph' for more information."
|
36
|
119 (interactive "p")
|
|
120 (or arg (setq arg 1))
|
|
121 (forward-paragraph (- arg)))
|
|
122
|
|
123 (defun mark-paragraph ()
|
|
124 "Put point at beginning of this paragraph, mark at end.
|
|
125 The paragraph marked is the one that contains point or follows point."
|
|
126 (interactive)
|
|
127 (forward-paragraph 1)
|
|
128 (push-mark nil t)
|
|
129 (backward-paragraph 1))
|
|
130
|
|
131 (defun kill-paragraph (arg)
|
|
132 "Kill forward to end of paragraph.
|
|
133 With arg N, kill forward to Nth end of paragraph;
|
|
134 negative arg -N means kill backward to Nth start of paragraph."
|
237
|
135 (interactive "p")
|
36
|
136 (kill-region (point) (progn (forward-paragraph arg) (point))))
|
|
137
|
|
138 (defun backward-kill-paragraph (arg)
|
|
139 "Kill back to start of paragraph.
|
|
140 With arg N, kill back to Nth start of paragraph;
|
|
141 negative arg -N means kill forward to Nth end of paragraph."
|
237
|
142 (interactive "p")
|
36
|
143 (kill-region (point) (progn (backward-paragraph arg) (point))))
|
|
144
|
|
145 (defun transpose-paragraphs (arg)
|
|
146 "Interchange this (or next) paragraph with previous one."
|
|
147 (interactive "*p")
|
|
148 (transpose-subr 'forward-paragraph arg))
|
|
149
|
|
150 (defun start-of-paragraph-text ()
|
|
151 (let ((opoint (point)) npoint)
|
|
152 (forward-paragraph -1)
|
|
153 (setq npoint (point))
|
|
154 (skip-chars-forward " \t\n")
|
68
|
155 ;; If the range of blank lines found spans the original start point,
|
|
156 ;; try again from the beginning of it.
|
|
157 ;; Must be careful to avoid infinite loop
|
|
158 ;; when following a single return at start of buffer.
|
|
159 (if (and (>= (point) opoint) (< npoint opoint))
|
36
|
160 (progn
|
|
161 (goto-char npoint)
|
|
162 (if (> npoint (point-min))
|
|
163 (start-of-paragraph-text))))))
|
|
164
|
|
165 (defun end-of-paragraph-text ()
|
|
166 (let ((opoint (point)))
|
|
167 (forward-paragraph 1)
|
|
168 (if (eq (preceding-char) ?\n) (forward-char -1))
|
|
169 (if (<= (point) opoint)
|
|
170 (progn
|
|
171 (forward-char 1)
|
|
172 (if (< (point) (point-max))
|
|
173 (end-of-paragraph-text))))))
|
|
174
|
|
175 (defun forward-sentence (&optional arg)
|
237
|
176 "Move forward to next`sentence-end'. With argument, repeat.
|
|
177 With negative argument, move backward repeatedly to `sentence-beginning'.
|
36
|
178
|
237
|
179 The variable `sentence-end' is a regular expression that matches ends of
|
|
180 sentences. Also, every paragraph boundary terminates sentences as well."
|
36
|
181 (interactive "p")
|
|
182 (or arg (setq arg 1))
|
|
183 (while (< arg 0)
|
|
184 (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
|
|
185 (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
|
|
186 (goto-char (1- (match-end 0)))
|
|
187 (goto-char par-beg)))
|
|
188 (setq arg (1+ arg)))
|
|
189 (while (> arg 0)
|
|
190 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
|
|
191 (if (re-search-forward sentence-end par-end t)
|
|
192 (skip-chars-backward " \t\n")
|
|
193 (goto-char par-end)))
|
|
194 (setq arg (1- arg))))
|
|
195
|
|
196 (defun backward-sentence (&optional arg)
|
|
197 "Move backward to start of sentence. With arg, do it arg times.
|
237
|
198 See `forward-sentence' for more information."
|
36
|
199 (interactive "p")
|
|
200 (or arg (setq arg 1))
|
|
201 (forward-sentence (- arg)))
|
|
202
|
|
203 (defun kill-sentence (&optional arg)
|
|
204 "Kill from point to end of sentence.
|
|
205 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
|
|
206 (interactive "*p")
|
|
207 (let ((beg (point)))
|
|
208 (forward-sentence arg)
|
|
209 (kill-region beg (point))))
|
|
210
|
|
211 (defun backward-kill-sentence (&optional arg)
|
|
212 "Kill back from point to start of sentence.
|
|
213 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
|
|
214 (interactive "*p")
|
|
215 (let ((beg (point)))
|
|
216 (backward-sentence arg)
|
|
217 (kill-region beg (point))))
|
|
218
|
|
219 (defun mark-end-of-sentence (arg)
|
237
|
220 "Put mark at end of sentence. Arg works as in `forward-sentence'."
|
36
|
221 (interactive "p")
|
|
222 (push-mark
|
|
223 (save-excursion
|
|
224 (forward-sentence arg)
|
|
225 (point))))
|
|
226
|
|
227 (defun transpose-sentences (arg)
|
|
228 "Interchange this (next) and previous sentence."
|
|
229 (interactive "*p")
|
|
230 (transpose-subr 'forward-sentence arg))
|