better zsh completion
3 files changed, 49 insertions(+), 7 deletions(-)

M jd/trees.scm
M jd/zsh.scm
M johnny.scm
M jd/trees.scm +36 -1
@@ 4,7 4,8 @@ 
   (use jd.structures)
   (export node? make-node node-label node-children leaf?
           map-tree fold-tree
-          jd:insert-in-tree jd:fuse-trees jd:print-tree))
+          jd:insert-in-tree jd:fuse-trees jd:print-tree
+          jd:fetch-additional-info-from-second-tree))
  
 (select-module jd.trees)
 

          
@@ 143,6 144,40 @@ 
                         label1))]
       (make-node new-label new-children))))
 
+(define jd:fetch-info-from-second-node-list
+  (lambda [list1 list2]
+      (cond
+        [(null? list1) list1]
+        [(null? list2) list1]
+        [else
+          (let* [(head1 (car list1))
+                 (head2 (car list2))
+                 (label1 (node-label head1))
+                 (label2 (node-label head2))
+                 (num1 (jd:record-number label1))
+                 (num2 (jd:record-number label2))]
+            (case (compare num1 num2)
+              ((-1) (cons head1 (jd:fetch-info-from-second-node-list 
+                                  (cdr list1) list2)))
+              ((1)  (jd:fetch-info-from-second-node-list list1 (cdr list2)))
+              ((0)  (cons (jd:fetch-additional-info-from-second-tree
+                            head1 head2)
+                          (jd:fetch-info-from-second-node-list 
+                            (cdr list1) (cdr list2))))))])))
+
+(define jd:fetch-additional-info-from-second-tree
+  (lambda [tree1 tree2]
+    (let* ([label1    (node-label tree1)]
+           [label2    (node-label tree2)]
+           [children1 (node-children tree1)]
+           [children2 (node-children tree2)]
+           [new-label (if (and (jd:record? label1) (jd:record? label2)) 
+                              (jd:record-append label2 label1) label1)]
+           [new-children (jd:fetch-info-from-second-node-list
+                           children1 children2)])
+      (make-node new-label new-children))))
+      
+
 (define -node-numspec (lambda (node) (jd:record-number (label node))))
 (define jd:get-closest
   ; gives a list (pointer) the closest to the given numspec.

          
M jd/zsh.scm +9 -2
@@ 1,7 1,10 @@ 
 (define-module jd.zsh
   (use jd.trees)
   (use jd.structures)
-  (export jd:generate-zsh-completions))
+  (use jd.filesystem)
+  (use jd.meta)
+  (export jd:generate-zsh-completions
+          jd:full-info-tree))
 
 (select-module jd.zsh)
 

          
@@ 10,12 13,16 @@ 
   (lambda [numspec]
     (string-join (map (pa$ format "~2,,,'0,,@s") numspec) ".")))
 
+(define jd:full-info-tree
+  (cut jd:fetch-additional-info-from-second-tree 
+       (jd:file-tree) (jd:meta-tree)))
+
 (define jd:generate-zsh-completions
   (lambda [jd-file-tree]
     (fold-tree 
       (lambda [fst ls] 
         (let1 purged (filter (complement (pa$ equal? "")) (cons fst ls))
-          (string-join purged " ")))
+          (string-join purged "\n")))
       (lambda [_ record]
         (if (or ($ not $ jd:record? record) 
                 ($ not $ any (pa$ eq? 'directory) 

          
M johnny.scm +4 -4
@@ 1,11 1,11 @@ 
-#!/bin/env -S gauche -I.
+#!/bin/env gauche
 ;;;; johnny.scm
 ;;;; Module to deal with Johnny.Decimal related operations.
 ;;;; Copyright xaltsc 2021
 
-(use gauche.process)
+(add-load-path "." :relative)
+
 (use jd.zsh)
-(use jd.filesystem)
 
 
 ;(define jd:full-tree

          
@@ 20,5 20,5 @@ 
     (let1 argv (cdr args)
       (ecase ($ string->symbol $ car argv)
         [(--complete)
-         (format (current-output-port) "~a" (jd:generate-zsh-completions (jd:file-tree)))
+         (format (current-output-port) "~a" (jd:generate-zsh-completions (jd:full-info-tree)))
          0]))))