clojure.kak (12905B)
1 # http://clojure.org 2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate .*[.](clj|cljc|cljs|cljx|edn|jank) %{ 8 set-option buffer filetype clojure 9 } 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 hook global WinSetOption filetype=clojure %{ 14 require-module clojure 15 clojure-configure-window 16 } 17 18 hook -group clojure-highlight global WinSetOption filetype=clojure %{ 19 add-highlighter window/clojure ref clojure 20 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/clojure } 21 } 22 23 hook -group clojure-insert global BufNewFile .*[.](clj|cljc|cljs|cljx) %{ 24 require-module clojure 25 clojure-insert-ns 26 } 27 28 provide-module clojure %{ 29 30 require-module lisp 31 32 # Highlighters 33 # ‾‾‾‾‾‾‾‾‾‾‾‾ 34 35 add-highlighter shared/clojure regions 36 add-highlighter shared/clojure/code default-region group 37 add-highlighter shared/clojure/comment region '(?<!\\)(?:\\\\)*\K;' '$' fill comment 38 add-highlighter shared/clojure/shebang region '(?<!\\)(?:\\\\)*\K#!' '$' fill comment 39 add-highlighter shared/clojure/string region '(?<!\\)(?:\\\\)*\K"' '(?<!\\)(?:\\\\)*"' fill string 40 41 add-highlighter shared/clojure/code/ regex \b(nil|true|false)\b 0:value 42 add-highlighter shared/clojure/code/ regex \ 43 \\(?:space|tab|newline|return|backspace|formfeed|u[0-9a-fA-F]{4}|o[0-3]?[0-7]{1,2}|.)\b 0:string 44 45 evaluate-commands %sh{ 46 exec awk -f - <<'EOF' 47 BEGIN{ 48 symbol_char="[^\\s()\\[\\]{}\"\\;@^`~\\\\%/]"; 49 in_core="(clojure\\.core/|(?<!/))"; 50 split( \ 51 "case cond condp cond-> cond->> def definline definterface defmacro defmethod "\ 52 "defmulti defn defn- defonce defprotocol defrecord defstruct deftype fn if "\ 53 "if-let if-not if-some let letfn new ns when when-first when-let when-not "\ 54 "when-some . ..", keywords); 55 56 split( \ 57 "* *' + +' - -' -> ->> ->ArrayChunk ->Eduction ->Vec ->VecNode ->VecSeq / < "\ 58 "<= = == > >= abs accessor aclone "\ 59 "add-classpath add-tap add-watch agent agent-error agent-errors aget alength alias "\ 60 "all-ns alter alter-meta! alter-var-root amap ancestors and any? apply "\ 61 "areduce array-map as-> aset aset-boolean aset-byte aset-char aset-double "\ 62 "aset-float aset-int aset-long aset-short assert assoc assoc! assoc-in "\ 63 "associative? atom await await-for bases bean bigdec bigint biginteger "\ 64 "binding bit-and bit-and-not bit-clear bit-flip bit-not bit-or bit-set "\ 65 "bit-shift-left bit-shift-right bit-test bit-xor boolean boolean-array "\ 66 "boolean? booleans bound-fn bound-fn* bound? bounded-count butlast byte "\ 67 "byte-array bytes bytes? cast cat catch char char-array char-escape-string "\ 68 "char-name-string char? chars class class? clear-agent-errors "\ 69 "clojure-version coll? comment commute comp comparator compare "\ 70 "compare-and-set! compile complement completing concat conj conj! cons "\ 71 "constantly construct-proxy contains? count counted? create-ns "\ 72 "create-struct cycle dec dec' decimal? declare dedupe default-data-readers "\ 73 "delay delay? deliver denominator deref derive descendants disj disj! "\ 74 "dissoc dissoc! distinct distinct? do doall dorun doseq dosync dotimes doto "\ 75 "double double-array double? doubles drop drop-last drop-while eduction "\ 76 "empty empty? ensure ensure-reduced enumeration-seq error-handler "\ 77 "error-mode eval even? every-pred every? ex-cause ex-data ex-info ex-message extend "\ 78 "extend-protocol extend-type extenders extends? false? ffirst file-seq "\ 79 "filter filterv finally find find-keyword find-ns find-var first flatten "\ 80 "float float-array float? floats flush fn? fnext fnil for force format "\ 81 "frequencies future future-call future-cancel future-cancelled? "\ 82 "future-done? future? gen-class gen-interface gensym get get-in get-method "\ 83 "get-proxy-class get-thread-bindings get-validator group-by halt-when hash "\ 84 "hash-map hash-ordered-coll hash-set hash-unordered-coll ident? identical? "\ 85 "identity ifn? import in-ns inc inc' indexed? infinite? init-proxy inst-ms inst? "\ 86 "instance? int int-array int? integer? interleave intern interpose into "\ 87 "into-array ints io! isa? iterate iteration iterator-seq juxt keep keep-indexed key "\ 88 "keys keyword keyword? last lazy-cat lazy-seq line-seq list list* list? "\ 89 "load load-file load-reader load-string loaded-libs locking long long-array "\ 90 "longs loop macroexpand macroexpand-1 make-array make-hierarchy map "\ 91 "map-entry? map-indexed map? mapcat mapv max max-key memfn memoize merge "\ 92 "merge-with meta methods min min-key mix-collection-hash mod monitor-enter "\ 93 "monitor-exit name namespace namespace-munge NaN? nat-int? neg-int? neg? newline "\ 94 "next nfirst nil? nnext not not-any? not-empty not-every? not= ns-aliases "\ 95 "ns-imports ns-interns ns-map ns-name ns-publics ns-refers ns-resolve "\ 96 "ns-unalias ns-unmap nth nthnext nthrest num number? numerator object-array "\ 97 "odd? or parents parse-boolean parse-double parse-long parse-uuid partial "\ 98 "partition partition-all partition-by partitionv partitionv-all pcalls peek "\ 99 "persistent! pmap pop pop! pop-thread-bindings pos-int? pos? pr pr-str "\ 100 "prefer-method prefers print print-str printf println println-str PrintWriter-on prn "\ 101 "prn-str promise proxy proxy-mappings proxy-super push-thread-bindings "\ 102 "pvalues qualified-ident? qualified-keyword? qualified-symbol? quot quote "\ 103 "rand rand-int rand-nth random-sample random-uuid range ratio? rational? rationalize "\ 104 "re-find re-groups re-matcher re-matches re-pattern re-seq read read+string read-line "\ 105 "read-string reader-conditional reader-conditional? realized? record? recur "\ 106 "reduce reduce-kv reduced reduced? reductions ref ref-history-count "\ 107 "ref-max-history ref-min-history ref-set refer refer-clojure reify "\ 108 "release-pending-sends rem remove remove-all-methods remove-method "\ 109 "remove-ns remove-watch repeat repeatedly replace replicate require requiring-resolve reset! "\ 110 "reset-meta! reset-vals! resolve rest restart-agent resultset-seq reverse "\ 111 "reversible? rseq rsubseq run! satisfies? second select-keys send send-off "\ 112 "send-via seq seq-to-map-for-destructuring seq? seqable? seque sequence sequential? set set! "\ 113 "set-agent-send-executor! set-agent-send-off-executor! set-error-handler! "\ 114 "set-error-mode! set-validator! set? short short-array shorts shuffle "\ 115 "shutdown-agents simple-ident? simple-keyword? simple-symbol? slurp some "\ 116 "some-> some->> some-fn some? sort sort-by sorted-map sorted-map-by "\ 117 "sorted-set sorted-set-by sorted? special-symbol? spit split-at split-with splitv-at "\ 118 "StackTraceElement->vec str stream-into! stream-reduce! stream-seq! stream-transduce! "\ 119 "string? struct struct-map subs subseq subvec supers swap! swap-vals! "\ 120 "symbol symbol? sync tagged-literal tagged-literal? take take-last take-nth "\ 121 "take-while tap> test the-ns thread-bound? throw Throwable->map time to-array to-array-2d "\ 122 "trampoline transduce transient tree-seq true? try type unchecked-add "\ 123 "unchecked-add-int unchecked-byte unchecked-char unchecked-dec "\ 124 "unchecked-dec-int unchecked-divide-int unchecked-double unchecked-float "\ 125 "unchecked-inc unchecked-inc-int unchecked-int unchecked-long "\ 126 "unchecked-multiply unchecked-multiply-int unchecked-negate "\ 127 "unchecked-negate-int unchecked-remainder-int unchecked-short "\ 128 "unchecked-subtract unchecked-subtract-int underive unreduced "\ 129 "unsigned-bit-shift-right update update-in update-keys update-proxy update-vals uri? use uuid? val "\ 130 "vals var var-get var-set var? vary-meta vec vector vector-of vector? "\ 131 "volatile! volatile? vreset! vswap! while with-bindings with-bindings* "\ 132 "with-in-str with-local-vars with-meta with-open with-out-str "\ 133 "with-precision with-redefs with-redefs-fn xml-seq zero? zipmap", core_fns); 134 135 split( \ 136 "*1 *2 *3 *agent* *assert* *clojure-version* *command-line-args* *compile-files* "\ 137 "*compile-path* *compiler-options* *data-readers* *default-data-reader-fn* "\ 138 "*e *err* *file* *flush-on-newline* *in* *ns* *out* *print-dup* "\ 139 "*print-length* *print-level* *print-meta* *print-namespace-maps* "\ 140 "*print-readably* *read-eval* *repl* *unchecked-math* *warn-on-reflection*", core_vars); 141 } 142 function print_word_highlighter(words, face, first) { 143 printf("add-highlighter shared/clojure/code/ regex (?<!%s)%s(", \ 144 symbol_char, in_core); 145 first = 1; 146 for (i in words) { 147 if (!first) { printf("|"); } 148 printf("\\Q%s\\E", words[i]); 149 first = 0; 150 } 151 printf(")(?!%s) 0:%s\n", symbol_char, face); 152 } 153 function print_static_words(words) { 154 for (i in words) { 155 printf("%s clojure.core/%s ", words[i], words[i]); 156 } 157 } 158 BEGIN{ 159 # Keywords 160 printf("add-highlighter shared/clojure/code/ regex ::?(%s+/)?%s+ 0:value\n", symbol_char, symbol_char); 161 162 # Numbers 163 printf("add-highlighter shared/clojure/code/ regex (?<!%s)[-+]?(?:0(?:[xX][0-9a-fA-F]+|[0-7]*)|[1-9]\\d*)N? 0:value\n", symbol_char); 164 printf("add-highlighter shared/clojure/code/ regex (?<!%s)[-+]?(?:0|[1-9]\\d*)(?:\\.\\d*)(?:M|[eE][-+]?\\d+)? 0:value\n", symbol_char); 165 printf("add-highlighter shared/clojure/code/ regex (?<!%s)[-+]?(?:0|[1-9]\\d*)/(?:0|[1-9]\\d*) 0:value\n", symbol_char); 166 167 print_word_highlighter(keywords, "keyword"); 168 print_word_highlighter(core_fns, "function"); 169 print_word_highlighter(core_vars, "variable"); 170 171 printf("declare-option str-list clojure_static_words ") 172 print_static_words(keywords); 173 print_static_words(core_fns); 174 print_static_words(core_vars); 175 printf("\n"); 176 } 177 EOF 178 } 179 180 # Commands 181 # ‾‾‾‾‾‾‾‾ 182 183 define-command -hidden clojure-configure-window %{ 184 set-option window static_words %opt{clojure_static_words} 185 186 hook window ModeChange pop:insert:.* -group clojure-trim-indent clojure-trim-indent 187 hook window InsertChar \n -group clojure-indent clojure-indent-on-new-line 188 189 set-option buffer extra_word_chars '_' . / * ? + - < > ! : "'" 190 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window clojure-.+ } 191 } 192 193 define-command -hidden clojure-trim-indent lisp-trim-indent 194 195 declare-option \ 196 -docstring 'regex matching the head of forms which have options *and* indented bodies' \ 197 regex clojure_special_indent_forms \ 198 '(?:def.*|doseq|for|fn\*?|if(-.*|)|let.*|loop|ns|testing|with-.*|when(-.*|))' 199 200 define-command -hidden clojure-indent-on-new-line %{ 201 # registers: i = best align point so far; w = start of first word of form 202 evaluate-commands -draft -save-regs '/"|^@iw' -itersel %{ 203 execute-keys -draft 'gk"iZ' 204 try %{ 205 execute-keys -draft '[bl"i<a-Z><gt>"wZ' 206 207 try %{ 208 # If a special form, indent another (indentwidth - 1) spaces 209 execute-keys -draft '"wze<a-K>[\s()\[\]\{\}]<ret><a-k>\A' %opt{clojure_special_indent_forms} '\z<ret>' 210 execute-keys -draft '"wze<a-L>s.{' %sh{printf $(( kak_opt_indentwidth - 1 ))} '}\K.*<ret><a-;>;"i<a-Z><gt>' 211 } catch %{ 212 # If not special and parameter appears on line 1, indent to parameter 213 execute-keys -draft '"wz<a-K>[()[\]{}]<ret>e<a-K>[\s()\[\]\{\}]<ret><a-l>s\h\K[^\s].*<ret><a-;>;"i<a-Z><gt>' 214 } 215 } 216 try %{ execute-keys -draft '[rl"i<a-Z><gt>' } 217 try %{ execute-keys -draft '[Bl"i<a-Z><gt>' } 218 execute-keys -draft ';"i<a-z>a&,' 219 } 220 } 221 222 declare-option -docstring %{ 223 top-level directories which can contain clojure files 224 e.g. '(src|test|dev)' 225 } regex clojure_source_directories '(src|test|dev)' 226 227 define-command -docstring %{clojure-insert-ns: Insert namespace directive at top of Clojure source file} \ 228 clojure-insert-ns %{ 229 evaluate-commands -draft %{ 230 execute-keys -save-regs '' 'gk\O' "%val{bufname}" '<esc>giZ' 231 try %{ execute-keys 'z<a-l>s\.clj[csx]?$<ret><a-d>' } 232 try %{ execute-keys 'z<a-l>s^' "%opt{clojure_source_directories}" '/<ret><a-d>' } 233 try %{ execute-keys 'z<a-l>s/<ret>r.' } 234 try %{ execute-keys 'z<a-l>s_<ret>r-' } 235 execute-keys 'z<a-l>\c(ns <c-r>")<ret><esc>' 236 } 237 } 238 239 }