| 1 | ;;; paragraphs.el --- paragraph and sentence parsing. |
| 2 | |
| 3 | ;; Copyright (C) 1985, 86, 87, 91, 94, 95, 96, 1997 |
| 4 | ;; Free Software Foundation, Inc. |
| 5 | |
| 6 | ;; Maintainer: FSF |
| 7 | ;; Keywords: wp |
| 8 | |
| 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 |
| 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
| 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 |
| 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. |
| 25 | |
| 26 | ;;; Commentary: |
| 27 | |
| 28 | ;; This package provides the paragraph-oriented commands documented in the |
| 29 | ;; Emacs manual. |
| 30 | |
| 31 | ;;; Code: |
| 32 | |
| 33 | (defgroup paragraphs nil |
| 34 | "Paragraph and sentence parsing." |
| 35 | :group 'editing) |
| 36 | |
| 37 | ;; It isn't useful to use defcustom for this variable |
| 38 | ;; because it is always buffer-local. |
| 39 | (defvar use-hard-newlines nil |
| 40 | "Non-nil means to distinguish hard and soft newlines. |
| 41 | See also the documentation for the function `use-hard-newlines'.") |
| 42 | (make-variable-buffer-local 'use-hard-newlines) |
| 43 | |
| 44 | (defun use-hard-newlines (&optional arg insert) |
| 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 |
| 53 | newlines, ask the user whether to mark as hard any newlines preceeding a |
| 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 |
| 56 | marking, `guess' to try to do the right thing with no questions, nil |
| 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." |
| 61 | (interactive (list current-prefix-arg nil)) |
| 62 | (if (or (<= (prefix-numeric-value arg) 0) |
| 63 | (and use-hard-newlines (null arg))) |
| 64 | ;; Turn mode off |
| 65 | (setq use-hard-newlines nil) |
| 66 | ;; Turn mode on |
| 67 | ;; Intuit hard newlines -- |
| 68 | ;; mark as hard any newlines preceding a paragraph-start line. |
| 69 | (if (or (eq insert t) (eq insert 'always) |
| 70 | (and (not (eq 'never insert)) |
| 71 | (not use-hard-newlines) |
| 72 | (not (text-property-any (point-min) (point-max) 'hard t)) |
| 73 | (save-excursion |
| 74 | (goto-char (point-min)) |
| 75 | (search-forward "\n" nil t)) |
| 76 | (or (eq insert 'guess) |
| 77 | (y-or-n-p "Make newlines between paragraphs hard? ")))) |
| 78 | (save-excursion |
| 79 | (goto-char (point-min)) |
| 80 | (while (search-forward "\n" nil t) |
| 81 | (let ((pos (point))) |
| 82 | (move-to-left-margin) |
| 83 | (if (looking-at paragraph-start) |
| 84 | (progn |
| 85 | (set-hard-newline-properties (1- pos) pos) |
| 86 | ;; If paragraph-separate, newline after it is hard too. |
| 87 | (if (looking-at paragraph-separate) |
| 88 | (progn |
| 89 | (end-of-line) |
| 90 | (if (not (eobp)) |
| 91 | (set-hard-newline-properties |
| 92 | (point) (1+ (point)))))))))))) |
| 93 | (setq use-hard-newlines t))) |
| 94 | |
| 95 | (defcustom paragraph-start "[ \t\n\f]" "\ |
| 96 | *Regexp for beginning of a line that starts OR separates paragraphs. |
| 97 | This regexp should match lines that separate paragraphs |
| 98 | and should also match lines that start a paragraph |
| 99 | \(and are part of that paragraph). |
| 100 | |
| 101 | This is matched against the text at the left margin, which is not necessarily |
| 102 | the beginning of the line, so it should never use \"^\" as an anchor. This |
| 103 | ensures that the paragraph functions will work equally well within a region |
| 104 | of text indented by a margin setting. |
| 105 | |
| 106 | The variable `paragraph-separate' specifies how to distinguish |
| 107 | lines that start paragraphs from lines that separate them. |
| 108 | |
| 109 | If the variable `use-hard-newlines' is nonnil, then only lines following a |
| 110 | hard newline are considered to match." |
| 111 | :group 'paragraphs |
| 112 | :type 'regexp) |
| 113 | |
| 114 | ;; paragraph-start requires a hard newline, but paragraph-separate does not: |
| 115 | ;; It is assumed that paragraph-separate is distinctive enough to be believed |
| 116 | ;; whenever it occurs, while it is reasonable to set paragraph-start to |
| 117 | ;; something very minimal, even including "." (which makes every hard newline |
| 118 | ;; start a new paragraph). |
| 119 | |
| 120 | (defcustom paragraph-separate "[ \t\f]*$" |
| 121 | "*Regexp for beginning of a line that separates paragraphs. |
| 122 | If you change this, you may have to change paragraph-start also. |
| 123 | |
| 124 | This is matched against the text at the left margin, which is not necessarily |
| 125 | the beginning of the line, so it should not use \"^\" as an anchor. This |
| 126 | ensures that the paragraph functions will work equally within a region of |
| 127 | text indented by a margin setting." |
| 128 | :group 'paragraphs |
| 129 | :type 'regexp) |
| 130 | |
| 131 | (defcustom sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)[ \t\n]*") |
| 132 | "*Regexp describing the end of a sentence. |
| 133 | All paragraph boundaries also end sentences, regardless. |
| 134 | |
| 135 | In order to be recognized as the end of a sentence, the ending period, |
| 136 | question mark, or exclamation point must be followed by two spaces, |
| 137 | unless it's inside some sort of quotes or parenthesis." |
| 138 | :group 'paragraphs |
| 139 | :type 'regexp) |
| 140 | |
| 141 | (defcustom page-delimiter "^\014" |
| 142 | "*Regexp describing line-beginnings that separate pages." |
| 143 | :group 'paragraphs |
| 144 | :type 'regexp) |
| 145 | |
| 146 | (defcustom paragraph-ignore-fill-prefix nil |
| 147 | "*Non-nil means the paragraph commands are not affected by `fill-prefix'. |
| 148 | This is desirable in modes where blank lines are the paragraph delimiters." |
| 149 | :group 'paragraphs |
| 150 | :type 'boolean) |
| 151 | |
| 152 | (defun forward-paragraph (&optional arg) |
| 153 | "Move forward to end of paragraph. |
| 154 | With argument ARG, do it ARG times; |
| 155 | a negative argument ARG = -N means move backward N paragraphs. |
| 156 | |
| 157 | A line which `paragraph-start' matches either separates paragraphs |
| 158 | \(if `paragraph-separate' matches it also) or is the first line of a paragraph. |
| 159 | A paragraph end is the beginning of a line which is not part of the paragraph |
| 160 | to which the end of the previous line belongs, or the end of the buffer." |
| 161 | (interactive "p") |
| 162 | (or arg (setq arg 1)) |
| 163 | (let* ((fill-prefix-regexp |
| 164 | (and fill-prefix (not (equal fill-prefix "")) |
| 165 | (not paragraph-ignore-fill-prefix) |
| 166 | (regexp-quote fill-prefix))) |
| 167 | ;; Remove ^ from paragraph-start and paragraph-sep if they are there. |
| 168 | ;; These regexps shouldn't be anchored, because we look for them |
| 169 | ;; starting at the left-margin. This allows paragraph commands to |
| 170 | ;; work normally with indented text. |
| 171 | ;; This hack will not find problem cases like "whatever\\|^something". |
| 172 | (paragraph-start (if (and (not (equal "" paragraph-start)) |
| 173 | (equal ?^ (aref paragraph-start 0))) |
| 174 | (substring paragraph-start 1) |
| 175 | paragraph-start)) |
| 176 | (paragraph-separate (if (and (not (equal "" paragraph-separate)) |
| 177 | (equal ?^ (aref paragraph-separate 0))) |
| 178 | (substring paragraph-separate 1) |
| 179 | paragraph-separate)) |
| 180 | (paragraph-separate |
| 181 | (if fill-prefix-regexp |
| 182 | (concat paragraph-separate "\\|" |
| 183 | fill-prefix-regexp "[ \t]*$") |
| 184 | paragraph-separate)) |
| 185 | ;; This is used for searching. |
| 186 | (sp-paragraph-start (concat "^[ \t]*\\(" paragraph-start "\\)")) |
| 187 | start found-start) |
| 188 | (while (and (< arg 0) (not (bobp))) |
| 189 | (if (and (not (looking-at paragraph-separate)) |
| 190 | (re-search-backward "^\n" (max (1- (point)) (point-min)) t) |
| 191 | (looking-at paragraph-separate)) |
| 192 | nil |
| 193 | (setq start (point)) |
| 194 | ;; Move back over paragraph-separating lines. |
| 195 | (forward-char -1) (beginning-of-line) |
| 196 | (while (and (not (bobp)) |
| 197 | (progn (move-to-left-margin) |
| 198 | (looking-at paragraph-separate))) |
| 199 | (forward-line -1)) |
| 200 | (if (bobp) |
| 201 | nil |
| 202 | ;; Go to end of the previous (non-separating) line. |
| 203 | (end-of-line) |
| 204 | ;; Search back for line that starts or separates paragraphs. |
| 205 | (if (if fill-prefix-regexp |
| 206 | ;; There is a fill prefix; it overrides paragraph-start. |
| 207 | (let (multiple-lines) |
| 208 | (while (and (progn (beginning-of-line) (not (bobp))) |
| 209 | (progn (move-to-left-margin) |
| 210 | (not (looking-at paragraph-separate))) |
| 211 | (looking-at fill-prefix-regexp)) |
| 212 | (if (not (= (point) start)) |
| 213 | (setq multiple-lines t)) |
| 214 | (forward-line -1)) |
| 215 | (move-to-left-margin) |
| 216 | ;;; This deleted code caused a long hanging-indent line |
| 217 | ;;; not to be filled together with the following lines. |
| 218 | ;;; ;; Don't move back over a line before the paragraph |
| 219 | ;;; ;; which doesn't start with fill-prefix |
| 220 | ;;; ;; unless that is the only line we've moved over. |
| 221 | ;;; (and (not (looking-at fill-prefix-regexp)) |
| 222 | ;;; multiple-lines |
| 223 | ;;; (forward-line 1)) |
| 224 | (not (bobp))) |
| 225 | (while (and (re-search-backward sp-paragraph-start nil 1) |
| 226 | (setq found-start t) |
| 227 | ;; Found a candidate, but need to check if it is a |
| 228 | ;; REAL paragraph-start. |
| 229 | (progn (setq start (point)) |
| 230 | (move-to-left-margin) |
| 231 | (not (looking-at paragraph-separate))) |
| 232 | (not (and (looking-at paragraph-start) |
| 233 | (not |
| 234 | (and use-hard-newlines |
| 235 | (not (bobp)) |
| 236 | (not (get-text-property (1- start) |
| 237 | 'hard))))))) |
| 238 | (setq found-start nil) |
| 239 | (goto-char start)) |
| 240 | found-start) |
| 241 | ;; Found one. |
| 242 | (progn |
| 243 | ;; Move forward over paragraph separators. |
| 244 | ;; We know this cannot reach the place we started |
| 245 | ;; because we know we moved back over a non-separator. |
| 246 | (while (and (not (eobp)) |
| 247 | (progn (move-to-left-margin) |
| 248 | (looking-at paragraph-separate))) |
| 249 | (forward-line 1)) |
| 250 | ;; If line before paragraph is just margin, back up to there. |
| 251 | (end-of-line 0) |
| 252 | (if (> (current-column) (current-left-margin)) |
| 253 | (forward-char 1) |
| 254 | (skip-chars-backward " \t") |
| 255 | (if (not (bolp)) |
| 256 | (forward-line 1)))) |
| 257 | ;; No starter or separator line => use buffer beg. |
| 258 | (goto-char (point-min))))) |
| 259 | (setq arg (1+ arg))) |
| 260 | (while (and (> arg 0) (not (eobp))) |
| 261 | ;; Move forward over separator lines, and one more line. |
| 262 | (while (prog1 (and (not (eobp)) |
| 263 | (progn (move-to-left-margin) (not (eobp))) |
| 264 | (looking-at paragraph-separate)) |
| 265 | (forward-line 1))) |
| 266 | (if fill-prefix-regexp |
| 267 | ;; There is a fill prefix; it overrides paragraph-start. |
| 268 | (while (and (not (eobp)) |
| 269 | (progn (move-to-left-margin) (not (eobp))) |
| 270 | (not (looking-at paragraph-separate)) |
| 271 | (looking-at fill-prefix-regexp)) |
| 272 | (forward-line 1)) |
| 273 | (while (and (re-search-forward sp-paragraph-start nil 1) |
| 274 | (progn (setq start (match-beginning 0)) |
| 275 | (goto-char start) |
| 276 | (not (eobp))) |
| 277 | (progn (move-to-left-margin) |
| 278 | (not (looking-at paragraph-separate))) |
| 279 | (or (not (looking-at paragraph-start)) |
| 280 | (and use-hard-newlines |
| 281 | (not (get-text-property (1- start) 'hard))))) |
| 282 | (forward-char 1)) |
| 283 | (if (< (point) (point-max)) |
| 284 | (goto-char start))) |
| 285 | (setq arg (1- arg))))) |
| 286 | |
| 287 | (defun backward-paragraph (&optional arg) |
| 288 | "Move backward to start of paragraph. |
| 289 | With argument ARG, do it ARG times; |
| 290 | a negative argument ARG = -N means move forward N paragraphs. |
| 291 | |
| 292 | A paragraph start is the beginning of a line which is a |
| 293 | `first-line-of-paragraph' or which is ordinary text and follows a |
| 294 | paragraph-separating line; except: if the first real line of a |
| 295 | paragraph is preceded by a blank line, the paragraph starts at that |
| 296 | blank line. |
| 297 | |
| 298 | See `forward-paragraph' for more information." |
| 299 | (interactive "p") |
| 300 | (or arg (setq arg 1)) |
| 301 | (forward-paragraph (- arg))) |
| 302 | |
| 303 | (defun mark-paragraph () |
| 304 | "Put point at beginning of this paragraph, mark at end. |
| 305 | The paragraph marked is the one that contains point or follows point." |
| 306 | (interactive) |
| 307 | (forward-paragraph 1) |
| 308 | (push-mark nil t t) |
| 309 | (backward-paragraph 1)) |
| 310 | |
| 311 | (defun kill-paragraph (arg) |
| 312 | "Kill forward to end of paragraph. |
| 313 | With arg N, kill forward to Nth end of paragraph; |
| 314 | negative arg -N means kill backward to Nth start of paragraph." |
| 315 | (interactive "p") |
| 316 | (kill-region (point) (progn (forward-paragraph arg) (point)))) |
| 317 | |
| 318 | (defun backward-kill-paragraph (arg) |
| 319 | "Kill back to start of paragraph. |
| 320 | With arg N, kill back to Nth start of paragraph; |
| 321 | negative arg -N means kill forward to Nth end of paragraph." |
| 322 | (interactive "p") |
| 323 | (kill-region (point) (progn (backward-paragraph arg) (point)))) |
| 324 | |
| 325 | (defun transpose-paragraphs (arg) |
| 326 | "Interchange this (or next) paragraph with previous one." |
| 327 | (interactive "*p") |
| 328 | (transpose-subr 'forward-paragraph arg)) |
| 329 | |
| 330 | (defun start-of-paragraph-text () |
| 331 | (let ((opoint (point)) npoint) |
| 332 | (forward-paragraph -1) |
| 333 | (setq npoint (point)) |
| 334 | (skip-chars-forward " \t\n") |
| 335 | ;; If the range of blank lines found spans the original start point, |
| 336 | ;; try again from the beginning of it. |
| 337 | ;; Must be careful to avoid infinite loop |
| 338 | ;; when following a single return at start of buffer. |
| 339 | (if (and (>= (point) opoint) (< npoint opoint)) |
| 340 | (progn |
| 341 | (goto-char npoint) |
| 342 | (if (> npoint (point-min)) |
| 343 | (start-of-paragraph-text)))))) |
| 344 | |
| 345 | (defun end-of-paragraph-text () |
| 346 | (let ((opoint (point))) |
| 347 | (forward-paragraph 1) |
| 348 | (if (eq (preceding-char) ?\n) (forward-char -1)) |
| 349 | (if (<= (point) opoint) |
| 350 | (progn |
| 351 | (forward-char 1) |
| 352 | (if (< (point) (point-max)) |
| 353 | (end-of-paragraph-text)))))) |
| 354 | |
| 355 | (defun forward-sentence (&optional arg) |
| 356 | "Move forward to next `sentence-end'. With argument, repeat. |
| 357 | With negative argument, move backward repeatedly to `sentence-beginning'. |
| 358 | |
| 359 | The variable `sentence-end' is a regular expression that matches ends of |
| 360 | sentences. Also, every paragraph boundary terminates sentences as well." |
| 361 | (interactive "p") |
| 362 | (or arg (setq arg 1)) |
| 363 | (while (< arg 0) |
| 364 | (let ((par-beg (save-excursion (start-of-paragraph-text) (point)))) |
| 365 | (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t) |
| 366 | (goto-char (1- (match-end 0))) |
| 367 | (goto-char par-beg))) |
| 368 | (setq arg (1+ arg))) |
| 369 | (while (> arg 0) |
| 370 | (let ((par-end (save-excursion (end-of-paragraph-text) (point)))) |
| 371 | (if (re-search-forward sentence-end par-end t) |
| 372 | (skip-chars-backward " \t\n") |
| 373 | (goto-char par-end))) |
| 374 | (setq arg (1- arg)))) |
| 375 | |
| 376 | (defun backward-sentence (&optional arg) |
| 377 | "Move backward to start of sentence. With arg, do it arg times. |
| 378 | See `forward-sentence' for more information." |
| 379 | (interactive "p") |
| 380 | (or arg (setq arg 1)) |
| 381 | (forward-sentence (- arg))) |
| 382 | |
| 383 | (defun kill-sentence (&optional arg) |
| 384 | "Kill from point to end of sentence. |
| 385 | With arg, repeat; negative arg -N means kill back to Nth start of sentence." |
| 386 | (interactive "p") |
| 387 | (kill-region (point) (progn (forward-sentence arg) (point)))) |
| 388 | |
| 389 | (defun backward-kill-sentence (&optional arg) |
| 390 | "Kill back from point to start of sentence. |
| 391 | With arg, repeat, or kill forward to Nth end of sentence if negative arg -N." |
| 392 | (interactive "p") |
| 393 | (kill-region (point) (progn (backward-sentence arg) (point)))) |
| 394 | |
| 395 | (defun mark-end-of-sentence (arg) |
| 396 | "Put mark at end of sentence. Arg works as in `forward-sentence'." |
| 397 | (interactive "p") |
| 398 | (push-mark |
| 399 | (save-excursion |
| 400 | (forward-sentence arg) |
| 401 | (point)) |
| 402 | nil t)) |
| 403 | |
| 404 | (defun transpose-sentences (arg) |
| 405 | "Interchange this (next) and previous sentence." |
| 406 | (interactive "*p") |
| 407 | (transpose-subr 'forward-sentence arg)) |
| 408 | |
| 409 | ;;; paragraphs.el ends here |