Add ein-kill-ring module and its tests

This commit is contained in:
Takafumi Arakaki 2012-05-14 23:08:18 +02:00
parent d9fa116e7f
commit a1b0f98a5f
2 changed files with 96 additions and 0 deletions

55
ein-kill-ring.el Normal file
View file

@ -0,0 +1,55 @@
;;; ein-kill-ring.el --- Kill-ring for cells
;; Copyright (C) 2012- Takafumi Arakaki
;; Author: Takafumi Arakaki
;; This file is NOT part of GNU Emacs.
;; ein-kill-ring.el 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.
;; ein-kill-ring.el 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 ein-kill-ring.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Stolen from simple.el.
;;; Code:
(defvar ein:kill-ring nil)
(defvar ein:kill-ring-yank-pointer nil)
(defvar ein:kill-ring-max kill-ring-max)
(defun ein:kill-new (obj)
"Make OBJ the latest kill in the kill ring `ein:kill-ring'.
Set `ein:kill-ring-yank-pointer' to point to it."
(push obj ein:kill-ring)
(if (> (length ein:kill-ring) ein:kill-ring-max)
(setcdr (nthcdr (1- ein:kill-ring-max) ein:kill-ring) nil))
(setq ein:kill-ring-yank-pointer ein:kill-ring))
(defun ein:current-kill (n &optional do-not-move)
"Rotate the yanking point by N places, and then return that kill.
If optional arg DO-NOT-MOVE is non-nil, then don't actually
move the yanking point; just return the Nth kill forward."
(unless ein:kill-ring (error "Kill ring is empty"))
(let ((ARGth-kill-element
(nthcdr (mod (- n (length ein:kill-ring-yank-pointer))
(length ein:kill-ring))
ein:kill-ring)))
(unless do-not-move
(setq ein:kill-ring-yank-pointer ARGth-kill-element))
(car ARGth-kill-element)))
(provide 'ein-kill-ring)
;;; ein-kill-ring.el ends here

View file

@ -0,0 +1,41 @@
(eval-when-compile (require 'cl))
(require 'ert)
(require 'ein-kill-ring)
(ert-deftest ein:kill-ring-simple ()
(let (ein:kill-ring
ein:kill-ring-yank-pointer)
(ein:kill-new 1)
(should (equal (ein:current-kill 0) 1))))
(defun eintest:kill-ring-simple-repeat-setup ()
(loop for i from 0 below 5
do (ein:kill-new i)
do (should (equal (ein:current-kill 0) i))))
(ert-deftest ein:kill-ring-simple-repeat ()
(let (ein:kill-ring
ein:kill-ring-yank-pointer)
(eintest:kill-ring-simple-repeat-setup)
(should (equal ein:kill-ring ein:kill-ring-yank-pointer))
(should (equal ein:kill-ring '(4 3 2 1 0)))))
(ert-deftest ein:kill-ring-repeat-n-1 ()
(let (ein:kill-ring
ein:kill-ring-yank-pointer)
(eintest:kill-ring-simple-repeat-setup)
(loop for i in '(3 2 1 0 4 3 2)
do (should (equal (ein:current-kill 1) i)))
(should-not (equal ein:kill-ring ein:kill-ring-yank-pointer))
(should (equal ein:kill-ring '(4 3 2 1 0)))
(should (equal ein:kill-ring-yank-pointer '(2 1 0)))))
(ert-deftest ein:kill-ring-exceeds-max ()
(let (ein:kill-ring
ein:kill-ring-yank-pointer
(ein:kill-ring-max 3))
(eintest:kill-ring-simple-repeat-setup)
(should (equal ein:kill-ring ein:kill-ring-yank-pointer))
(should (equal (length ein:kill-ring) ein:kill-ring-max))
(should (equal ein:kill-ring '(4 3 2)))))