38412
|
1 ;;; paragraphs.el --- paragraph and sentence parsing
|
659
|
2
|
37302
|
3 ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96, 1997, 1999, 2000, 2001
|
19475
|
4 ;; Free Software Foundation, Inc.
|
845
|
5
|
789
|
6 ;; Maintainer: FSF
|
814
|
7 ;; Keywords: wp
|
789
|
8
|
36
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
|
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
|
804
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
36
|
14 ;; any later version.
|
|
15
|
|
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.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
14169
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
36
|
25
|
2308
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; This package provides the paragraph-oriented commands documented in the
|
|
29 ;; Emacs manual.
|
|
30
|
789
|
31 ;;; Code:
|
36
|
32
|
19475
|
33 (defgroup paragraphs nil
|
|
34 "Paragraph and sentence parsing."
|
|
35 :group 'editing)
|
|
36
|
40483
|
37 (define-minor-mode use-hard-newlines
|
16019
|
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
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
41 only considered as a candidate to match `paragraph-start' or
|
16019
|
42 `paragraph-separate' if it follows a hard newline.
|
|
43
|
|
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
|
49599
|
46 newlines, ask the user whether to mark as hard any newlines preceeding a
|
16019
|
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
|
49599
|
49 marking, `guess' to try to do the right thing with no questions, nil
|
16019
|
50 or anything else to ask the user.
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
51
|
16019
|
52 Newlines not marked hard are called \"soft\", and are always internal
|
|
53 to paragraphs. The fill functions insert and delete only soft newlines."
|
48401
|
54 :group 'paragraphs
|
40483
|
55 :extra-args (insert)
|
|
56 (when use-hard-newlines
|
16019
|
57 ;; Turn mode on
|
|
58 ;; Intuit hard newlines --
|
|
59 ;; mark as hard any newlines preceding a paragraph-start line.
|
|
60 (if (or (eq insert t) (eq insert 'always)
|
|
61 (and (not (eq 'never insert))
|
|
62 (not (text-property-any (point-min) (point-max) 'hard t))
|
|
63 (save-excursion
|
|
64 (goto-char (point-min))
|
|
65 (search-forward "\n" nil t))
|
|
66 (or (eq insert 'guess)
|
|
67 (y-or-n-p "Make newlines between paragraphs hard? "))))
|
|
68 (save-excursion
|
|
69 (goto-char (point-min))
|
|
70 (while (search-forward "\n" nil t)
|
|
71 (let ((pos (point)))
|
|
72 (move-to-left-margin)
|
40483
|
73 (when (looking-at paragraph-start)
|
|
74 (set-hard-newline-properties (1- pos) pos))
|
|
75 ;; If paragraph-separate, newline after it is hard too.
|
|
76 (when (looking-at paragraph-separate)
|
|
77 (set-hard-newline-properties (1- pos) pos)
|
|
78 (end-of-line)
|
|
79 (unless (eobp)
|
|
80 (set-hard-newline-properties (point) (1+ (point)))))))))))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
81
|
40483
|
82 (defcustom paragraph-start "\f\\|[ \t]*$" "\
|
4585
|
83 *Regexp for beginning of a line that starts OR separates paragraphs.
|
|
84 This regexp should match lines that separate paragraphs
|
|
85 and should also match lines that start a paragraph
|
|
86 \(and are part of that paragraph).
|
10424
|
87
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
88 This is matched against the text at the left margin, which is not necessarily
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
89 the beginning of the line, so it should never use \"^\" as an anchor. This
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
90 ensures that the paragraph functions will work equally well within a region
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
91 of text indented by a margin setting.
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
92
|
4585
|
93 The variable `paragraph-separate' specifies how to distinguish
|
10424
|
94 lines that start paragraphs from lines that separate them.
|
|
95
|
34655
|
96 If the variable `use-hard-newlines' is non-nil, then only lines following a
|
19475
|
97 hard newline are considered to match."
|
|
98 :group 'paragraphs
|
|
99 :type 'regexp)
|
264
|
100
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
101 ;; paragraph-start requires a hard newline, but paragraph-separate does not:
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
102 ;; It is assumed that paragraph-separate is distinctive enough to be believed
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
103 ;; whenever it occurs, while it is reasonable to set paragraph-start to
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
104 ;; something very minimal, even including "." (which makes every hard newline
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
105 ;; start a new paragraph).
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
106
|
19475
|
107 (defcustom paragraph-separate "[ \t\f]*$"
|
|
108 "*Regexp for beginning of a line that separates paragraphs.
|
24309
|
109 If you change this, you may have to change `paragraph-start' also.
|
10424
|
110
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
111 This is matched against the text at the left margin, which is not necessarily
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
112 the beginning of the line, so it should not use \"^\" as an anchor. This
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
113 ensures that the paragraph functions will work equally within a region of
|
19475
|
114 text indented by a margin setting."
|
|
115 :group 'paragraphs
|
|
116 :type 'regexp)
|
264
|
117
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
118 (defcustom sentence-end-double-space t
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
119 "*Non-nil means a single space does not end a sentence.
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
120 This is relevant for filling. See also `sentence-end-without-period'
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
121 and `colon-double-space'.
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
122
|
54977
|
123 This value is used by the function `sentence-end' to construct the
|
|
124 regexp describing the end of a sentence, in case when the value of
|
|
125 the variable `sentence-end' is nil. See Info node `Sentences'."
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
126 :type 'boolean
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
127 :group 'fill)
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
128
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
129 (defcustom sentence-end-without-period nil
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
130 "*Non-nil means a sentence will end without a period.
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
131 For example, a sentence in Thai text ends with double space but
|
54977
|
132 without a period.
|
|
133
|
|
134 This value is used by the function `sentence-end' to construct the
|
|
135 regexp describing the end of a sentence, in case when the value of
|
|
136 the variable `sentence-end' is nil. See Info node `Sentences'."
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
137 :type 'boolean
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
138 :group 'fill)
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
139
|
53721
|
140 (defcustom sentence-end-without-space
|
|
141 "$B!#!%!)!*$A!##.#?#!$(0!$!%!)!*$(G!$!%!)!*(B"
|
|
142 "*String containing characters that end sentence without following spaces.
|
54977
|
143
|
|
144 This value is used by the function `sentence-end' to construct the
|
|
145 regexp describing the end of a sentence, in case when the value of
|
|
146 the variable `sentence-end' is nil. See Info node `Sentences'."
|
53721
|
147 :group 'paragraphs
|
|
148 :type 'string)
|
|
149
|
54977
|
150 (defcustom sentence-end nil
|
19475
|
151 "*Regexp describing the end of a sentence.
|
37415
|
152 The value includes the whitespace following the sentence.
|
2503
|
153 All paragraph boundaries also end sentences, regardless.
|
|
154
|
54977
|
155 The value nil means to use the default value defined by the
|
|
156 function `sentence-end'. You should always use this function
|
|
157 to obtain the value of this variable."
|
|
158 :group 'paragraphs
|
|
159 :type '(choice regexp (const :tag "Use default value" nil)))
|
|
160
|
|
161 (defun sentence-end ()
|
|
162 "Return the regexp describing the end of a sentence.
|
24472
|
163
|
54977
|
164 This function returns either the value of the variable `sentence-end'
|
|
165 if it is non-nil, or the default value constructed from the
|
|
166 variables `sentence-end-double-space', `sentence-end-without-period'
|
|
167 and `sentence-end-without-space'. The default value specifies
|
|
168 that in order to be recognized as the end of a sentence, the
|
|
169 ending period, question mark, or exclamation point must be
|
|
170 followed by two spaces, unless it's inside some sort of quotes or
|
|
171 parenthesis. See Info node `Sentences'."
|
|
172 (or sentence-end
|
|
173 (concat (if sentence-end-without-period "\\w \\|")
|
56084
|
174 "\\([.?!][]\"'\xd0c9\x5397d)}]*"
|
54977
|
175 (if sentence-end-double-space
|
|
176 "\\($\\| $\\|\t\\| \\)" "\\($\\|[\t ]\\)")
|
|
177 "\\|[" sentence-end-without-space "]+\\)"
|
|
178 "[ \t\n]*")))
|
264
|
179
|
19475
|
180 (defcustom page-delimiter "^\014"
|
|
181 "*Regexp describing line-beginnings that separate pages."
|
|
182 :group 'paragraphs
|
|
183 :type 'regexp)
|
264
|
184
|
19475
|
185 (defcustom paragraph-ignore-fill-prefix nil
|
|
186 "*Non-nil means the paragraph commands are not affected by `fill-prefix'.
|
|
187 This is desirable in modes where blank lines are the paragraph delimiters."
|
|
188 :group 'paragraphs
|
|
189 :type 'boolean)
|
264
|
190
|
36
|
191 (defun forward-paragraph (&optional arg)
|
|
192 "Move forward to end of paragraph.
|
16757
|
193 With argument ARG, do it ARG times;
|
|
194 a negative argument ARG = -N means move backward N paragraphs.
|
36
|
195
|
|
196 A line which `paragraph-start' matches either separates paragraphs
|
|
197 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
|
|
198 A paragraph end is the beginning of a line which is not part of the paragraph
|
47829
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
199 to which the end of the previous line belongs, or the end of the buffer.
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
200 Returns the count of paragraphs left to move."
|
36
|
201 (interactive "p")
|
|
202 (or arg (setq arg 1))
|
26055
|
203 (let* ((opoint (point))
|
|
204 (fill-prefix-regexp
|
36
|
205 (and fill-prefix (not (equal fill-prefix ""))
|
|
206 (not paragraph-ignore-fill-prefix)
|
|
207 (regexp-quote fill-prefix)))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
208 ;; Remove ^ from paragraph-start and paragraph-sep if they are there.
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
209 ;; These regexps shouldn't be anchored, because we look for them
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
210 ;; starting at the left-margin. This allows paragraph commands to
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
211 ;; work normally with indented text.
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
212 ;; This hack will not find problem cases like "whatever\\|^something".
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
213 (parstart (if (and (not (equal "" paragraph-start))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
214 (equal ?^ (aref paragraph-start 0)))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
215 (substring paragraph-start 1)
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
216 paragraph-start))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
217 (parsep (if (and (not (equal "" paragraph-separate))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
218 (equal ?^ (aref paragraph-separate 0)))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
219 (substring paragraph-separate 1)
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
220 paragraph-separate))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
221 (parsep
|
36
|
222 (if fill-prefix-regexp
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
223 (concat parsep "\\|"
|
36
|
224 fill-prefix-regexp "[ \t]*$")
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
225 parsep))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
226 ;; This is used for searching.
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
227 (sp-parstart (concat "^[ \t]*\\(?:" parstart "\\|" parsep "\\)"))
|
22953
fbeacfb09096
(forward-paragraph): Fix the logic for handling beginning of buffer
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
228 start found-start)
|
5608
|
229 (while (and (< arg 0) (not (bobp)))
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
230 (if (and (not (looking-at parsep))
|
10424
|
231 (re-search-backward "^\n" (max (1- (point)) (point-min)) t)
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
232 (looking-at parsep))
|
47829
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
233 (setq arg (1+ arg))
|
12717
|
234 (setq start (point))
|
5608
|
235 ;; Move back over paragraph-separating lines.
|
36
|
236 (forward-char -1) (beginning-of-line)
|
10424
|
237 (while (and (not (bobp))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
238 (progn (move-to-left-margin)
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
239 (looking-at parsep)))
|
34655
|
240 (forward-line -1))
|
5608
|
241 (if (bobp)
|
|
242 nil
|
47829
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
243 (setq arg (1+ arg))
|
5608
|
244 ;; Go to end of the previous (non-separating) line.
|
|
245 (end-of-line)
|
|
246 ;; Search back for line that starts or separates paragraphs.
|
|
247 (if (if fill-prefix-regexp
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
248 ;; There is a fill prefix; it overrides parstart.
|
12717
|
249 (let (multiple-lines)
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
250 (while (and (progn (beginning-of-line) (not (bobp)))
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
251 (progn (move-to-left-margin)
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
252 (not (looking-at parsep)))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
253 (looking-at fill-prefix-regexp))
|
34655
|
254 (unless (= (point) start)
|
|
255 (setq multiple-lines t))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
256 (forward-line -1))
|
12717
|
257 (move-to-left-margin)
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
258 ;; This deleted code caused a long hanging-indent line
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
259 ;; not to be filled together with the following lines.
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
260 ;; ;; Don't move back over a line before the paragraph
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
261 ;; ;; which doesn't start with fill-prefix
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
262 ;; ;; unless that is the only line we've moved over.
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
263 ;; (and (not (looking-at fill-prefix-regexp))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
264 ;; multiple-lines
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
265 ;; (forward-line 1))
|
12717
|
266 (not (bobp)))
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
267 (while (and (re-search-backward sp-parstart nil 1)
|
22953
fbeacfb09096
(forward-paragraph): Fix the logic for handling beginning of buffer
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
268 (setq found-start t)
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
269 ;; Found a candidate, but need to check if it is a
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
270 ;; REAL parstart.
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
271 (progn (setq start (point))
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
272 (move-to-left-margin)
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
273 (not (looking-at parsep)))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
274 (not (and (looking-at parstart)
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
275 (or (not use-hard-newlines)
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
276 (get-text-property (1- start) 'hard)
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
277 (bobp)))))
|
22953
fbeacfb09096
(forward-paragraph): Fix the logic for handling beginning of buffer
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
278 (setq found-start nil)
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
279 (goto-char start))
|
22953
fbeacfb09096
(forward-paragraph): Fix the logic for handling beginning of buffer
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
280 found-start)
|
47829
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
281 ;; Found one.
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
282 (progn
|
5608
|
283 ;; Move forward over paragraph separators.
|
|
284 ;; We know this cannot reach the place we started
|
|
285 ;; because we know we moved back over a non-separator.
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
286 (while (and (not (eobp))
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
287 (progn (move-to-left-margin)
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
288 (looking-at parsep)))
|
5608
|
289 (forward-line 1))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
290 ;; If line before paragraph is just margin, back up to there.
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
291 (end-of-line 0)
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
292 (if (> (current-column) (current-left-margin))
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
293 (forward-char 1)
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
294 (skip-chars-backward " \t")
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
295 (if (not (bolp))
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
296 (forward-line 1))))
|
5608
|
297 ;; No starter or separator line => use buffer beg.
|
47829
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
298 (goto-char (point-min))))))
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
299
|
5608
|
300 (while (and (> arg 0) (not (eobp)))
|
47829
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
301 ;; Move forward over separator lines...
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
302 (while (and (not (eobp))
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
303 (progn (move-to-left-margin) (not (eobp)))
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
304 (looking-at parsep))
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
305 (forward-line 1))
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
306 (unless (eobp) (setq arg (1- arg)))
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
307 ;; ... and one more line.
|
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
308 (forward-line 1)
|
36
|
309 (if fill-prefix-regexp
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
310 ;; There is a fill prefix; it overrides parstart.
|
36
|
311 (while (and (not (eobp))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
312 (progn (move-to-left-margin) (not (eobp)))
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
313 (not (looking-at parsep))
|
36
|
314 (looking-at fill-prefix-regexp))
|
|
315 (forward-line 1))
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
316 (while (and (re-search-forward sp-parstart nil 1)
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
317 (progn (setq start (match-beginning 0))
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
318 (goto-char start)
|
12804
|
319 (not (eobp)))
|
|
320 (progn (move-to-left-margin)
|
40485
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
321 (not (looking-at parsep)))
|
ca833879aa7b
(sentence-end-double-space, sentence-end-without-period): Move from fill.el.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
322 (or (not (looking-at parstart))
|
10864
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
323 (and use-hard-newlines
|
33769cbeb58e
(paragraph-start, paragraph-separate): Default values no longer start
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
324 (not (get-text-property (1- start) 'hard)))))
|
10424
|
325 (forward-char 1))
|
|
326 (if (< (point) (point-max))
|
47829
353dae5e0134
(forward-paragraph): Keep track of remaining paragraphs to skip more carefully.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
327 (goto-char start))))
|
47750
|
328 (constrain-to-field nil opoint t)
|
|
329 ;; Return the number of steps that could not be done.
|
|
330 arg))
|
36
|
331
|
|
332 (defun backward-paragraph (&optional arg)
|
|
333 "Move backward to start of paragraph.
|
16757
|
334 With argument ARG, do it ARG times;
|
|
335 a negative argument ARG = -N means move forward N paragraphs.
|
36
|
336
|
237
|
337 A paragraph start is the beginning of a line which is a
|
|
338 `first-line-of-paragraph' or which is ordinary text and follows a
|
|
339 paragraph-separating line; except: if the first real line of a
|
|
340 paragraph is preceded by a blank line, the paragraph starts at that
|
|
341 blank line.
|
|
342
|
|
343 See `forward-paragraph' for more information."
|
36
|
344 (interactive "p")
|
|
345 (or arg (setq arg 1))
|
|
346 (forward-paragraph (- arg)))
|
|
347
|
41709
|
348 (defun mark-paragraph (&optional arg)
|
36
|
349 "Put point at beginning of this paragraph, mark at end.
|
41709
|
350 The paragraph marked is the one that contains point or follows point.
|
|
351
|
|
352 With argument ARG, puts mark at end of a following paragraph, so that
|
|
353 the number of paragraphs marked equals ARG.
|
|
354
|
|
355 If ARG is negative, point is put at end of this paragraph, mark is put
|
43301
|
356 at beginning of this or a previous paragraph.
|
|
357
|
|
358 If this command is repeated, it marks the next ARG paragraphs after (or
|
|
359 before, if arg is negative) the ones already marked."
|
41709
|
360 (interactive "p")
|
43384
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
361 (unless arg (setq arg 1))
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
362 (when (zerop arg)
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
363 (error "Cannot mark zero paragraphs"))
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
364 (cond ((and (eq last-command this-command) (mark t))
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
365 (set-mark
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
366 (save-excursion
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
367 (goto-char (mark))
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
368 (forward-paragraph arg)
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
369 (point))))
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
370 (t
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
371 (forward-paragraph arg)
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
372 (push-mark nil t t)
|
cc3ba2d0d471
* emacs-lisp/lisp.el (mark-defun): Don't leave multiple marks
Kai Großjohann <kgrossjo@eu.uu.net>
diff
changeset
|
373 (backward-paragraph arg))))
|
36
|
374
|
|
375 (defun kill-paragraph (arg)
|
|
376 "Kill forward to end of paragraph.
|
|
377 With arg N, kill forward to Nth end of paragraph;
|
|
378 negative arg -N means kill backward to Nth start of paragraph."
|
27837
|
379 (interactive "p")
|
7064
|
380 (kill-region (point) (progn (forward-paragraph arg) (point))))
|
36
|
381
|
|
382 (defun backward-kill-paragraph (arg)
|
|
383 "Kill back to start of paragraph.
|
|
384 With arg N, kill back to Nth start of paragraph;
|
|
385 negative arg -N means kill forward to Nth end of paragraph."
|
27837
|
386 (interactive "p")
|
26055
|
387 (kill-region (point) (progn (backward-paragraph arg) (point))))
|
36
|
388
|
|
389 (defun transpose-paragraphs (arg)
|
|
390 "Interchange this (or next) paragraph with previous one."
|
|
391 (interactive "*p")
|
|
392 (transpose-subr 'forward-paragraph arg))
|
|
393
|
|
394 (defun start-of-paragraph-text ()
|
|
395 (let ((opoint (point)) npoint)
|
|
396 (forward-paragraph -1)
|
|
397 (setq npoint (point))
|
|
398 (skip-chars-forward " \t\n")
|
68
|
399 ;; If the range of blank lines found spans the original start point,
|
|
400 ;; try again from the beginning of it.
|
|
401 ;; Must be careful to avoid infinite loop
|
|
402 ;; when following a single return at start of buffer.
|
|
403 (if (and (>= (point) opoint) (< npoint opoint))
|
36
|
404 (progn
|
|
405 (goto-char npoint)
|
|
406 (if (> npoint (point-min))
|
|
407 (start-of-paragraph-text))))))
|
|
408
|
|
409 (defun end-of-paragraph-text ()
|
|
410 (let ((opoint (point)))
|
|
411 (forward-paragraph 1)
|
|
412 (if (eq (preceding-char) ?\n) (forward-char -1))
|
|
413 (if (<= (point) opoint)
|
|
414 (progn
|
|
415 (forward-char 1)
|
|
416 (if (< (point) (point-max))
|
|
417 (end-of-paragraph-text))))))
|
|
418
|
|
419 (defun forward-sentence (&optional arg)
|
2503
|
420 "Move forward to next `sentence-end'. With argument, repeat.
|
237
|
421 With negative argument, move backward repeatedly to `sentence-beginning'.
|
36
|
422
|
237
|
423 The variable `sentence-end' is a regular expression that matches ends of
|
|
424 sentences. Also, every paragraph boundary terminates sentences as well."
|
36
|
425 (interactive "p")
|
|
426 (or arg (setq arg 1))
|
54977
|
427 (let ((opoint (point))
|
|
428 (sentence-end (sentence-end)))
|
26055
|
429 (while (< arg 0)
|
38715
|
430 (let ((pos (point))
|
|
431 (par-beg (save-excursion (start-of-paragraph-text) (point))))
|
|
432 (if (and (re-search-backward sentence-end par-beg t)
|
|
433 (or (< (match-end 0) pos)
|
|
434 (re-search-backward sentence-end par-beg t)))
|
|
435 (goto-char (match-end 0))
|
26055
|
436 (goto-char par-beg)))
|
|
437 (setq arg (1+ arg)))
|
|
438 (while (> arg 0)
|
|
439 (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
|
|
440 (if (re-search-forward sentence-end par-end t)
|
|
441 (skip-chars-backward " \t\n")
|
|
442 (goto-char par-end)))
|
|
443 (setq arg (1- arg)))
|
|
444 (constrain-to-field nil opoint t)))
|
36
|
445
|
51159
|
446 (defun repunctuate-sentences ()
|
|
447 "Put two spaces at the end of sentences from point to the end of buffer.
|
|
448 It works using `query-replace-regexp'."
|
54014
6296b1d1ae51
* net/telnet.el (telnet-interrupt-subjob): Move doc string to the correct place.
Eli Zaretskii <eliz@is.elta.co.il>
diff
changeset
|
449 (interactive)
|
51159
|
450 (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
|
|
451 "\\1\\2\\3 "))
|
|
452
|
|
453
|
36
|
454 (defun backward-sentence (&optional arg)
|
|
455 "Move backward to start of sentence. With arg, do it arg times.
|
237
|
456 See `forward-sentence' for more information."
|
36
|
457 (interactive "p")
|
|
458 (or arg (setq arg 1))
|
|
459 (forward-sentence (- arg)))
|
|
460
|
|
461 (defun kill-sentence (&optional arg)
|
|
462 "Kill from point to end of sentence.
|
|
463 With arg, repeat; negative arg -N means kill back to Nth start of sentence."
|
27837
|
464 (interactive "p")
|
7064
|
465 (kill-region (point) (progn (forward-sentence arg) (point))))
|
36
|
466
|
|
467 (defun backward-kill-sentence (&optional arg)
|
|
468 "Kill back from point to start of sentence.
|
|
469 With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
|
27837
|
470 (interactive "p")
|
26055
|
471 (kill-region (point) (progn (backward-sentence arg) (point))))
|
36
|
472
|
|
473 (defun mark-end-of-sentence (arg)
|
43301
|
474 "Put mark at end of sentence. Arg works as in `forward-sentence'.
|
|
475 If this command is repeated, it marks the next ARG sentences after the
|
|
476 ones already marked."
|
36
|
477 (interactive "p")
|
|
478 (push-mark
|
43301
|
479 (save-excursion
|
|
480 (if (and (eq last-command this-command) (mark t))
|
|
481 (goto-char (mark)))
|
|
482 (forward-sentence arg)
|
|
483 (point))
|
|
484 nil t))
|
36
|
485
|
|
486 (defun transpose-sentences (arg)
|
|
487 "Interchange this (next) and previous sentence."
|
|
488 (interactive "*p")
|
|
489 (transpose-subr 'forward-sentence arg))
|
659
|
490
|
49949
|
491 ;;; Local Variables:
|
|
492 ;;; coding: iso-2022-7bit
|
|
493 ;;; End:
|
|
494
|
52401
|
495 ;;; arch-tag: e727eb1a-527a-4464-b9d7-9d3ec0d1a575
|
659
|
496 ;;; paragraphs.el ends here
|