|
| 1 | +#!/usr/bin/env lein-exec |
| 2 | +;; |
| 3 | +;; Vim keyword syntax generator for Clojure. |
| 4 | +;; Functionality kept in a monolithic function for easy copy and paste. |
| 5 | +;; |
| 6 | +;; Copyright (c) 2012 guns <self@sungpae.com> |
| 7 | +;; Distributed under the MIT license. |
| 8 | +;; http://www.opensource.org/licenses/mit-license.php |
| 9 | + |
| 10 | +(defn vim-syntax-keywords |
| 11 | + "Return Vimscript literal syntax keyword definitions. Special forms, |
| 12 | + constants, and every public var in clojure.core will be included." |
| 13 | + [] |
| 14 | + (let [builtins [["Constant" '[nil]] |
| 15 | + ["Boolean" '[true false]] |
| 16 | + ;; http://clojure.org/special_forms |
| 17 | + ["Special" '[def if do let quote var fn loop recur throw try |
| 18 | + catch finally monitor-enter monitor-exit . new set!]] |
| 19 | + ;; The duplicates from Special are intentional |
| 20 | + ["Exception" '[try catch finally throw]] |
| 21 | + ["Cond" '[if if-not if-let when when-> when->> when-not |
| 22 | + when-let when-first cond condp case]] |
| 23 | + ["Repeat" '[map mapv mapcat reduce reduce-kv filter filterv |
| 24 | + for doall dorun doseq dotimes map-indexed keep |
| 25 | + keep-indexed while]]] |
| 26 | + declared (atom (set (filter symbol? (mapcat peek builtins)))) |
| 27 | + coresyms (keys (ns-publics 'clojure.core)) |
| 28 | + select! (fn [pred] |
| 29 | + (let [xs (clojure.set/difference (set (filter pred coresyms)) @declared)] |
| 30 | + (swap! declared into xs) |
| 31 | + xs)) |
| 32 | + builtins (conj builtins |
| 33 | + ;; Clojure devs are fastidious about accurate metadata |
| 34 | + ["Define" (select! #(re-seq #"\Adef(?!ault)" (str %)))] |
| 35 | + ["Macro" (select! #(:macro (meta (resolve %))))] |
| 36 | + ["Func" (select! #(:arglists (meta (resolve %))))] |
| 37 | + ["Variable" (select! identity)]) |
| 38 | + names (fn [coll] |
| 39 | + (reduce (fn [v x] |
| 40 | + ;; Include fully qualified versions of core vars |
| 41 | + (cond (symbol? x) (if-let [m (meta (resolve x))] |
| 42 | + (conj v (str (:name m)) (str (:ns m) \/ (:name m))) |
| 43 | + (conj v (str x))) |
| 44 | + (nil? x) (conj v "nil") |
| 45 | + :else (conj v (str x)))) |
| 46 | + [] coll)) |
| 47 | + definitions (map (fn [[group keywords]] |
| 48 | + (str "syntax keyword clojure" group \space |
| 49 | + (clojure.string/join \space (sort (names keywords))))) |
| 50 | + builtins)] |
| 51 | + (clojure.string/join \newline definitions))) |
| 52 | + |
| 53 | +(println (str "\" Clojure " (clojure-version) \newline |
| 54 | + (vim-syntax-keywords))) |
0 commit comments