Make denote-sequence-convert also convert complete sequences

This commit is contained in:
Protesilaos Stavrou 2025-01-12 10:57:42 +02:00
parent 26f43c8aec
commit a717f46eee
No known key found for this signature in database
GPG key ID: 99BD6459CD5CA3EA
2 changed files with 50 additions and 4 deletions

View file

@ -225,16 +225,55 @@ has the same meaning as in `denote-sequence-and-scheme-p'."
(concat prefix suffix)
(make-string times ?z)))))))
(defun denote-sequence-convert (string)
(defun denote-sequence--alpha-to-number-complete (sequence)
"Like `denote-sequence--alpha-to-number' but for the complete SEQUENCE."
(if (denote-sequence-numeric-p sequence)
sequence
(let* ((parts (denote-sequence-split sequence))
(converted-parts (mapcar
(lambda (string)
(if (denote-sequence--numeric-partial-p string)
string
(denote-sequence--alpha-to-number string)))
parts)))
(denote-sequence-join converted-parts 'numeric))))
(defun denote-sequence--number-to-alpha-complete (sequence)
"Like `denote-sequence--number-to-alpha' but for the complete SEQUENCE."
(if (denote-sequence-alphanumeric-p sequence)
sequence
(let* ((parts (denote-sequence-split sequence))
(odd-is-numeric 0)
(converted-parts (mapcar
(lambda (string)
(setq odd-is-numeric (+ odd-is-numeric 1))
(cond
((= (% odd-is-numeric 2) 1)
string)
((denote-sequence--alphanumeric-partial-p string)
string)
(t
(denote-sequence--number-to-alpha string))))
parts)))
(denote-sequence-join converted-parts 'alphanumeric))))
(defun denote-sequence-convert (string &optional string-is-sequence)
"Convert STRING to its counterpart sequencing scheme.
Also see `denote-sequence-scheme'."
If STRING-IS-SEQUENCE then assume STRING to be a complete sequence, in
which case convert the entirety of it. Also see `denote-sequence-scheme'."
(cond
((and string-is-sequence (denote-sequence-alphanumeric-p string))
(denote-sequence--alpha-to-number-complete string))
((and string-is-sequence (denote-sequence-numeric-p string))
(denote-sequence--number-to-alpha-complete string))
((denote-sequence--alphanumeric-partial-p string)
(denote-sequence--alpha-to-number string))
((denote-sequence--numeric-partial-p string)
(denote-sequence--number-to-alpha string))
(t
(error "The `%s' must not contain both numbers and letters" string))))
(if string-is-sequence
(error "String `%s' did not pass `denote-sequence-p'" string)
(error "The `%s' must not contain both numbers and letters" string)))))
(defun denote-sequence-increment (string)
"Increment number represented by STRING and return it as a string.

View file

@ -752,7 +752,14 @@ This is done using the alphanumeric `denote-sequence-scheme'."
(string= (denote-sequence-convert "za") "27")
(string= (denote-sequence-convert "zzzzz") "130")
(string= (denote-sequence-convert "zzzzza") "131")))
(should-error (denote-sequence-convert "1a2")))
(should (and (string= (denote-sequence-convert "1=1=2" :string-is-sequence) "1a2")
(string= (denote-sequence-convert "1a2" :string-is-sequence) "1=1=2")
(string= (denote-sequence-convert "1=27=2=55" :string-is-sequence) "1za2zzc")
(string= (denote-sequence-convert "1za2zzc" :string-is-sequence) "1=27=2=55")
(string= (denote-sequence-convert "1=1=2=2=4=1" :string-is-sequence) "1a2b4a")
(string= (denote-sequence-convert "1a2b4a" :string-is-sequence) "1=1=2=2=4=1")))
(should-error (denote-sequence-convert "111=a" :string-is-sequence))
(should-error (denote-sequence-convert "a1" :string-is-sequence)))
(ert-deftest dt-denote-sequence-increment ()
"Test that `denote-sequence-increment' works with numbers and letters."