kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

scheme.kak (8794B)


      1 # http://www.scheme-reports.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate (.*/)?(.*\.(scm|ss|sld|sps|sls)) %{
      8     set-option buffer filetype scheme
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=scheme %{
     15     require-module scheme
     16 
     17     set-option window static_words %opt{scheme_static_words}
     18 
     19     set-option buffer extra_word_chars '!' '$' '%' '&' '*' '+' '-' '.' '/' ':' '<' '=' '>' '?' '@' '^' '_' '~'
     20     hook window ModeChange pop:insert:.* -group scheme-trim-indent lisp-trim-indent
     21     hook window InsertChar \n -group scheme-indent lisp-indent-on-new-line
     22 
     23     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window scheme-.+ }
     24 }
     25 
     26 hook -group scheme-highlight global WinSetOption filetype=scheme %{
     27     add-highlighter window/scheme ref scheme
     28     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/scheme }
     29 }
     30 
     31 provide-module scheme %{
     32 
     33 require-module lisp
     34 
     35 # Highlighters
     36 # ‾‾‾‾‾‾‾‾‾‾‾‾
     37 
     38 add-highlighter shared/scheme regions
     39 add-highlighter shared/scheme/code default-region group
     40 
     41 add-highlighter shared/scheme/string region %{(?<!#\\)"} (?<!\\)(\\\\)*" fill string
     42 add-highlighter shared/scheme/comment region %{(?<!#\\);} '$' fill comment
     43 add-highlighter shared/scheme/comment-form region -recurse "\(" "#;\(" "\)" fill comment
     44 add-highlighter shared/scheme/comment-block region "#\|" "\|#" fill comment
     45 
     46 add-highlighter shared/scheme/code/ regex (#t|#f) 0:value
     47 
     48 # Numbers
     49 add-highlighter shared/scheme/code/ regex '(#[bB]#[eiEI]|#[eiEI]#[bB]|#[bB])[01]+' 0:value
     50 add-highlighter shared/scheme/code/ regex '(#[oO]#[eiEI]|#[eiEI]#[oO]|#[oO])[0-7]+' 0:value
     51 add-highlighter shared/scheme/code/ regex '(#[dD](#[eiEI])?|#[eiEI]#[dD]|#[eiEI])(\d+(?:\.\d*)?|\.\d+)([esfdlESFDL][-+]?\d+)?' 0:value
     52 add-highlighter shared/scheme/code/ regex '(#[xX]#[eiEI]|#[eiEI]#[xX]|#[xX])[0-9a-fA-F]+' 0:value
     53 
     54 add-highlighter shared/scheme/code/ regex (#\\((\w+)|(.))) 0:value
     55 
     56 add-highlighter shared/scheme/code/ regex '#!(?:no-)?fold-case\b' 0:meta
     57 
     58 evaluate-commands %sh{ exec awk -f - <<'EOF'
     59     BEGIN {
     60         split("and begin call-with-current-continuation call/cc case case-lambda cond define "\
     61               "define-record-type define-values delay delay-force do else guard if lambda "\
     62               "let let* let-values let*-values letrec letrec* or set! unless when", keywords);
     63 
     64         # Macro expressions, imports/exports/library
     65         split("begin-syntax cond-expand define-library define-syntax export import include "\
     66               "include-ci include-library-declarations let-syntax letrec-syntax quote "\
     67               "quasiquote syntax-rules syntax-case unquote unquote-splicing", meta);
     68 
     69         # Basic operators.
     70         split("* + - ... / < <= = => > >=", operators);
     71 
     72         # Procedures that create a base type and their predicates (for easier type checking)
     73         split("list vector bytevector cons string boolean? list? pair? vector? bytevector? "\
     74               "string? char? complex? eof-object eof-object? input-port? null? number? "\
     75               "output-port? port? procedure? symbol?", types);
     76 
     77         # R7RS available procedures
     78         split("abs acos angle append apply asin assoc assq assv atan boolean=? "\
     79               "bytevector-append bytevector-copy bytevector-copy! bytevector-length "\
     80               "bytevector-u8-ref bytevector-u8-set! caaaar caaadr caaar caadar caaddr caadr "\
     81               "caar cadaar cadadr cadar caddar cadddr caddr cadr call-with-input-file "\
     82               "call-with-output-file call-with-values car cdaaar cdaadr cdaar cdadar cdaddr "\
     83               "cdadr cdar cddaar cddadr cddar cdddar cddddr cdddr cddr cdr ceiling "\
     84               "char->integer char-alphabetic? char-ci<=? char-ci<? char-ci=? char-ci>=? "\
     85               "char-ci>? char-downcase char-foldcase char-lower-case? char-numeric? "\
     86               "char-ready? char-upcase char-upper-case? char-whitespace? char<=? char<? "\
     87               "char=? char>=? char>? close-input-port close-output-port close-port cons cos "\
     88               "current-input-port current-output-port denominator digit-value display "\
     89               "dynamic-wind eq? equal? eqv? error error-object-irritants "\
     90               "error-object-message error-object? eval even? exact exact->inexact "\
     91               "exact-integer? exact-integer-sqrt exact? exp expt features file-error? "\
     92               "finite? floor floor/ floor-quotient floor-remainder for-each force "\
     93               "flush-output-port gcd get-output-bytevector get-output-string guard imag-part "\
     94               "inexact->exact inexact inexact? infinite? input-port-open? integer->char "\
     95               "integer? interaction-environment lcm length list list-copy list-set! "\
     96               "list->string list->vector list-ref list-tail load log magnitude "\
     97               "make-bytevector make-list make-parameter make-polar make-promise "\
     98               "make-rectangular make-string make-vector map max member memq memv min modulo "\
     99               "nan? negative? newline not null-environment number->string numerator odd? "\
    100               "open-input-bytevector open-input-file open-input-string "\
    101               "open-output-bytevector open-output-file open-output-string output-port-open? "\
    102               "or parameterize peek-char peek-u8 positive? promise? quotient raise "\
    103               "raise-continuable rational? rationalize read read-bytevector read-bytevector! "\
    104               "read-char read-error? read-line read-string read-u8 real-part real? remainder "\
    105               "reverse round scheme-report-environment set-car! set-cdr! sin square sqrt "\
    106               "string->list string->number string->symbol string->utf8 string->vector "\
    107               "string-append string-ci<=? string-ci<? string-ci=? string-ci>=? string-ci>? "\
    108               "string-copy string-copy! string-downcase string-fill! string-foldcase "\
    109               "string-for-each string-length string-map string-ref string-set! string-upcase "\
    110               "string<=? string<? string=? string>=? string>? substring symbol=? "\
    111               "symbol->string syntax-error tan textual-port? truncate truncate/ "\
    112               "truncate-quotient truncate-remainder u8-ready? unless utf8->string values "\
    113               "vector vector->list vector->string vector-append vector-copy vector-copy! "\
    114               "vector-for-each vector-fill! vector-length vector-map vector-ref vector-set! "\
    115               "when with-exception-handler with-input-from-file with-output-to-file write "\
    116               "write-bytevector write-char write-string write-u8 zero?", builtins);
    117 
    118         non_word_chars="['\"\\s\\(\\)\\[\\]\\{\\};]";
    119 
    120         normal_identifiers="-!$%&\\*\\+\\./:<=>\\?@\\^_~a-zA-Z0-9";
    121         identifier_chars="[" normal_identifiers "][" normal_identifiers ",#]*";
    122     }
    123     function kak_escape(s) {
    124         gsub(/'/, "''", s);
    125         return "'" s "'";
    126     }
    127     function add_highlighter(regex, highlight) {
    128         printf("add-highlighter shared/scheme/code/ regex %s %s\n", kak_escape(regex), highlight);
    129     }
    130     function quoted_join(words, quoted, first) {
    131         first=1
    132         for (i in words) {
    133             if (!first) { quoted=quoted "|"; }
    134             quoted=quoted "\\Q" words[i] "\\E";
    135             first=0;
    136         }
    137         return quoted;
    138     }
    139     function add_word_highlighter(words, face, regex) {
    140         regex = "(?<![" normal_identifiers "])(" quoted_join(words) ")(?![" normal_identifiers "])";
    141         add_highlighter(regex, "1:" face);
    142     }
    143     function print_words(words) {
    144         for (i in words) { printf(" %s", words[i]); }
    145     }
    146 
    147     BEGIN {
    148         printf("declare-option str-list scheme_static_words ");
    149         print_words(keywords); print_words(meta); print_words(operators); print_words(builtins);
    150         printf("\n");
    151 
    152         add_word_highlighter(keywords, "keyword");
    153         add_word_highlighter(meta, "meta");
    154         add_word_highlighter(operators, "operator");
    155         add_word_highlighter(builtins, "function");
    156         add_word_highlighter(types, "type");
    157         add_highlighter(non_word_chars "+('" identifier_chars ")", "1:attribute");
    158         add_highlighter("\\(define\\W+\\((" identifier_chars ")", "1:function");
    159         add_highlighter("\\(define\\W+(" identifier_chars ")\\W+\\(lambda", "1:function");
    160 
    161         # unprefixed decimals
    162         add_highlighter("(?<![" normal_identifiers "])(\\d+(\\.\\d*)?|\\.\\d+)(?:[esfdlESFDL][-+]?\\d+)?(?![" normal_identifiers "])", "0:value");
    163         # inf and nan
    164         add_highlighter("(?<![" normal_identifiers "])[+-](?:inf|nan)\\.0(?![" normal_identifiers "])", "0:value");
    165     }
    166 EOF
    167 }
    168 
    169 }