r.kak (7003B)
1 # http://kakoune.org 2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate (.*/)?(\.Rprofile|.*\.[rR]) %{ 8 set-option buffer filetype r 9 } 10 11 provide-module r %§ 12 13 # Highlighters & Completion 14 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 15 16 add-highlighter shared/r regions 17 add-highlighter shared/r/code default-region group 18 add-highlighter shared/r/double_string region '"' (?<!\\)(\\\\)*" fill string 19 add-highlighter shared/r/single_string region "'" (?<!\\)(\\\\)*' fill string 20 add-highlighter shared/r/comment region '#' '$' fill comment 21 add-highlighter shared/r/identifier region '`' (?<!\\)(\\\\)*` fill attribute 22 23 # see base::NumericConstants 24 add-highlighter shared/r/code/ regex '(?i)\b(?<![.\d])\d+(\.\d*)?(e[-+]?\d+)?(?I)[iL]?(?![\d.e\w])' 0:value 25 add-highlighter shared/r/code/ regex '(?i)(?<![.\d\w])\.\d+(e[-+]?\d+)?(?I)[iL]?(?![\d.e])' 0:value 26 add-highlighter shared/r/code/ regex '(?i)\b(?<![.\d])0x[0-9a-f]+?(p[-+]?\d+)?(?I)[iL]?(?![\d.e\w])' 0:value 27 28 evaluate-commands %sh{ 29 # see base::Reserved 30 values="TRUE|FALSE|NULL|Inf|NaN|NA|NA_integer_|NA_real_|NA_complex_|NA_character_|\.{3}|\.{2}\d+|" 31 keywords="if|else|repeat|while|function|for|in|next|break" 32 33 # see base::Ops and methods::Ops 34 math_functions="abs|sign|sqrt|floor|ceiling|trunc|round|signif|exp|log|expm1|log1p|cos|sin|tan|cospi|sinpi|tanpi|acos|asin|atan|cosh|sinh|tanh|acosh|asinh|atanh|lgamma|gamma|digamma|trigamma" 35 summary_functions="all|any|sum|prod|min|max|range" 36 complex_functions="Arg|Conj|Im|Mod|Re" 37 38 # Add the language's grammar to the static completion list 39 printf %s\\n "hook global WinSetOption filetype=python %{ 40 set-option window static_words ${values} ${keywords} ${math_functions} ${summary_functions} ${complex_functions} 41 }" | tr '|' ' ' 42 43 printf %s " 44 add-highlighter shared/r/code/ regex '\b(${values})\b' 0:value 45 add-highlighter shared/r/code/ regex '\b(${keywords})\b' 0:keyword 46 add-highlighter shared/r/code/ regex '\b(${math_functions}|${summary_functions}|${complex_functions})\b' 0:function 47 " 48 } 49 50 # see base::Syntax 51 add-highlighter shared/r/code/ regex (?<=[\w\s\d'"_)])(\$|@|\^|-|\+|%[^%^\n]+%|\*|/|<|>|<=|>=|!|&{1,2}|\|{1,2}|~|\?|:{1,3}|\[{1,2}|\]{1,2}|={1,2}|<{1,2}-|->{1,2}|!=|%%) 0:operator 52 53 54 # Commands 55 # ‾‾‾‾‾‾‾‾ 56 57 define-command -hidden r-trim-indent %{ 58 # remove the line if it's empty when leaving the insert mode 59 try %{ execute-keys -draft x 1s^(\h+)$<ret> d } 60 } 61 62 define-command -hidden r-indent-on-newline %< evaluate-commands -draft -itersel %< 63 execute-keys <semicolon> 64 try %< 65 # if previous line closed a paren (possibly followed by words and a comment), 66 # copy indent of the opening paren line 67 execute-keys -draft kx 1s(\))(\h+\w+)*\h*(\;\h*)?(?:#\N+)?\n\z<ret> m<a-semicolon>J <a-S> 1<a-&> 68 > catch %< 69 # else indent new lines with the same level as the previous one 70 execute-keys -draft K <a-&> 71 > 72 # remove previous empty lines resulting from the automatic indent 73 try %< execute-keys -draft k x <a-k>^\h+$<ret> Hd > 74 # indent after an opening brace or parenthesis at end of line 75 try %< execute-keys -draft k x s[{(]\h*$<ret> j <a-gt> > 76 # indent after a statement not followed by an opening brace 77 try %< execute-keys -draft k x s\)\h*(?:#\N+)?\n\z<ret> \ 78 <a-semicolon>mB <a-k>\A\b(if|for|while)\b<ret> <a-semicolon>j <a-gt> > 79 try %< execute-keys -draft k x s \belse\b\h*(?:#\N+)?\n\z<ret> \ 80 j <a-gt> > 81 # deindent after a single line statement end 82 try %< execute-keys -draft K x <a-k>\;\h*(#\N+)?$<ret> \ 83 K x s\)(\h+\w+)*\h*(#\N+)?\n(\N*\n){2}\z<ret> \ 84 MB <a-k>\A\b(if|for|while)\b<ret> <a-S>1<a-&> > 85 try %< execute-keys -draft K x <a-k>\;\h*(#\N+)?$<ret> \ 86 K x s \belse\b\h*(?:#\N+)?\n(\N*\n){2}\z<ret> \ 87 <a-S>1<a-&> > 88 # align to the opening parenthesis or opening brace (whichever is first) 89 # on a previous line if its followed by text on the same line 90 try %< evaluate-commands -draft %< 91 # Go to opening parenthesis and opening brace, then select the most nested one 92 try %< execute-keys [c [({],[)}] <ret> > 93 # Validate selection and get first and last char 94 execute-keys <a-k>\A[{(](\h*\S+)+\n<ret> <a-K>"(([^"]*"){2})*<ret> <a-K>'(([^']*'){2})*<ret> <a-:><a-semicolon>L <a-S> 95 # Remove possibly incorrect indent from new line which was copied from previous line 96 try %< execute-keys -draft , <a-h> s\h+<ret> d > 97 # Now indent and align that new line with the opening parenthesis/brace 98 execute-keys 1<a-&> & 99 > > 100 > > 101 102 define-command -hidden r-indent-on-opening-curly-brace %[ 103 # align indent with opening paren when { is entered on a new line after the closing paren 104 try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> <a-S> 1<a-&> ] 105 ] 106 107 define-command -hidden r-indent-on-closing-curly-brace %[ 108 # align to opening curly brace when alone on a line 109 try %[ 110 # in case open curly brace follows a closing paren, align indent with opening paren 111 execute-keys -itersel -draft <a-h><a-:><a-k>^\h+\}$<ret>hm <a-F>)M <a-k> \A\(.*\)\h\{.*\}\z <ret> <a-S>1<a-&> 112 ] catch %[ 113 # otherwise align with open curly brace 114 execute-keys -itersel -draft <a-h><a-:><a-k>^\h+\}$<ret>hm<a-S>1<a-&> 115 ] catch %[] 116 ] 117 118 define-command -hidden r-insert-on-newline %[ evaluate-commands -itersel -draft %[ 119 execute-keys <semicolon> 120 try %[ 121 evaluate-commands -draft -save-regs '/"' %[ 122 # copy the commenting prefix 123 execute-keys -save-regs '' k x1s^\h*(#+\h*)<ret> y 124 try %[ 125 # if the previous comment isn't empty, create a new one 126 execute-keys x<a-K>^\h*#+\h*$<ret> jxs^\h*<ret>P 127 ] catch %[ 128 # if there is no text in the previous comment, remove it completely 129 execute-keys d 130 ] 131 ] 132 ] 133 ] ] 134 135 § 136 137 # Initialization 138 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 139 140 hook -group r-highlight global WinSetOption filetype=r %{ 141 require-module r 142 add-highlighter window/r ref r 143 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/r } 144 } 145 146 hook global WinSetOption filetype=r %~ 147 require-module r 148 hook window ModeChange pop:insert:.* r-trim-indent 149 hook window InsertChar \n r-insert-on-newline 150 hook window InsertChar \n r-indent-on-newline 151 hook window InsertChar \{ r-indent-on-opening-curly-brace 152 hook window InsertChar \} r-indent-on-closing-curly-brace 153 154 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window r-.+ } 155 ~