kakoune

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

ocaml.kak (5793B)


      1 # http://ocaml.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*\.(ml|mli|mll|mly)$ %{
      8     set-option buffer filetype ocaml
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=ocaml %{
     15     require-module ocaml
     16     set-option window static_words %opt{ocaml_static_words}
     17     hook window InsertChar -group ocaml-insert '\*' ocaml-insert-closing-comment-bracket
     18     hook window InsertChar \n -group ocaml-insert ocaml-insert-on-new-line
     19     hook window ModeChange pop:insert:.* -group ocaml-trim-indent ocaml-trim-indent
     20 }
     21 
     22 hook -group ocaml-highlight global WinSetOption filetype=ocaml %{
     23     add-highlighter window/ocaml ref ocaml
     24     alias window alt ocaml-alternative-file
     25     hook -once -always window WinSetOption filetype=.* %{
     26         unalias window alt ocaml-alternative-file
     27         remove-highlighter window/ocaml
     28     }
     29 }
     30 
     31 provide-module ocaml %{
     32 
     33 # Highlighters
     34 # ‾‾‾‾‾‾‾‾‾‾‾‾
     35 
     36 add-highlighter shared/ocaml regions
     37 add-highlighter shared/ocaml/code default-region group
     38 add-highlighter shared/ocaml/string region (?<!['\\])" (?<!\\)(\\\\)*" fill string
     39 add-highlighter shared/ocaml/quotedstring region -match-capture %"\{(\w*)\|" %"\|(\w*)\}" fill string
     40 add-highlighter shared/ocaml/comment region -recurse \Q(* \Q(* \Q*) fill comment
     41 
     42 add-highlighter shared/ocaml/code/char regex %{\B'([^'\\]|(\\[\\"'nrtb])|(\\\d{3})|(\\x[a-fA-F0-9]{2})|(\\o[0-7]{3}))'\B} 0:value
     43 
     44 # integer literals
     45 add-highlighter shared/ocaml/code/ regex \b[0-9][0-9_]*([lLn]?)\b                          0:value
     46 add-highlighter shared/ocaml/code/ regex \b0[xX][A-Fa-f0-9][A-Fa-f0-9_]*([lLn]?)\b         0:value
     47 add-highlighter shared/ocaml/code/ regex \b0[oO][0-7][0-7_]*([lLn]?)\b                     0:value
     48 add-highlighter shared/ocaml/code/ regex \b0[bB][01][01_]*([lLn]?)\b                       0:value
     49 # float literals
     50 add-highlighter shared/ocaml/code/ regex \b[0-9][0-9_]*(\.[0-9_]*)?([eE][+-]?[0-9][0-9_]*)?                       0:value
     51 add-highlighter shared/ocaml/code/ regex \b0[xX][A-Fa-f0-9][A-Fa-f0-9]*(\.[a-fA-F0-9_]*)?([pP][+-]?[0-9][0-9_]*)? 0:value
     52 # constructors. must be put before any module name highlighter, as a fallback for capitalized identifiers
     53 add-highlighter shared/ocaml/code/ regex \b[A-Z][a-zA-Z0-9_]*\b                            0:value
     54 # the module name in a module path, e.g. 'M' in 'M.x'
     55 add-highlighter shared/ocaml/code/ regex (\b[A-Z][a-zA-Z0-9_]*[\h\n]*)(?=\.)               0:module
     56 # (simple) module declarations
     57 add-highlighter shared/ocaml/code/ regex (?<=module)([\h\n]+[A-Z][a-zA-Z0-9_]*)            0:module
     58 # (simple) signature declarations. 'type' is also highlighted, due to the lack of quantifiers in lookarounds.
     59 # Hence we must put keyword highlighters after this to recover proper highlight for 'type'
     60 add-highlighter shared/ocaml/code/ regex (?<=module)([\h\n]+type[\h\n]+[A-Z][a-zA-Z0-9_]*) 0:module
     61 # (simple) open statements
     62 add-highlighter shared/ocaml/code/ regex (?<=open)([\h\n]+[A-Z][a-zA-Z0-9_]*)              0:module
     63 # operators
     64 add-highlighter shared/ocaml/code/ regex [@!$%%^&*\-+=|<>/?]+                              0:operator
     65 
     66 
     67 # Macro
     68 # ‾‾‾‾‾
     69 
     70 evaluate-commands %sh{
     71   keywords="and|as|asr|assert|begin|class|constraint|do|done|downto|else|end|exception|external|false"
     72   keywords="${keywords}|for|fun|function|functor|if|in|include|inherit|initializer|land|lazy|let|lor"
     73   keywords="${keywords}|lsl|lsr|lxor|match|method|mod|module|mutable|new|nonrec|object|of|open|or"
     74   keywords="${keywords}|private|rec|sig|struct|then|to|true|try|type|val|virtual|when|while|with"
     75 
     76   printf %s\\n "declare-option str-list ocaml_static_words ${keywords}" | tr '|' ' '
     77 
     78 # must be put at last, since we have highlighted some keywords ('type' in 'module type') to other things
     79   printf %s "
     80     add-highlighter shared/ocaml/code/ regex \b(${keywords})\b 0:keyword
     81   "
     82 }
     83 
     84 # Conveniences
     85 # ‾‾‾‾‾‾‾‾‾‾‾‾
     86 
     87 # C has header and source files and you need to often switch between them.
     88 # Similarly OCaml has .ml (implementation) and .mli (interface files) and
     89 # one often needs to switch between them.
     90 #
     91 # This command provides a simple functionality that allows you to accomplish this.
     92 define-command ocaml-alternative-file -docstring 'Switch between .ml and .mli file or vice versa' %{
     93     evaluate-commands %sh{
     94         if [ "${kak_buffile##*.}" = 'ml' ]; then
     95             printf "edit -- '%s'" "$(printf %s "${kak_buffile}i" | sed "s/'/''/g")"
     96         elif [ "${kak_buffile##*.}" = 'mli' ]; then
     97             printf "edit -- '%s'" "$(printf %s "${kak_buffile%i}" | sed "s/'/''/g")"
     98         fi
     99     }
    100 }
    101 
    102 }
    103 
    104 # Remove trailing whitespaces
    105 define-command -hidden ocaml-trim-indent %{
    106     evaluate-commands -no-hooks -draft -itersel %{
    107         try %{ execute-keys -draft x s \h+$ <ret> d }
    108     }
    109 }
    110 
    111 # Preserve indentation when creating new line
    112 define-command -hidden ocaml-insert-on-new-line %{
    113     evaluate-commands -draft -itersel %{
    114         # copy white spaces at the beginnig of the previous line
    115         try %{ execute-keys -draft k x s ^\h+ <ret> y jgh P x s \h+$ <a-d> }
    116         # increase indentation if the previous line ended with either '=' sign or do keyword
    117         try %{ execute-keys -draft k x s (=|\bdo)$ <ret> j <a-gt> }
    118     }
    119 }
    120 
    121 # The OCaml comment is `(* Some comment *)`. Like the C-family this can be a multiline comment.
    122 #
    123 # Recognize when the user is trying to commence a comment when they type `(*` and
    124 # then automatically insert `*)` on behalf of the user. A small convenience.
    125 define-command -hidden ocaml-insert-closing-comment-bracket %{
    126     try %{
    127         execute-keys -draft 'HH<a-k>\(\*<ret>'
    128         execute-keys '  *)<left><left><left>'
    129     }
    130 }