                  :ws       ""
                  :wsopt    ""))))
 
-(defn nmlxform [tree sets]
+(defn nml-tree [fname]
+  (try (parse (slurp fname))
+       (catch Exception e (fail "Could not open namelist file '" fname "'."))))
+
+(defn nml-xform [tree sets]
   (loop [t tree s sets]
     (if (empty? s)
       t
       (let [[nml key val] (first s)]
-        (recur (nmlset t nml key val) (rest s))))))
-
-(defn nmltree [fname]
-  (try (parse (slurp fname))
-       (catch Exception e (fail "Could not open namelist file '" fname "'."))))
+        (recur (nml-set t nml key val) (rest s))))))
 
 ;; cli
 

  (:require [clojure.tools.cli :as cli   ])
  (:gen-class))

(declare nmlget nmlname nmlset nmlstr)

; defs


  (if msg (println (apply str msg)))
  (System/exit 1))

(defn nml-get [tree nml key]
  (let [stmt     (last (filter #(= (nml-name %) nml) (rest tree)))
        nvsubseq (last (filter #(= (nml-name %) key) (rest (last stmt))))
        values   (last nvsubseq)
        value    (if (nil? values) "" (nml-str values))]
    (println (str nml ":" key "=" value))
    tree))

(defn nml-name [x]
  (nml-str (second x)))

(defn nml-set [tree nml key val & sub]
  (let [child (if sub :nvsubseq :stmt)
        match (if sub key nml)
        vnew  (if sub (fn [tree] (parse val :start :values)) #(nml-set % nml key val true))
        f     (fn [k v] [child k (if (= (nml-str k) match) (vnew v) v)])]
    (insta/transform {child f} tree)))

(defn nml-str [x]
  (let [k (first x)
        v (rest  x)
        cjoin    #(string/join "," %)
        delegate #(map nml-str %)
        ds       (fn [v] (delegate (sort-by #(nml-name %) v)))
        list2str #(apply str (map nml-str %))
        sf       #(nml-str (first %))
        sl       #(nml-str (last %))]
    (if debug (println (str "k=" k " v=" v)))
    (apply str (case k
                 :s        (ds v)
