home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / emacs-15.0.3 / lisp / paragraphs.el < prev    next >
Lisp/Scheme  |  1990-07-19  |  7KB  |  201 lines

  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 (and (not (eobp)) (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 the range of blank lines found spans the original start point,
  126.     ;; try again from the beginning of it.
  127.     ;; Must be careful to avoid infinite loop
  128.     ;; when following a single return at start of buffer.
  129.     (if (and (>= (point) opoint) (< npoint opoint))
  130.     (progn
  131.       (goto-char npoint)
  132.       (if (> npoint (point-min))
  133.           (start-of-paragraph-text))))))
  134.  
  135. (defun end-of-paragraph-text ()
  136.   (let ((opoint (point)))
  137.     (forward-paragraph 1)
  138.     (if (eq (preceding-char) ?\n) (forward-char -1))
  139.     (if (<= (point) opoint)
  140.     (progn
  141.       (forward-char 1)
  142.       (if (< (point) (point-max))
  143.           (end-of-paragraph-text))))))
  144.  
  145. (defun forward-sentence (&optional arg)
  146.   "Move forward to next sentence-end.  With argument, repeat.
  147. With negative argument, move backward repeatedly to sentence-beginning.
  148. Sentence ends are identified by the value of sentence-end
  149. treated as a regular expression.  Also, every paragraph boundary
  150. terminates sentences as well."
  151.   (interactive "p")
  152.   (or arg (setq arg 1))
  153.   (while (< arg 0)
  154.     (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
  155.       (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
  156.       (goto-char (1- (match-end 0)))
  157.     (goto-char par-beg)))
  158.     (setq arg (1+ arg)))
  159.   (while (> arg 0)
  160.     (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
  161.       (if (re-search-forward sentence-end par-end t)
  162.       (skip-chars-backward " \t\n")
  163.     (goto-char par-end)))
  164.     (setq arg (1- arg))))
  165.  
  166. (defun backward-sentence (&optional arg)
  167.   "Move backward to start of sentence.  With arg, do it arg times.
  168. See forward-sentence for more information."
  169.   (interactive "p")
  170.   (or arg (setq arg 1))
  171.   (forward-sentence (- arg)))
  172.  
  173. (defun kill-sentence (&optional arg)
  174.   "Kill from point to end of sentence.
  175. With arg, repeat, or backward if negative arg."
  176.   (interactive "*p")
  177.   (let ((beg (point)))
  178.     (forward-sentence arg)
  179.     (kill-region beg (point))))
  180.  
  181. (defun backward-kill-sentence (&optional arg)
  182.   "Kill back from point to start of sentence.
  183. With arg, repeat, or forward if negative arg."
  184.   (interactive "*p")
  185.   (let ((beg (point)))
  186.     (backward-sentence arg)
  187.     (kill-region beg (point))))
  188.  
  189. (defun mark-end-of-sentence (arg)
  190.   "Put mark at end of sentence.  Arg works as in forward-sentence."
  191.   (interactive "p")
  192.   (push-mark
  193.     (save-excursion
  194.       (forward-sentence arg)
  195.       (point))))
  196.  
  197. (defun transpose-sentences (arg)
  198.   "Interchange this (next) and previous sentence."
  199.   (interactive "*p")
  200.   (transpose-subr 'forward-sentence arg))
  201.