summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kangas <stefankangas@gmail.com>2019-06-01 18:47:14 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2019-06-01 18:52:22 -0700
commit227b3c89f05f22556d49e127f236edc2d3cfc87a (patch)
treee2261b57f44f05c795b191148cf00606106df17b
parent5ad5c4ac991bc2b35a4a92e22f17a5a73009d7bc (diff)
downloademacs-227b3c89f05f22556d49e127f236edc2d3cfc87a.tar.gz
Use lexical-binding in paragraphs.el and add tests
* lisp/textmodes/paragraphs.el: Use lexical-binding. (repunctuate-sentences): Make it work non-interactively. * test/lisp/textmodes/paragraphs-tests.el: New file.
-rw-r--r--lisp/textmodes/paragraphs.el45
-rw-r--r--test/lisp/textmodes/paragraphs-tests.el165
2 files changed, 193 insertions, 17 deletions
diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index 92a6b907859..d0fab36599b 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -1,4 +1,4 @@
-;;; paragraphs.el --- paragraph and sentence parsing
+;;; paragraphs.el --- paragraph and sentence parsing -*- lexical-binding: t -*-
;; Copyright (C) 1985-1987, 1991, 1994-1997, 1999-2019 Free Software
;; Foundation, Inc.
@@ -398,15 +398,15 @@ it marks the next ARG paragraphs after the ones already marked."
(defun kill-paragraph (arg)
"Kill forward to end of paragraph.
-With arg N, kill forward to Nth end of paragraph;
-negative arg -N means kill backward to Nth start of paragraph."
+With ARG N, kill forward to Nth end of paragraph;
+negative ARG -N means kill backward to Nth start of paragraph."
(interactive "p")
(kill-region (point) (progn (forward-paragraph arg) (point))))
(defun backward-kill-paragraph (arg)
"Kill back to start of paragraph.
-With arg N, kill back to Nth start of paragraph;
-negative arg -N means kill forward to Nth end of paragraph."
+With ARG N, kill back to Nth start of paragraph;
+negative ARG -N means kill forward to Nth end of paragraph."
(interactive "p")
(kill-region (point) (progn (backward-paragraph arg) (point))))
@@ -421,6 +421,7 @@ the current paragraph with the one containing the mark."
(transpose-subr 'forward-paragraph arg))
(defun start-of-paragraph-text ()
+ "Move to the start of the current paragraph."
(let ((opoint (point)) npoint)
(forward-paragraph -1)
(setq npoint (point))
@@ -436,6 +437,7 @@ the current paragraph with the one containing the mark."
(start-of-paragraph-text))))))
(defun end-of-paragraph-text ()
+ "Move to the end of the current paragraph."
(let ((opoint (point)))
(forward-paragraph 1)
(if (eq (preceding-char) ?\n) (forward-char -1))
@@ -447,7 +449,7 @@ the current paragraph with the one containing the mark."
(defun forward-sentence (&optional arg)
"Move forward to next end of sentence. With argument, repeat.
-With negative argument, move backward repeatedly to start of sentence.
+When ARG is negative, move backward repeatedly to start of sentence.
The variable `sentence-end' is a regular expression that matches ends of
sentences. Also, every paragraph boundary terminates sentences as well."
@@ -483,37 +485,46 @@ sentences. Also, every paragraph boundary terminates sentences as well."
(setq arg (1- arg)))
(constrain-to-field nil opoint t)))
-(defun repunctuate-sentences ()
+(defun repunctuate-sentences (&optional no-query)
"Put two spaces at the end of sentences from point to the end of buffer.
-It works using `query-replace-regexp'."
+It works using `query-replace-regexp'.
+If optional argument NO-QUERY is non-nil, make changes without
+asking for confirmation."
(interactive)
- (query-replace-regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +"
- "\\1\\2\\3 "))
+ (let ((regexp "\\([]\"')]?\\)\\([.?!]\\)\\([]\"')]?\\) +")
+ (to-string "\\1\\2\\3 "))
+ (if no-query
+ (while (re-search-forward regexp nil t)
+ (replace-match to-string))
+ (query-replace-regexp regexp to-string))))
(defun backward-sentence (&optional arg)
- "Move backward to start of sentence. With arg, do it arg times.
-See `forward-sentence' for more information."
+ "Move backward to start of sentence.
+With ARG, do it ARG times. See `forward-sentence' for more
+information."
(interactive "^p")
(or arg (setq arg 1))
(forward-sentence (- arg)))
(defun kill-sentence (&optional arg)
"Kill from point to end of sentence.
-With arg, repeat; negative arg -N means kill back to Nth start of sentence."
+With ARG, repeat; negative ARG -N means kill back to Nth start of
+sentence."
(interactive "p")
(kill-region (point) (progn (forward-sentence arg) (point))))
(defun backward-kill-sentence (&optional arg)
"Kill back from point to start of sentence.
-With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
+With ARG, repeat, or kill forward to Nth end of sentence if
+negative ARG -N."
(interactive "p")
(kill-region (point) (progn (backward-sentence arg) (point))))
(defun mark-end-of-sentence (arg)
- "Put mark at end of sentence. Arg works as in `forward-sentence'.
-If this command is repeated, it marks the next ARG sentences after the
-ones already marked."
+ "Put mark at end of sentence.
+ARG works as in `forward-sentence'. If this command is repeated,
+it marks the next ARG sentences after the ones already marked."
(interactive "p")
(push-mark
(save-excursion
diff --git a/test/lisp/textmodes/paragraphs-tests.el b/test/lisp/textmodes/paragraphs-tests.el
new file mode 100644
index 00000000000..5772756740f
--- /dev/null
+++ b/test/lisp/textmodes/paragraphs-tests.el
@@ -0,0 +1,165 @@
+;;; paragraphs-tests.el --- Tests for paragraphs.el -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2019 Free Software Foundation, Inc.
+
+;; Author: Stefan Kangas <stefankangas@gmail.com>
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'ert)
+;; (require 'paragraphs) ; loaded by default
+
+(ert-deftest paragraphs-tests-sentence-end ()
+ (should (> (length (sentence-end)) 0))
+ (let ((sentence-end "override works"))
+ (should (equal (sentence-end) sentence-end))))
+
+(ert-deftest paragraphs-tests-forward-backward-paragraph ()
+ (with-temp-buffer
+ (insert "AA\nAA\n\nBB\nBB\n")
+ (goto-char (point-min))
+ (forward-paragraph)
+ (should (equal (point) 7))
+ (forward-paragraph)
+ (should (equal (point) 14))
+ (backward-paragraph)
+ (should (equal (point) 7))
+ (backward-paragraph)
+ (should (equal (point) (point-min)))))
+
+(ert-deftest paragraphs-tests-mark-paragraph ()
+ (with-temp-buffer
+ (insert "AA\nAA\n\nBB\nBB\n")
+ (goto-char (point-min))
+ (mark-paragraph)
+ (should mark-active)
+ (should (equal (mark) 7)))
+ (should-error (mark-paragraph 0)))
+
+(ert-deftest paragraphs-tests-kill-paragraph ()
+ (with-temp-buffer
+ (insert "AA\nAA\n\nBB\nBB\n")
+ (goto-char (point-min))
+ (kill-paragraph nil)
+ (should (equal (buffer-string) "\nBB\nBB\n"))))
+
+(ert-deftest paragraphs-tests-backward-kill-paragraph ()
+ (with-temp-buffer
+ (insert "AA\nAA\n\nBB\nBB\n")
+ (goto-char 7)
+ (backward-kill-paragraph nil)
+ (should (equal (buffer-string) "\nBB\nBB\n"))))
+
+(ert-deftest paragraphs-tests-transpose-paragraphs ()
+ (with-temp-buffer
+ (insert "AA\nAA\n\nBB\nBB\n")
+ (goto-char (point-min))
+ (transpose-paragraphs 1)
+ (should (equal (buffer-string) "\nBB\nBB\nAA\nAA\n"))))
+
+(ert-deftest paragraphs-tests-start-of-paragraph-text ()
+ (with-temp-buffer
+ (insert "AA\nAA\n\nBB\nBB\n")
+ (goto-char (point-max))
+ (start-of-paragraph-text)
+ (should (equal (point) 8))))
+
+(ert-deftest paragraphs-tests-end-of-paragraph-text ()
+ (with-temp-buffer
+ (insert "AA\nAA\n\nBB\nBB\n")
+ (goto-char (point-min))
+ (end-of-paragraph-text)
+ (should (equal (point) 6))))
+
+(ert-deftest paragraphs-tests-forward-sentence ()
+ (with-temp-buffer
+ (insert "First sentence. Second sentence.")
+ (goto-char (point-min))
+ (forward-sentence)
+ (should (equal (point) 16))
+ (goto-char (point-min))
+ (forward-sentence 2)
+ (should (equal (point) 34))))
+
+(ert-deftest paragraphs-tests-repunctuate-sentences ()
+ (with-temp-buffer
+ (insert "Just. Some. Sentences.")
+ (goto-char (point-min))
+ (repunctuate-sentences t)
+ (should (equal (buffer-string) "Just. Some. Sentences."))))
+
+(ert-deftest paragraphs-tests-backward-sentence ()
+ (with-temp-buffer
+ (insert "First sentence. Second sentence.")
+ (goto-char (point-max))
+ (backward-sentence)
+ (should (equal (point) 18))))
+
+(ert-deftest paragraphs-tests-kill-sentence ()
+ (with-temp-buffer
+ (insert "First sentence. Second sentence.")
+ (goto-char (point-min))
+ (kill-sentence)
+ (should (equal (buffer-string) " Second sentence."))))
+
+(ert-deftest paragraphs-tests-backward-kill-sentence ()
+ (with-temp-buffer
+ (insert "Should not be killed. Should be killed.")
+ (goto-char (point-max))
+ (backward-kill-sentence)
+ (should (equal (buffer-string) "Should not be killed. "))))
+
+(ert-deftest paragraphs-tests-mark-end-of-sentence ()
+ (with-temp-buffer
+ (insert "Example sentence. Followed by another one.")
+ (goto-char (point-min))
+ (mark-end-of-sentence 1)
+ (should mark-active)
+ (should (equal (mark) 18)))
+ (with-temp-buffer
+ (insert "Example sentence. Followed by another one.")
+ (goto-char (point-min))
+ (mark-end-of-sentence 2)
+ (should mark-active)
+ (should (equal (mark) 44)))
+ ;; FIXME: This does not work -- how do I do it?
+ (with-temp-buffer ; test repeating the command
+ (insert "Example sentence. Followed by another one.")
+ (goto-char (point-min))
+ (mark-end-of-sentence 1)
+ (setq last-command 'mark-end-of-sentence) ; hack
+ (mark-end-of-sentence 1)
+ (should mark-active)
+ (should (equal (mark) 18))))
+
+(ert-deftest paragraphs-tests-transpose-sentences ()
+ (with-temp-buffer
+ (insert "First sentence. Second sentence. Third sentence.")
+ (goto-char (point-min))
+ (transpose-sentences 1)
+ (should (equal (buffer-string)
+ "Second sentence. First sentence. Third sentence."))
+ (goto-char (point-min))
+ (transpose-sentences 2)
+ (should (equal (buffer-string)
+ "First sentence. Third sentence. Second sentence."))))
+
+(provide 'paragraphs-tests)
+;;; paragraphs-tests.el ends here