Skip to content

Instantly share code, notes, and snippets.

@staab
Created December 3, 2019 15:28
Show Gist options
  • Select an option

  • Save staab/0f09c55e3d9fdbc7647d36e347d9dd5b to your computer and use it in GitHub Desktop.

Select an option

Save staab/0f09c55e3d9fdbc7647d36e347d9dd5b to your computer and use it in GitHub Desktop.

Revisions

  1. Jon Staab created this gist Dec 3, 2019.
    131 changes: 131 additions & 0 deletions janet.kak
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,131 @@
    # http://janet-lang.org
    # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

    # require lisp.kak

    # Detection
    # ‾‾‾‾‾‾‾‾‾

    hook global BufCreate .*[.](janet) %{
    set-option buffer filetype janet
    }

    # Highlighters
    # ‾‾‾‾‾‾‾‾‾‾‾‾

    add-highlighter shared/janet regions
    add-highlighter shared/janet/code default-region group
    add-highlighter shared/janet/comment region '(?<!\\)(?:\\\\)*\K#' '$' fill comment
    add-highlighter shared/janet/string region '(?<!\\)(?:\\\\)*\K"' '(?<!\\)(?:\\\\)*"' fill string

    add-highlighter shared/janet/code/ regex \b(nil|true|false)\b 0:value
    add-highlighter shared/janet/code/ regex \
    \\(?:space|tab|newline|return|backspace|formfeed|u[0-9a-fA-F]{4}|o[0-3]?[0-7]{1,2}|.)\b 0:string

    hook global WinSetOption filetype=janet %{
    set-option window extra_word_chars . / * ? + - < > ! : "'"
    }

    evaluate-commands %sh{
    symbol_char='[^\s()\[\]{}"\;@^`~\\%/]'
    modules="
    array buffer debug fiber file int janet math module os parser peg string
    table tarray tuple"
    keywords="
    * *= + ++ += - -- -= -> ->> -?> -?>> < <= = == > >= abstract? all all-bindings
    all-dynamics and apply array array? as-> as?-> asm bad-compile bad-parse band
    blshift bnot boolean? bor brshift brushift buffer buffer? bxor bytes? case cfunction?
    cli-main comment comp compile complement cond coro count debug dec deep-not= deep=
    def def- default defglobal defmacro defmacro- defn defn- describe dictionary?
    disasm distinct doc doc* doc-format dofile drop drop-until drop-while dyn
    each empty? env-lookup eprin eprint eprintf error eval eval-string even? every?
    extreme false? fiber? filter find find-index first flatten flatten-into for freeze
    frequencies function? gccollect gcinterval gcsetinterval generate gensym get
    get-in getline hash idempotent? identity if-let if-not import import* in
    inc indexed? int? interleave interpose invert juxt juxt* keep keys keyword
    keyword? kvs last length let load-image loop macex macex1 make-env make-image
    map mapcat marshal match max max-order mean merge merge-into min min-order nat?
    native neg? next nil? not not= not== number? odd? one? or order< order<= order>
    order>= pairs partial partition pos? postwalk pp prewalk prin print printf product
    propagate put put-in quit range reduce repl require resume reverse run-context
    scan-number seq setdyn short-fn slice slurp some sort sorted spit stderr stdin
    stdout string string? struct struct? sum symbol symbol? table table? take
    take-until take-while trace true? try tuple tuple? type unless unmarshal untrace
    update update-in use values var varfn varglobal walk when when-let with with-dyns
    with-syms with-vars yield zero? zipcoll defmulti defmethod if while fn"
    join() { sep=$2; set -- $1; IFS="$sep"; echo "$*"; }
    keywords() {
    words="$1"
    type="$2"
    words="$(echo "$words" |sed -e 's/[+?*\.]/\\&/g')"
    printf 'add-highlighter shared/janet/code/ regex (?<!%s)(%s)(?!%s) 0:%s\n' \
    "${symbol_char}" \
    "$(join "${words}" '|')" \
    "${symbol_char}" \
    "${type}"
    }
    static_words="$(join "$keywords" ' ')"
    printf %s "
    # Keywords
    add-highlighter shared/janet/code/ regex ::?(${symbol_char}+/)?${symbol_char}+ 0:value
    # Numbers
    add-highlighter shared/janet/code/ regex (?<!${symbol_char})[-+]?(?:0(?:[xX][0-9a-fA-F]+|[0-7]*)|[0-9]+)N? 0:value
    add-highlighter shared/janet/code/ regex (?<!${symbol_char})[-+]?(?:0|[0-9]\d*)(?:\.\d*)(?:M|[eE][-+]?\d+)? 0:value
    add-highlighter shared/janet/code/ regex (?<!${symbol_char})[-+]?(?:0|[0-9]\d*)/(?:0|[0-9]\d*) 0:value
    $(keywords "${keywords}" keyword)
    hook global WinSetOption filetype=janet %{
    set-option window static_words $static_words
    }
    "
    }

    # Commands
    # ‾‾‾‾‾‾‾‾

    define-command -hidden janet-filter-around-selections lisp-filter-around-selections

    declare-option \
    -docstring 'regex matching the head of forms which have options *and* indented bodies' \
    regex janet_special_indent_forms \
    '(?:def.*|doseq|for|fn\*?|if(-.*|)|let.*|loop|ns|testing|with-.*|when(-.*|))'

    define-command -hidden janet-indent-on-new-line %{
    # registers: i = best align point so far; w = start of first word of form
    evaluate-commands -draft -save-regs '/"|^@iw' -itersel %{
    execute-keys -draft 'gk"iZ'
    try %{
    execute-keys -draft '[bl"i<a-Z><gt>"wZ'

    try %{
    # If a special form, indent another space
    execute-keys -draft '"wze<a-k>\A' %opt{janet_special_indent_forms} '\z<ret><a-L>s.\K.*<ret><a-;>;"i<a-Z><gt>'
    } catch %{
    # If not special and parameter appears on line 1, indent to parameter
    execute-keys -draft '"wze<a-l>s\h\K[^\s].*<ret><a-;>;"i<a-Z><gt>'
    }
    }
    try %{ execute-keys -draft '[rl"i<a-Z><gt>' }
    try %{ execute-keys -draft '[Bl"i<a-Z><gt>' }
    execute-keys -draft '"i<a-z>a&<space>'
    }
    }

    # Initialization
    # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
    hook -group janet-highlight global WinSetOption filetype=janet %{ add-highlighter window/janet ref janet }

    hook global WinSetOption filetype=janet %[
    hook window ModeChange insert:.* -group janet-hooks janet-filter-around-selections
    hook window InsertChar \n -group janet-indent janet-indent-on-new-line
    ]

    hook -group janet-highlight global WinSetOption filetype=(?!janet).* %{ remove-highlighter window/janet }

    hook global WinSetOption filetype=(?!janet).* %{
    remove-hooks window janet-.+
    }