Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Documentation and renaming (current -> most-recent)

Signed-off-by: Damien Cassou <damien.cassou@gmail.com>
  • Loading branch information...
commit 48d088a8a6df66c8a35a7ea78d85c181674c7751 1 parent ed8d109
@DamienCassou DamienCassou authored
Showing with 92 additions and 59 deletions.
  1. +42 −42 rswitcher-test.el
  2. +48 −15 rswitcher.el
  3. +2 −2 shell-switcher.el
View
84 rswitcher-test.el
@@ -8,16 +8,16 @@
(rswitcher-add s 'b)
(rswitcher-add s 'a)
(should (equal '(a b c) (rswitcher--elements s)))
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(should (equal 'a (rswitcher--delete s 0)))
(should (equal '(b c) (rswitcher--elements s)))
- (should (equal 'b (rswitcher-current s))))
+ (should (equal 'b (rswitcher-most-recent s))))
(let ((s (rswitcher-make)))
(rswitcher-add s 'a)
(should (equal '(a) (rswitcher--elements s)))
(should (equal 'a (rswitcher--delete s 0)))
(should (equal '() (rswitcher--elements s)))
- (should (equal nil (rswitcher-current s)))))
+ (should (equal nil (rswitcher-most-recent s)))))
(ert-deftest rswitcher-test-delete-last ()
"rswitcher--delete removes last correctly"
@@ -26,9 +26,9 @@
(rswitcher-add s 'b)
(rswitcher-add s 'a)
(should (equal '(a b c) (rswitcher--elements s)))
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(should (equal 'c (rswitcher--delete s 2)))
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(should (equal '(a b) (rswitcher--elements s)))))
(ert-deftest rswitcher-test-delete-middle ()
@@ -38,9 +38,9 @@
(rswitcher-add s 'b)
(rswitcher-add s 'a)
(should (equal '(a b c) (rswitcher--elements s)))
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(should (equal 'b (rswitcher--delete s 1)))
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(should (equal '(a c) (rswitcher--elements s)))))
(ert-deftest rswitcher-test-delete-after-switch ()
@@ -48,75 +48,75 @@
(rswitcher-add s 'c)
(rswitcher-add s 'b)
(rswitcher-add s 'a)
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(rswitcher-switch-full s)
(rswitcher-switch-partial s)
- (should (equal 'c (rswitcher-current s)))
+ (should (equal 'c (rswitcher-most-recent s)))
(should (equal '(a b c) (rswitcher--elements s)))
(rswitcher--delete s 2)
(should (equal '(a b) (rswitcher--elements s)))
- (should (equal 'a (rswitcher-current s)))))
+ (should (equal 'a (rswitcher-most-recent s)))))
-(ert-deftest rswitcher-test-add-make-current ()
- "rswitcher-add makes new element current"
+(ert-deftest rswitcher-test-add-make-most-recent ()
+ "rswitcher-add makes new element most-recent"
(let ((s (rswitcher-make)))
(rswitcher-add s 'a)
- (should (equal (rswitcher-current s) 'a))
+ (should (equal (rswitcher-most-recent s) 'a))
(rswitcher-add s 'b)
- (should (equal (rswitcher-current s) 'b))))
+ (should (equal (rswitcher-most-recent s) 'b))))
(ert-deftest rswitcher-test-switch-full ()
"simple scenario for switch-full"
(let ((s (rswitcher-make)))
(rswitcher-add s 'a)
(rswitcher-add s 'b)
- (should (equal (rswitcher-current s) 'b))
+ (should (equal (rswitcher-most-recent s) 'b))
(rswitcher-switch-full s)
- (should (equal (rswitcher-current s) 'a))
+ (should (equal (rswitcher-most-recent s) 'a))
(rswitcher-switch-full s)
- (should (equal (rswitcher-current s) 'b))
+ (should (equal (rswitcher-most-recent s) 'b))
(rswitcher-add s 'c)
- (should (equal (rswitcher-current s) 'c))
+ (should (equal (rswitcher-most-recent s) 'c))
(rswitcher-switch-full s)
- (should (equal (rswitcher-current s) 'b))
+ (should (equal (rswitcher-most-recent s) 'b))
(rswitcher-switch-full s)
- (should (equal (rswitcher-current s) 'c))
+ (should (equal (rswitcher-most-recent s) 'c))
(rswitcher-switch-full s)
- (should (equal (rswitcher-current s) 'b))))
+ (should (equal (rswitcher-most-recent s) 'b))))
(ert-deftest rswitcher-test-switch-partial ()
"simple scenario for switch-partial"
(let ((s (rswitcher-make)))
(rswitcher-add s 'a)
(rswitcher-add s 'b)
- (should (equal (rswitcher-current s) 'b))
+ (should (equal (rswitcher-most-recent s) 'b))
(rswitcher-switch-full s)
- (should (equal (rswitcher-current s) 'a))
+ (should (equal (rswitcher-most-recent s) 'a))
(rswitcher-switch-partial s)
- (should (equal (rswitcher-current s) 'b))
+ (should (equal (rswitcher-most-recent s) 'b))
(rswitcher-switch-partial s)
- (should (equal (rswitcher-current s) 'a))))
+ (should (equal (rswitcher-most-recent s) 'a))))
(ert-deftest rswitcher-test-mixing-partial-and-full-with-2-elts ()
"mixing use of partial and full"
(let ((s (rswitcher-make)))
(rswitcher-add s 'a)
(rswitcher-add s 'b)
- (should (equal 'b (rswitcher-current s)))
+ (should (equal 'b (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(rswitcher-switch-partial s)
- (should (equal 'b (rswitcher-current s)))
+ (should (equal 'b (rswitcher-most-recent s)))
(rswitcher-switch-partial s)
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(rswitcher-switch-partial s)
- (should (equal 'b (rswitcher-current s)))
+ (should (equal 'b (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'b (rswitcher-current s)))
+ (should (equal 'b (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'a (rswitcher-current s)))))
+ (should (equal 'a (rswitcher-most-recent s)))))
(ert-deftest rswitcher-test-mixing-partial-and-full ()
"mixing use of partial and full"
@@ -124,20 +124,20 @@
(rswitcher-add s 'a)
(rswitcher-add s 'b)
(rswitcher-add s 'c)
- (should (equal 'c (rswitcher-current s)))
+ (should (equal 'c (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'b (rswitcher-current s)))
+ (should (equal 'b (rswitcher-most-recent s)))
(rswitcher-switch-partial s)
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(rswitcher-switch-partial s)
- (should (equal 'c (rswitcher-current s)))
+ (should (equal 'c (rswitcher-most-recent s)))
(rswitcher-switch-partial s)
- (should (equal 'b (rswitcher-current s)))
+ (should (equal 'b (rswitcher-most-recent s)))
(rswitcher-switch-partial s)
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'c (rswitcher-current s)))
+ (should (equal 'c (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'a (rswitcher-current s)))
+ (should (equal 'a (rswitcher-most-recent s)))
(rswitcher-switch-full s)
- (should (equal 'c (rswitcher-current s)))))
+ (should (equal 'c (rswitcher-most-recent s)))))
View
63 rswitcher.el
@@ -40,37 +40,66 @@
(eval-when-compile (require 'cl))
(defun rswitcher-make ()
+ "Return a new switcher.
+The switcher is internally implemented as a cons. The `car' of
+this cons contains the list of elements in the rswitcher (see
+`rswitcher--elements') while its `cdr' contains the index of the
+most-recent element (see `rswitcher--last-pos')."
(cons nil nil))
(defun rswitcher--elements (switcher)
+ "Return the list of elements in SWITCHER.
+See `rswitcher-length', `rswitcher-empty-p', `rswitcher-memq',
+`rswitcher-add', and `rswitcher-delete-most-recent' for ways to query
+and manipulate SWITCHER."
(car switcher))
(defun rswitcher--last-pos (switcher)
+ "Return the index of SWITCHER's most-recent element.
+This function returns nil in all cases except after a
+`rswitcher-switch-partial' (and the function
+`rswitcher--reset-last-pos' will make this function return nil
+again). See `rswitcher--most-recent-pos' and `rswitcher--most-recent' for
+higher-level functions. Change the value returned by this
+function by using `rswitcher--reset-last-pos',
+`rswitcher--increment-last-pos', and `rswitcher--set-last-pos'."
(cdr switcher))
(defun rswitcher--set-last-pos (switcher last-pos)
+ "Set the last pos field of SWITCHER to be LAST-POS."
(setcdr switcher last-pos))
(defun rswitcher--increment-last-pos (switcher)
+ "Add 1 (modulo SWITCHER's number of elements) to LAST-POS field."
(let ((last-pos (rswitcher--last-pos switcher)))
(rswitcher--set-last-pos switcher
(% (1+ last-pos)
(rswitcher-length switcher)))))
(defun rswitcher--reset-last-pos (switcher)
+ "Set last pos field of SWITCHER to nil.
+See `rswitcher--last-pos'."
(rswitcher--set-last-pos switcher nil))
(defun rswitcher-length (switcher)
+ "Return the number of elements in SWITCHER.
+See `rswitcher--elements'."
(length (rswitcher--elements switcher)))
(defun rswitcher-empty-p (switcher)
+ "Check if there is no more elements in SWITCHER."
(zerop (rswitcher-length switcher)))
-(defun rswitcher--current-pos (switcher)
+(defun rswitcher--most-recent-pos (switcher)
+ "Return the index of the most recent element.
+The most recent element is always the first of SWITCHER's
+elements except after a call to `rswitcher-switch-partial' in
+which case the most recent element is pointed to by the last pos
+field of SWITCHER."
(or (rswitcher--last-pos switcher) 0))
-(defun rswitcher-current (switcher)
- (elt (rswitcher--elements switcher) (rswitcher--current-pos switcher)))
+(defun rswitcher-most-recent (switcher)
+ (elt (rswitcher--elements switcher) (rswitcher--most-recent-pos switcher)))
(defun rswitcher--push (switcher elt)
(setcar switcher (cons elt (car switcher))))
@@ -93,34 +122,34 @@
(let ((tail (nthcdr (1+ pos) (rswitcher--elements switcher)))
(head (nthcdr (1- pos) (rswitcher--elements switcher))))
(setcdr head tail)))
- (if (= pos (rswitcher--current-pos switcher))
- ;; we just removed the element that was current
+ (if (= pos (rswitcher--most-recent-pos switcher))
+ ;; we just removed the element that was most-recent
(rswitcher--reset-last-pos switcher))))
-(defun rswitcher-delete-current (switcher)
- (rswitcher--delete switcher (rswitcher--current-pos switcher)))
+(defun rswitcher-delete-most-recent (switcher)
+ (rswitcher--delete switcher (rswitcher--most-recent-pos switcher)))
-(defun rswitcher-make-current-elt-the-first (switcher)
- ;; put current element to the beginning of the list
- (unless (zerop (rswitcher--current-pos switcher))
+(defun rswitcher-make-most-recent-elt-the-first (switcher)
+ ;; put most-recent element to the beginning of the list
+ (unless (zerop (rswitcher--most-recent-pos switcher))
(rswitcher--push
switcher
- (rswitcher--delete switcher (rswitcher--current-pos switcher)))))
+ (rswitcher--delete switcher (rswitcher--most-recent-pos switcher)))))
(defun rswitcher-add (switcher elt)
- (rswitcher-make-current-elt-the-first switcher)
+ (rswitcher-make-most-recent-elt-the-first switcher)
(rswitcher--push switcher elt)
(rswitcher--reset-last-pos switcher))
(defun rswitcher--swap-first-two-elts (switcher)
- (let ((current (rswitcher--pop switcher))
+ (let ((most-recent (rswitcher--pop switcher))
(new (rswitcher--pop switcher)))
- (rswitcher--push switcher current)
+ (rswitcher--push switcher most-recent)
(rswitcher--push switcher new)))
(defun rswitcher-switch-full (switcher)
(when (>= (rswitcher-length switcher) 2)
- (rswitcher-make-current-elt-the-first switcher)
+ (rswitcher-make-most-recent-elt-the-first switcher)
(rswitcher--swap-first-two-elts switcher)
(rswitcher--reset-last-pos switcher)))
@@ -134,3 +163,7 @@
(if (> (rswitcher-length switcher) 2) 2 0)))))
(provide 'rswitcher)
+
+(provide 'rswitcher)
+
+;;; rswitcher.el ends here
View
4 shell-switcher.el
@@ -84,7 +84,7 @@ This function is to be used as value for
(defun sswitcher--most-recent ()
"Return the most recently accessed shell."
- (rswitcher-current sswitcher-ring))
+ (rswitcher-most-recent sswitcher-ring))
(defun sswitcher--most-recent-shell-valid-p ()
"Check that the most recently created shell can still be accessed."
@@ -94,7 +94,7 @@ This function is to be used as value for
"Remove all shell buffers until we find a valid one."
(while (and (not (rswitcher-empty-p sswitcher-ring))
(not (sswitcher--most-recent-shell-valid-p)))
- (rswitcher-delete-current sswitcher-ring)))
+ (rswitcher-delete-most-recent sswitcher-ring)))
(defun sswitcher--shell-exist-p ()
"Check that there is at least one valid shell to switch to."
Please sign in to comment.
Something went wrong with that request. Please try again.