|
|
1.1 ! root 1: ;; Paragraph and sentence parsing. ! 2: ;; Copyright (C) 1985 Free Software Foundation, Inc. ! 3: ! 4: ;; This file is part of GNU Emacs. ! 5: ! 6: ;; GNU Emacs is distributed in the hope that it will be useful, ! 7: ;; but WITHOUT ANY WARRANTY. No author or distributor ! 8: ;; accepts responsibility to anyone for the consequences of using it ! 9: ;; or for whether it serves any particular purpose or works at all, ! 10: ;; unless he says so in writing. Refer to the GNU Emacs General Public ! 11: ;; License for full details. ! 12: ! 13: ;; Everyone is granted permission to copy, modify and redistribute ! 14: ;; GNU Emacs, but only under the conditions described in the ! 15: ;; GNU Emacs General Public License. A copy of this license is ! 16: ;; supposed to have been given to you along with GNU Emacs so you ! 17: ;; can know your rights and responsibilities. It should be in a ! 18: ;; file named COPYING. Among other things, the copyright notice ! 19: ;; and this notice must be preserved on all copies. ! 20: ! 21: ! 22: (defvar paragraph-ignore-fill-prefix nil ! 23: "Non-nil means the paragraph commands are not affected by fill-prefix. ! 24: This is desirable in modes where blank lines are the paragraph delimiters.") ! 25: ! 26: (defun forward-paragraph (&optional arg) ! 27: "Move forward to end of paragraph. With arg, do it arg times. ! 28: A line which paragraph-start matches either separates paragraphs ! 29: \(if paragraph-separate matches it also) or is the first line of a paragraph. ! 30: A paragraph end is the beginning of a line which is not part of the paragraph ! 31: to which the end of the previous line belongs, or the end of the buffer." ! 32: (interactive "p") ! 33: (or arg (setq arg 1)) ! 34: (let* ((fill-prefix-regexp ! 35: (and fill-prefix (not (equal fill-prefix "")) ! 36: (not paragraph-ignore-fill-prefix) ! 37: (regexp-quote fill-prefix))) ! 38: (paragraph-separate ! 39: (if fill-prefix-regexp ! 40: (concat paragraph-separate "\\|^" ! 41: fill-prefix-regexp "[ \t]*$") ! 42: paragraph-separate))) ! 43: (while (< arg 0) ! 44: (if (and (not (looking-at paragraph-separate)) ! 45: (re-search-backward "^\n" (max (1- (point)) (point-min)) t)) ! 46: nil ! 47: (forward-char -1) (beginning-of-line) ! 48: (while (and (not (bobp)) (looking-at paragraph-separate)) ! 49: (forward-line -1)) ! 50: (end-of-line) ! 51: ;; Search back for line that starts or separates paragraphs. ! 52: (if (if fill-prefix-regexp ! 53: ;; There is a fill prefix; it overrides paragraph-start. ! 54: (progn ! 55: (while (progn (beginning-of-line) ! 56: (and (not (bobp)) ! 57: (not (looking-at paragraph-separate)) ! 58: (looking-at fill-prefix-regexp))) ! 59: (forward-line -1)) ! 60: (not (bobp))) ! 61: (re-search-backward paragraph-start nil t)) ! 62: ;; Found one. ! 63: (progn ! 64: (while (looking-at paragraph-separate) ! 65: (forward-line 1)) ! 66: (if (eq (char-after (- (point) 2)) ?\n) ! 67: (forward-line -1))) ! 68: ;; No starter or separator line => use buffer beg. ! 69: (goto-char (point-min)))) ! 70: (setq arg (1+ arg))) ! 71: (while (> arg 0) ! 72: (beginning-of-line) ! 73: (while (prog1 (and (not (eobp)) ! 74: (looking-at paragraph-separate)) ! 75: (forward-line 1))) ! 76: (if fill-prefix-regexp ! 77: ;; There is a fill prefix; it overrides paragraph-start. ! 78: (while (and (not (eobp)) ! 79: (not (looking-at paragraph-separate)) ! 80: (looking-at fill-prefix-regexp)) ! 81: (forward-line 1)) ! 82: (if (re-search-forward paragraph-start nil t) ! 83: (goto-char (match-beginning 0)) ! 84: (goto-char (point-max)))) ! 85: (setq arg (1- arg))))) ! 86: ! 87: (defun backward-paragraph (&optional arg) ! 88: "Move backward to start of paragraph. With arg, do it arg times. ! 89: A paragraph start is the beginning of a line which is a first-line-of-paragraph ! 90: or which is ordinary text and follows a paragraph-separating line; except: ! 91: if the first real line of a paragraph is preceded by a blank line, ! 92: the paragraph starts at that blank line. ! 93: See forward-paragraph for more information." ! 94: (interactive "p") ! 95: (or arg (setq arg 1)) ! 96: (forward-paragraph (- arg))) ! 97: ! 98: (defun mark-paragraph () ! 99: "Put point at beginning of this paragraph, mark at end." ! 100: (interactive) ! 101: (forward-paragraph 1) ! 102: (push-mark nil t) ! 103: (backward-paragraph 1)) ! 104: ! 105: (defun kill-paragraph (arg) ! 106: "Kill to end of paragraph." ! 107: (interactive "*p") ! 108: (kill-region (point) (progn (forward-paragraph arg) (point)))) ! 109: ! 110: (defun backward-kill-paragraph (arg) ! 111: "Kill back to start of paragraph." ! 112: (interactive "*p") ! 113: (kill-region (point) (progn (backward-paragraph arg) (point)))) ! 114: ! 115: (defun transpose-paragraphs (arg) ! 116: "Interchange this (or next) paragraph with previous one." ! 117: (interactive "*p") ! 118: (transpose-subr 'forward-paragraph arg)) ! 119: ! 120: (defun start-of-paragraph-text () ! 121: (let ((opoint (point)) npoint) ! 122: (forward-paragraph -1) ! 123: (setq npoint (point)) ! 124: (skip-chars-forward " \t\n") ! 125: (if (>= (point) opoint) ! 126: (progn ! 127: (goto-char npoint) ! 128: (if (> npoint (point-min)) ! 129: (start-of-paragraph-text)))))) ! 130: ! 131: (defun end-of-paragraph-text () ! 132: (let ((opoint (point))) ! 133: (forward-paragraph 1) ! 134: (if (eq (preceding-char) ?\n) (forward-char -1)) ! 135: (if (<= (point) opoint) ! 136: (progn ! 137: (forward-char 1) ! 138: (if (< (point) (point-max)) ! 139: (end-of-paragraph-text)))))) ! 140: ! 141: (defun forward-sentence (&optional arg) ! 142: "Move forward to next sentence-end. With argument, repeat. ! 143: With negative argument, move backward repeatedly to sentence-beginning. ! 144: Sentence ends are identified by the value of sentence-end ! 145: treated as a regular expression. Also, every paragraph boundary ! 146: terminates sentences as well." ! 147: (interactive "p") ! 148: (or arg (setq arg 1)) ! 149: (while (< arg 0) ! 150: (let ((par-beg (save-excursion (start-of-paragraph-text) (point)))) ! 151: (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t) ! 152: (goto-char (1- (match-end 0))) ! 153: (goto-char par-beg))) ! 154: (setq arg (1+ arg))) ! 155: (while (> arg 0) ! 156: (let ((par-end (save-excursion (end-of-paragraph-text) (point)))) ! 157: (if (re-search-forward sentence-end par-end t) ! 158: (skip-chars-backward " \t\n") ! 159: (goto-char par-end))) ! 160: (setq arg (1- arg)))) ! 161: ! 162: (defun backward-sentence (&optional arg) ! 163: "Move backward to start of sentence. With arg, do it arg times. ! 164: See forward-sentence for more information." ! 165: (interactive "p") ! 166: (or arg (setq arg 1)) ! 167: (forward-sentence (- arg))) ! 168: ! 169: (defun kill-sentence (&optional arg) ! 170: "Kill from point to end of sentence. ! 171: With arg, repeat, or backward if negative arg." ! 172: (interactive "*p") ! 173: (let ((beg (point))) ! 174: (forward-sentence arg) ! 175: (kill-region beg (point)))) ! 176: ! 177: (defun backward-kill-sentence (&optional arg) ! 178: "Kill back from point to start of sentence. ! 179: With arg, repeat, or forward if negative arg." ! 180: (interactive "*p") ! 181: (let ((beg (point))) ! 182: (backward-sentence arg) ! 183: (kill-region beg (point)))) ! 184: ! 185: (defun mark-end-of-sentence (arg) ! 186: "Put mark at end of sentence. Arg works as in forward-sentence." ! 187: (interactive "p") ! 188: (push-mark ! 189: (save-excursion ! 190: (forward-sentence arg) ! 191: (point)))) ! 192: ! 193: (defun transpose-sentences (arg) ! 194: "Interchange this (next) and previous sentence." ! 195: (interactive "*p") ! 196: (transpose-subr 'forward-sentence arg))
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.