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