branch: externals/denote-sequence
commit 7040e4cb6706c7a3023b1712ac94f83b448daffe
Author: Protesilaos Stavrou <i...@protesilaos.com>
Commit: Protesilaos Stavrou <i...@protesilaos.com>

    Define function to infer a sibling and write a test for it as well
---
 denote-sequence.el            | 17 +++++++++++++++++
 tests/denote-sequence-test.el | 14 ++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/denote-sequence.el b/denote-sequence.el
index dadb169243..20ce224d6b 100644
--- a/denote-sequence.el
+++ b/denote-sequence.el
@@ -393,6 +393,23 @@ depth given SEQUENCE."
                            (t (error "Unknown type of sequence for `%s'" 
last-component)))))
     (denote-sequence-join (list sequence new-depth) scheme)))
 
+(defun denote-sequence--infer-sibling (sequence direction)
+  "Get sibling of SEQUENCE in DIRECTION `next' or `previous'.
+Do not actually try to create a new sibling nor to test for the
+existence of one.  Simply do the work of finding the next or previous
+sibling in the sequence."
+  (pcase-let* ((`(,sequence . ,scheme) (denote-sequence-and-scheme-p sequence))
+               (components (denote-sequence-split sequence))
+               (butlast (butlast components))
+               (last-component (car (nreverse components)))
+               (direction-fn (pcase direction
+                               ('next #'denote-sequence-increment-partial)
+                               ('previous #'denote-sequence-decrement-partial)
+                               (_ (error "Unknown DIRECTION `%s'" direction))))
+               (new-number (funcall direction-fn last-component)))
+    (when new-number
+      (denote-sequence-join (append butlast (list new-number)) scheme))))
+
 (defun denote-sequence--get-files (&optional files)
   "Return list of files or buffers in the variable `denote-directory'.
 With optional FILES only consider those, otherwise use 
`denote-directory-files'."
diff --git a/tests/denote-sequence-test.el b/tests/denote-sequence-test.el
index de5bb2c3a5..29abcd78df 100644
--- a/tests/denote-sequence-test.el
+++ b/tests/denote-sequence-test.el
@@ -245,6 +245,20 @@ function `denote-sequence-get-relative'."
   (should (string= (denote-sequence-decrement-partial "bbcza") "bbcz"))
   (should-error (denote-sequence-decrement-partial "1a")))
 
+(ert-deftest dst-denote-sequence--infer-sibling ()
+  "Test that `denote-sequence--infer-sibling' returns the correct result."
+  (should (string= (denote-sequence--infer-sibling "1" 'next) "2"))
+  (should (string= (denote-sequence--infer-sibling "1a" 'next) "1b"))
+  (should (string= (denote-sequence--infer-sibling "1z" 'next) "1za"))
+  (should (string= (denote-sequence--infer-sibling "1=1" 'next) "1=2"))
+  (should (string= (denote-sequence--infer-sibling "2" 'previous) "1"))
+  (should (string= (denote-sequence--infer-sibling "1b" 'previous) "1a"))
+  (should (string= (denote-sequence--infer-sibling "1za" 'previous) "1z"))
+  (should (string= (denote-sequence--infer-sibling "1=2" 'previous) "1=1"))
+  (should (null (denote-sequence--infer-sibling "1" 'previous)))
+  (should (null (denote-sequence--infer-sibling "1=1" 'previous)))
+  (should (null (denote-sequence--infer-sibling "1a" 'previous))))
+
 (provide 'denote-sequence-test)
 ;;; denote-sequence-test.el ends here
 

Reply via email to