kakoune

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

c-family.kak (26255B)


      1 # Detection
      2 hook global BufCreate .*\.(cc|cpp|cxx|C|hh|hpp|hxx|H)$ %{
      3     set-option buffer filetype cpp
      4 }
      5 
      6 hook global BufSetOption filetype=c\+\+ %{
      7     hook -once buffer NormalIdle '' "set-option buffer filetype cpp"
      8 }
      9 
     10 hook global BufCreate .*\.c$ %{
     11     set-option buffer filetype c
     12 }
     13 
     14 hook global BufCreate .*\.h$ %{
     15     try %{
     16         execute-keys -draft %{%s\b::\b|\btemplate\h*<lt>|\bclass\h+\w+|\b(typename|namespace)\b|\b(public|private|protected)\h*:<ret>}
     17         set-option buffer filetype cpp
     18     } catch %{
     19         set-option buffer filetype c
     20     }
     21 }
     22 
     23 hook global BufCreate .*\.m %{
     24     set-option buffer filetype objc
     25 }
     26 
     27 hook global WinSetOption filetype=(c|cpp|objc) %[
     28     require-module c-family
     29 
     30     evaluate-commands "set-option window static_words %%opt{%val{hook_param_capture_1}_static_words}"
     31 
     32     hook -group "%val{hook_param_capture_1}-trim-indent" window ModeChange pop:insert:.* c-family-trim-indent
     33     hook -group "%val{hook_param_capture_1}-insert" window InsertChar \n c-family-insert-on-newline
     34     hook -group "%val{hook_param_capture_1}-indent" window InsertChar \n c-family-indent-on-newline
     35     hook -group "%val{hook_param_capture_1}-indent" window InsertChar \{ c-family-indent-on-opening-curly-brace
     36     hook -group "%val{hook_param_capture_1}-indent" window InsertChar \} c-family-indent-on-closing-curly-brace
     37     hook -group "%val{hook_param_capture_1}-insert" window InsertChar \} c-family-insert-on-closing-curly-brace
     38 
     39     alias window alt "%val{hook_param_capture_1}-alternative-file"
     40 
     41     hook -once -always window WinSetOption filetype=.* "
     42         remove-hooks window %val{hook_param_capture_1}-.+
     43         unalias window alt %val{hook_param_capture_1}-alternative-file
     44     "
     45 ]
     46 
     47 hook -group c-highlight global WinSetOption filetype=c %{
     48     add-highlighter window/c ref c
     49     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/c }
     50 }
     51 
     52 hook -group cpp-highlight global WinSetOption filetype=cpp %{
     53     add-highlighter window/cpp ref cpp
     54     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/cpp }
     55 }
     56 
     57 hook -group objc-highlight global WinSetOption filetype=objc %{
     58     add-highlighter window/objc ref objc
     59     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/objc }
     60 }
     61 
     62 
     63 provide-module c-family %§
     64 
     65 define-command -hidden c-family-trim-indent %{
     66     # remove the line if it's empty when leaving the insert mode
     67     try %{ execute-keys -draft x 1s^(\h+)$<ret> d }
     68 }
     69 
     70 define-command -hidden c-family-indent-on-newline %< evaluate-commands -draft -itersel %<
     71     execute-keys <semicolon>
     72     try %<
     73         # if previous line is part of a comment, do nothing
     74         execute-keys -draft <a-?>/\*<ret> <a-K>^\h*[^/*\h]<ret>
     75     > catch %<
     76         # else if previous line closed a paren (possibly followed by words and a comment),
     77         # copy indent of the opening paren line
     78         execute-keys -draft kx 1s(\))(\h+\w+)*\h*(\;\h*)?(?://\N+)?\n\z<ret> m<a-semicolon>J <a-S> 1<a-&>
     79     > catch %<
     80         # else if previous line closed a block comment, copy indent of the comment opening line
     81         execute-keys -draft kx 1s(\*/)\h*\n\z<ret> <a-?>/\*<ret> <a-semicolon>J <a-S> 1<a-&>
     82     > catch %<
     83         # else indent new lines with the same level as the previous one
     84         execute-keys -draft K <a-&>
     85     >
     86     # remove previous empty lines resulting from the automatic indent
     87     try %< execute-keys -draft k x <a-k>^\h+$<ret> Hd >
     88     # indent after an opening brace or parenthesis at end of line
     89     try %< execute-keys -draft k x <a-k>[{(]\h*$<ret> j <a-gt> >
     90     # indent after a label
     91     try %< execute-keys -draft k x s[a-zA-Z0-9_-]+:\h*$<ret> j <a-gt> >
     92     # indent after a statement not followed by an opening brace
     93     try %< execute-keys -draft k x s\)\h*(?://\N+)?\n\z<ret> \
     94                                <a-semicolon>mB <a-k>\A\b(if|for|while)\b<ret> <a-semicolon>j <a-gt> >
     95     try %< execute-keys -draft k x s \belse\b\h*(?://\N+)?\n\z<ret> \
     96                                j <a-gt> >
     97     # deindent after a single line statement end
     98     try %< execute-keys -draft K x <a-k>\;\h*(//\N+)?$<ret> \
     99                                K x s\)(\h+\w+)*\h*(//\N+)?\n(\N*\n){2}\z<ret> \
    100                                MB <a-k>\A\b(if|for|while)\b<ret> <a-S>1<a-&> >
    101     try %< execute-keys -draft K x <a-k>\;\h*(//\N+)?$<ret> \
    102                                K x s \belse\b\h*(?://\N+)?\n(\N*\n){2}\z<ret> \
    103                                <a-S>1<a-&> >
    104     # deindent closing brace(s) when after cursor
    105     try %< execute-keys -draft x <a-k> ^\h*[})] <ret> gh / [})] <esc> m <a-S> 1<a-&> >
    106     # align to the opening parenthesis or opening brace (whichever is first)
    107     # on a previous line if its followed by text on the same line
    108     try %< evaluate-commands -draft %<
    109         # Go to opening parenthesis and opening brace, then select the most nested one
    110         try %< execute-keys [c [({],[)}] <ret> >
    111         # Validate selection and get first and last char
    112         execute-keys <a-k>\A[{(](\h*\S+)+\n<ret> <a-K>"(([^"]*"){2})*<ret> <a-K>'(([^']*'){2})*<ret> <a-:><a-semicolon>L <a-S>
    113         # Remove possibly incorrect indent from new line which was copied from previous line
    114         try %< execute-keys -draft , <a-h> s\h+<ret> d >
    115         # Now indent and align that new line with the opening parenthesis/brace
    116         execute-keys 1<a-&> &
    117      > >
    118 > >
    119 
    120 define-command -hidden c-family-indent-on-opening-curly-brace %[
    121     # align indent with opening paren when { is entered on a new line after the closing paren
    122     try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> <a-S> 1<a-&> ]
    123     # align indent with opening paren when { is entered on a new line after the else
    124     try %[ execute-keys -draft -itersel hK x s \belse\b\h*(?://\N+)?\n\h*\{<ret> <a-S> 1<a-&> ]
    125 ]
    126 
    127 define-command -hidden c-family-indent-on-closing-curly-brace %[
    128     evaluate-commands -draft -itersel -verbatim try %[
    129         # check if alone on the line and select to opening curly brace
    130         execute-keys <a-h><a-:><a-k>^\h+\}$<ret>hm
    131         try %[
    132             # in case open curly brace follows a closing paren possibly with qualifiers, extend to opening paren
    133             execute-keys -draft <a-f>) <a-k> \A\)(\h+\w+)*\h*\{\z <ret>
    134             execute-keys <a-F>)M
    135         ]
    136         # align to selection start
    137         execute-keys <a-S>1<a-&>
    138     ]
    139 ]
    140 
    141 define-command -hidden c-family-insert-on-closing-curly-brace %[
    142     # add a semicolon after a closing brace if part of a class, union or struct definition
    143     evaluate-commands -itersel -draft -verbatim try %[
    144         execute-keys -draft hmh <a-?>\b(class|struct|union|enum)\b<ret> <a-K>\{<ret> <a-K>\)(\s+\w+)*\s*\z<ret>
    145         execute-keys -draft ';i;<esc>'
    146     ]
    147 ]
    148 
    149 define-command -hidden c-family-insert-on-newline %[ evaluate-commands -itersel -draft %[
    150     execute-keys <semicolon>
    151     try %[
    152         evaluate-commands -draft -save-regs '/"' %[
    153             # copy the commenting prefix
    154             execute-keys -save-regs '' k x1s^\h*(//+\h*)<ret> y
    155             try %[
    156                 # if the previous comment isn't empty, create a new one
    157                 execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P
    158             ] catch %[
    159                 # if there is no text in the previous comment, remove it completely
    160                 execute-keys d
    161             ]
    162         ]
    163 
    164         # trim trailing whitespace on the previous line
    165         try %[ execute-keys -draft k x s\h+$<ret> d ]
    166     ]
    167     try %[
    168         # if the previous line isn't within a comment scope, break
    169         execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret>
    170 
    171         # find comment opening, validate it was not closed, and check its using star prefixes
    172         execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*(\N*\n\h*\*)*\N*\n\h*.\z<ret>
    173 
    174         try %[
    175             # if the previous line is opening the comment, insert star preceeded by space
    176             execute-keys -draft kx<a-k>^\h*/\*<ret>
    177             execute-keys -draft i*<space><esc>
    178         ] catch %[
    179            try %[
    180                 # if the next line is a comment line insert a star
    181                 execute-keys -draft jx<a-k>^\h+\*<ret>
    182                 execute-keys -draft i*<space><esc>
    183             ] catch %[
    184                 try %[
    185                     # if the previous line is an empty comment line, close the comment scope
    186                     execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc>
    187                 ] catch %[
    188                     # if the previous line is a non-empty comment line, add a star
    189                     execute-keys -draft i*<space><esc>
    190                 ]
    191             ]
    192         ]
    193 
    194         # trim trailing whitespace on the previous line
    195         try %[ execute-keys -draft k x s\h+$<ret> d ]
    196         # align the new star with the previous one, keeping its indentation characters
    197         execute-keys -draft K <a-&>
    198         try %[ execute-keys -draft kx<a-k>^\h*/\*<ret> jgi i<space><esc> ]
    199     ]
    200 ] ]
    201 
    202 # Regions definition are the same between c++ and objective-c
    203 evaluate-commands %sh{
    204     for ft in c cpp objc; do
    205         if [ "${ft}" = "objc" ]; then
    206             maybe_at='@?'
    207         else
    208             maybe_at=''
    209         fi
    210 
    211         cat <<-EOF
    212             add-highlighter shared/$ft regions
    213             add-highlighter shared/$ft/code default-region group
    214             add-highlighter shared/$ft/string region %{$maybe_at(?<!')(?<!'\\\\)"} %{(?<!\\\\)(?:\\\\\\\\)*"} fill string
    215             add-highlighter shared/$ft/documentation_comment region /\*(\*[^/]|!) \*/ fill documentation
    216             add-highlighter shared/$ft/line_documentation_comment region //[/!] $ fill documentation
    217             add-highlighter shared/$ft/comment region /\\* \\*/ fill comment
    218             add-highlighter shared/$ft/line_comment region // (?<!\\\\)(?=\\n) fill comment
    219             add-highlighter shared/$ft/disabled region -recurse "#\\h*if(?:def)?" ^\\h*?#\\h*if\\h+(?:0|FALSE)\\b "#\\h*(?:else|elif|endif)" fill comment
    220             add-highlighter shared/$ft/ifdef region %{^\\h*\\K#\\h*(?:define|elif|if)(?=\\h)} %{(?<!\\\\)(?=\\s)|(?=//)} fill meta
    221             add-highlighter shared/$ft/macro region %{^\\h*#} %{(?<!\\\\)(?=\\n)|(?=//)} group
    222             add-highlighter shared/$ft/macro/ regex ^\\h*(#\\h*\\S*) 1:meta
    223             add-highlighter shared/$ft/macro/ regex ^\\h*#\\h*include\\h+(<[^>]*>|"(?:[^"\\\\]|\\\\.)*") 1:module
    224             add-highlighter shared/$ft/macro/ regex /\\*.*?\\*/ 0:comment
    225 	EOF
    226     done
    227 }
    228 
    229 # c specific
    230 add-highlighter shared/c/code/numbers regex %{\b-?(0[xX][0-9a-fA-F]+|\d+)([uU][lL]{0,2}|[lL]{1,2}[uU]?|[fFdDiI]|([eE][-+]?\d+))?|'((\\.)?|[^'\\])'} 0:value
    231 evaluate-commands %sh{
    232     # Grammar
    233     keywords='asm break case continue default do else for goto if return
    234               sizeof switch while offsetof alignas alignof defer'
    235     attributes='auto atomic const enum extern inline register restrict static
    236                 struct typedef union volatile thread_local'
    237     types='char double float int long short signed unsigned void
    238            complex imaginary
    239            fenv_t fexcept_t
    240            imaxdiv_t
    241            lconv
    242            float_t double_t
    243            jmp_buf
    244            sig_atomic_t
    245            va_list
    246            memory_order atomic_flag atomic_bool atomic_char atomic_schar atomic_uchar atomic_wchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_llong atomic_ulong atomic_ullong atomic_char16_t atomic_char32_t atomic_intptr_t atomic_intmax_t atomic_int8_t atomic_int16_t atomic_int32_t atomic_int64_t atomic_int_least8_t atomic_int_least16_t atomic_int_least32_t atomic_int_least64_t atomic_int_fast8_t atomic_int_fast16_t atomic_int_fast32_t atomic_int_fast64_t atomic_uintptr_t atomic_uintmax_t atomic_uint8_t atomic_uint16_t atomic_uint32_t atomic_uint64_t atomic_uint_least8_t atomic_uint_least16_t atomic_uint_least32_t atomic_uint_least64_t atomic_uint_fast8_t atomic_uint_fast16_t atomic_uint_fast32_t atomic_uint_fast64_t atomic_size_t atomic_ptrdiff_t
    247            bool
    248            ptrdiff_t size_t max_align_t wchar_t
    249            intptr_t intmax_t int8_t int16_t int32_t int64_t int_least8_t int_least16_t int_least32_t int_least64_t int_fast8_t int_fast16_t int_fast32_t int_fast64_t uintptr_t uintmax_t uint8_t uint16_t uint32_t uint64_t uint_least8_t uint_least16_t uint_least32_t uint_least64_t uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t
    250            FILE fpos_t
    251            div_t ldiv_t lldiv_t
    252            cnd_t thrd_t thrd_start_t tss_t tss_dtor_t mtx_t once_flag
    253            clock_t time_t timespec tm
    254            mbstate_t wint_t
    255            wctrans_t wctype_t
    256            char16_t char32_t
    257            ssize_t gid_t uid_t off_t off64_t useconds_t pid_t socklen_t'
    258 
    259     macros='assert static_assert NDEBUG
    260             I
    261             EDOM EILSEQ ERANGE errno
    262             FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW FE_UNDERFLOW FE_ALL_EXCEPT FE_DOWNWARD FE_TONEAREST FE_TOWARDZERO FE_UPWARD FE_DFL_ENV
    263             DECIMAL_DIG FLT_ROUNDS FLT_EVAL_METHOD FLT_RADIX FLT_DIG FLT_MANT_DIG FLT_DECIMAL_DIG FLT_MIN_EXP FLT_MIN_10_EXP FLT_MAX_EXP FLT_MAX FLT_EPSILON FLT_TRUE_MIN FLT_HAS_SUBNORM DBL_DIG DBL_MANT_DIG DBL_DECIMAL_DIG DBL_MIN_EXP DBL_MIN_10_EXP DBL_MAX_EXP DBL_MAX DBL_EPSILON DBL_TRUE_MIN DBL_HAS_SUBNORM LDBL_DIG LDBL_MANT_DIG LDBL_DECIMAL_DIG LDBL_MIN_EXP LDBL_MIN_10_EXP LDBL_MAX_EXP LDBL_MAX LDBL_EPSILON LDBL_TRUE_MIN LDBL_HAS_SUBNORM
    264             PRIdMAX PRIdPTR PRId8 PRId16 PRId32 PRId64 PRIdLEAST8 PRIdLEAST16 PRIdLEAST32 PRIdLEAST64 PRIdFAST8 PRIdFAST16 PRIdFAST32 PRIdFAST64 PRIiMAX PRIiPTR PRIi8 PRIi16 PRIi32 PRIi64 PRIiLEAST8 PRIiLEAST16 PRIiLEAST32 PRIiLEAST64 PRIiFAST8 PRIiFAST16 PRIiFAST32 PRIiFAST64 PRIoMAX PRIoPTR PRIo8 PRIo16 PRIo32 PRIo64 PRIoLEAST8 PRIoLEAST16 PRIoLEAST32 PRIoLEAST64 PRIoFAST8 PRIoFAST16 PRIoFAST32 PRIoFAST64 PRIuMAX PRIuPTR PRIu8 PRIu16 PRIu32 PRIu64 PRIuLEAST8 PRIuLEAST16 PRIuLEAST32 PRIuLEAST64 PRIuFAST8 PRIuFAST16 PRIuFAST32 PRIuFAST64 PRIxMAX PRIxPTR PRIx8 PRIx16 PRIx32 PRIx64 PRIxLEAST8 PRIxLEAST16 PRIxLEAST32 PRIxLEAST64 PRIxFAST8 PRIxFAST16 PRIxFAST32 PRIxFAST64 PRIXMAX PRIXPTR PRIX8 PRIX16 PRIX32 PRIX64 PRIXLEAST8 PRIXLEAST16 PRIXLEAST32 PRIXLEAST64 PRIXFAST8 PRIXFAST16 PRIXFAST32 PRIXFAST64 SCNdMAX SCNdPTR SCNd8 SCNd16 SCNd32 SCNd64 SCNdLEAST8 SCNdLEAST16 SCNdLEAST32 SCNdLEAST64 SCNdFAST8 SCNdFAST16 SCNdFAST32 SCNdFAST64 SCNiMAX SCNiPTR SCNi8 SCNi16 SCNi32 SCNi64 SCNiLEAST8 SCNiLEAST16 SCNiLEAST32 SCNiLEAST64 SCNiFAST8 SCNiFAST16 SCNiFAST32 SCNiFAST64 SCNoMAX SCNoPTR SCNo8 SCNo16 SCNo32 SCNo64 SCNoLEAST8 SCNoLEAST16 SCNoLEAST32 SCNoLEAST64 SCNoFAST8 SCNoFAST16 SCNoFAST32 SCNoFAST64 SCNuMAX SCNuPTR SCNu8 SCNu16 SCNu32 SCNu64 SCNuLEAST8 SCNuLEAST16 SCNuLEAST32 SCNuLEAST64 SCNuFAST8 SCNuFAST16 SCNuFAST32 SCNuFAST64 SCNxMAX SCNxPTR SCNx8 SCNx16 SCNx32 SCNx64 SCNxLEAST8 SCNxLEAST16 SCNxLEAST32 SCNxLEAST64 SCNxFAST8 SCNxFAST16 SCNxFAST32 SCNxFAST64
    265             and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq
    266             CHAR_MIN CHAR_MAX SCHAR_MIN SCHAR_MAX WCHAR_MIN WCHAR_MAX SHRT_MIN SHRT_MAX INT_MIN INT_MAX LONG_MIN LONG_MAX LLONG_MIN LLONG_MAX MB_LEN_MAX UCHAR_MAX USHRT_MAX UINT_MAX ULONG_MAX ULLONG_MAX CHAR_BIT
    267             LC_ALL LC_COLLATE LC_CTYPE LC_MONETARY LC_NUMERIC LC_TIME
    268             HUGE_VAL HUGE_VALF HUGE_VALL INFINITY NAN FP_INFINITE FP_NAN FP_NORMAL FP_SUBNORMAL FP_ZERO FP_FAST_FMA FP_FAST_FMAF FP_FAST_FMAL FP_ILOGB0 FP_ILOGBNAN MATH_ERRNO MATH_ERREXCEPT math_errhandling isgreater isgreaterequal isless islessequal islessgreater isunordered
    269             setjmp
    270             SIG_DFL SIG_ERR SIG_IGN SIGABRT SIGFPE SIGILL SIGINT SIGSEGV SIGTERM
    271             va_start va_arg va_end va_copy
    272             ATOMIC_BOOL_LOCK_FREE ATOMIC_CHAR_LOCK_FREE ATOMIC_CHAR16_T_LOCK_FREE ATOMIC_CHAR32_T_LOCK_FREE ATOMIC_WCHAR_T_LOCK_FREE ATOMIC_SHORT_LOCK_FREE ATOMIC_INT_LOCK_FREE ATOMIC_LONG_LOCK_FREE ATOMIC_LLONG_LOCK_FREE ATOMIC_POINTER_LOCK_FREE ATOMIC_FLAG_INIT ATOMIC_VAR_INIT memory_order_relaxed memory_order_consume memory_order_acquire memory_order_release memory_order_acq_rel memory_order_seq_cst kill_dependency
    273             true false
    274             NULL
    275             _IOFBF _IOLBF _IONBF BUFSIZ EOF FOPEN_MAX FILENAME_MAX TMP_MAX L_tmpnam SEEK_CUR SEEK_END SEEK_SET stderr stdin stdout
    276             EXIT_FAILURE EXIT_SUCCESS MB_CUR_MAX RAND_MAX
    277             PTRDIFF_MIN PTRDIFF_MAX SIG_ATOMIC_MIN SIG_ATOMIC_MAX WINT_MIN WINT_MAX INTMAX_MIN INTMAX_MAX INTPTR_MIN INTPTR_MAX INT8_MIN INT8_MAX INT16_MIN INT16_MAX INT32_MIN INT32_MAX INT64_MIN INT64_MAX INT_LEAST8_MIN INT_LEAST8_MAX INT_LEAST16_MIN INT_LEAST16_MAX INT_LEAST32_MIN INT_LEAST32_MAX INT_LEAST64_MIN INT_LEAST64_MAX INT_FAST8_MIN INT_FAST8_MAX INT_FAST16_MIN INT_FAST16_MAX INT_FAST32_MIN INT_FAST32_MAX INT_FAST64_MIN INT_FAST64_MAX UINTMAX_MAX UINTPTR_MAX UINT8_MAX UINT16_MAX UINT32_MAX UINT64_MAX UINT_LEAST8_MAX UINT_LEAST16_MAX UINT_LEAST32_MAX UINT_LEAST64_MAX UINT_FAST8_MAX UINT_FAST16_MAX UINT_FAST32_MAX UINT_FAST64_MAX INTMAX_C INT8_C INT16_C INT32_C INT64_C UINTMAX_C UINT8_C UINT16_C UINT32_C UINT64_C
    278             mtx_plain mtx_recursive mtx_timed thrd_timedout thrd_success thrd_busy thrd_error thrd_nomem ONCE_FLAG_INIT TSS_DTOR_ITERATION
    279             CLOCKS_PER_SEC TIME_UTC
    280             WEOF
    281             noreturn
    282             R_OK W_OK X_OK F_OK F_LOCK F_ULOCK F_TLOCK F_TEST'
    283 
    284     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
    285 
    286     # Add the language's grammar to the static completion list
    287     printf %s\\n "declare-option str-list c_static_words $(join "${keywords} ${attributes} ${types} ${macros}" ' ')"
    288 
    289     # Highlight keywords
    290     printf %s "
    291         add-highlighter shared/c/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
    292         add-highlighter shared/c/code/attributes regex \b($(join "${attributes}" '|'))\b 0:attribute
    293         add-highlighter shared/c/code/types regex \b($(join "${types}" '|'))\b 0:type
    294         add-highlighter shared/c/code/values regex \b($(join "${macros}" '|'))\b 0:value
    295     "
    296 }
    297 
    298 # c++ specific
    299 
    300 # raw strings
    301 add-highlighter shared/cpp/raw_string region -match-capture %{R"([^(]*)\(} %{\)([^")]*)"} fill string
    302 
    303 # integer literals
    304 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b[1-9]('?\d+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
    305 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b0b[01]('?[01]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
    306 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b0('?[0-7]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
    307 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
    308 
    309 # floating point literals
    310 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b\d('?\d+)*\.([fl]\b|\B)(?!\.)} 0:value
    311 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b\d('?\d+)*\.?e[+-]?\d('?\d+)*[fl]?\b(?!\.)} 0:value
    312 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)(\b(\d('?\d+)*)|\B)\.\d('?[\d]+)*(e[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value
    313 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*\.([fl]\b|\B)(?!\.)} 0:value
    314 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*\.?p[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value
    315 add-highlighter shared/cpp/code/ regex %{(?i)(?<!\.)\b0x([\da-f]('?[\da-f]+)*)?\.\d('?[\d]+)*(p[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value
    316 
    317 # character literals (no multi-character literals)
    318 add-highlighter shared/cpp/code/char regex %{(\b(u8|u|U|L)|\B)'((\\.)|[^'\\])'\B} 0:value
    319 
    320 evaluate-commands %sh{
    321     # Grammar
    322     keywords='alignas alignof and and_eq asm bitand bitor break case catch
    323               compl const_cast continue decltype delete do dynamic_cast
    324               else export for goto if new not not_eq operator or or_eq
    325               reinterpret_cast return sizeof static_assert static_cast switch
    326               throw try typedef typeid using while xor xor_eq'
    327     attributes='audit auto axiom const consteval constexpr default explicit
    328                 extern final friend inline mutable noexcept override private
    329                 protected public register requires static thread_local typename
    330                 virtual volatile'
    331     entities='class concept enum namespace struct template union'
    332     types='bool byte char char8_t char16_t char32_t double float int long
    333            max_align_t nullptr_t ptrdiff_t short signed size_t unsigned void
    334            wchar_t'
    335     values='NULL false nullptr this true'
    336 
    337     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
    338 
    339     # Add the language's grammar to the static completion list
    340     printf %s\\n "declare-option str-list cpp_static_words $(join "${keywords} ${attributes} ${entities} ${types} ${values}" ' ')"
    341 
    342     # Highlight keywords
    343     printf %s "
    344         add-highlighter shared/cpp/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
    345         add-highlighter shared/cpp/code/attributes regex \b($(join "${attributes} ${entities}" '|'))\b 0:attribute
    346         add-highlighter shared/cpp/code/types regex \b($(join "${types}" '|'))\b 0:type
    347         add-highlighter shared/cpp/code/values regex \b($(join "${values}" '|'))\b 0:value
    348     "
    349 }
    350 
    351 # c and c++ compiler macros
    352 evaluate-commands %sh{
    353     builtin_macros="__cplusplus|__STDC_HOSTED__|__FILE__|__LINE__|__DATE__|__TIME__|__STDCPP_DEFAULT_NEW_ALIGNMENT__"
    354 
    355     printf %s "
    356         add-highlighter shared/c/code/macros regex \b(${builtin_macros})\b 0:builtin
    357         add-highlighter shared/cpp/code/macros regex \b(${builtin_macros})\b 0:builtin
    358     "
    359 }
    360 
    361 # objective-c specific
    362 add-highlighter shared/objc/code/number regex %{\b-?\d+[fdiu]?|'((\\.)?|[^'\\])'} 0:value
    363 
    364 evaluate-commands %sh{
    365     # Grammar
    366     keywords='break case continue default do else for goto if return switch
    367               while'
    368     attributes='IBAction IBOutlet __block assign auto const copy enum extern
    369                 inline nonatomic readonly retain static strong struct typedef
    370                 union volatile weak'
    371     types='BOOL CGFloat NSInteger NSString NSUInteger bool char float
    372            instancetype int long short signed size_t unsigned void'
    373     values='FALSE NO NULL TRUE YES id nil self super'
    374     decorators='autoreleasepool catch class end implementation interface
    375                 property protocol selector synchronized synthesize try'
    376 
    377     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
    378 
    379     # Add the language's grammar to the static completion list
    380     printf %s\\n "declare-option str-list objc_static_words $(join "${keywords} ${attributes} ${types} ${values} ${decorators}" ' ')"
    381 
    382     # Highlight keywords
    383     printf %s "
    384         add-highlighter shared/objc/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
    385         add-highlighter shared/objc/code/attributes regex \b($(join "${attributes}" '|'))\b 0:attribute
    386         add-highlighter shared/objc/code/types regex \b($(join "${types}" '|'))\b 0:type
    387         add-highlighter shared/objc/code/values regex \b($(join "${values}" '|'))\b 0:value
    388         add-highlighter shared/objc/code/decorators regex  @($(join "${decorators}" '|'))\b 0:attribute
    389     "
    390 }
    391 
    392 declare-option -docstring %{
    393     control the type of include guard to be inserted in empty headers
    394     Can be one of the following:
    395         ifdef: old style ifndef/define guard
    396         pragma: newer type of guard using "pragma once"
    397 } str c_include_guard_style "ifdef"
    398 
    399 define-command -hidden c-family-insert-include-guards %{
    400     evaluate-commands %sh{
    401         case "${kak_opt_c_include_guard_style}" in
    402             ifdef)
    403                 echo 'execute-keys gg"%PI/<esc>xs^.*/<ret>dxs\.<ret>r_A_INCLUDED<esc>xyPPI#ifndef<space><esc>jI#define<space><esc>jI#endif<space>//<space><esc>O<esc>'
    404                 ;;
    405             pragma)
    406                 echo 'execute-keys ggi#pragma<space>once<esc>'
    407                 ;;
    408             *);;
    409         esac
    410     }
    411 }
    412 
    413 hook -group c-family-insert global BufNewFile .*\.(h|hh|hpp|hxx|H) c-family-insert-include-guards
    414 
    415 declare-option -docstring "colon separated list of path in which header files will be looked for" \
    416     str-list alt_dirs '.' '..'
    417 
    418 define-command -hidden c-family-alternative-file %{
    419     evaluate-commands %sh{
    420         file="${kak_buffile##*/}"
    421         file_noext="${file%.*}"
    422         dir=$(dirname "${kak_buffile}")
    423 
    424         # Set $@ to alt_dirs
    425         eval "set -- ${kak_quoted_opt_alt_dirs}"
    426 
    427         case ${file} in
    428             *.c|*.cc|*.cpp|*.cxx|*.C|*.inl|*.m)
    429                 for alt_dir in "$@"; do
    430                     for ext in h hh hpp hxx H; do
    431                         case "$alt_dir" in
    432                         /*) altname="${alt_dir}/${file_noext}.${ext}" ;;
    433                         *) altname="${dir}/${alt_dir}/${file_noext}.${ext}" ;;
    434                         esac
    435                         if [ -f "${altname}" ]; then
    436                             printf 'edit %%{%s}\n' "${altname}"
    437                             exit
    438                         fi
    439                     done
    440                 done
    441             ;;
    442             *.h|*.hh|*.hpp|*.hxx|*.H)
    443                 for alt_dir in "$@"; do
    444                     for ext in c cc cpp cxx C m; do
    445                         case "$alt_dir" in
    446                         /*) altname="${alt_dir}/${file_noext}.${ext}" ;;
    447                         *) altname="${dir}/${alt_dir}/${file_noext}.${ext}" ;;
    448                         esac
    449                         if [ -f "${altname}" ]; then
    450                             printf 'edit %%{%s}\n' "${altname}"
    451                             exit
    452                         fi
    453                     done
    454                 done
    455             ;;
    456             *)
    457                 echo "fail 'extension not recognized'"
    458                 exit
    459             ;;
    460         esac
    461         echo "fail 'alternative file not found'"
    462     }
    463 }
    464 
    465 define-command c-alternative-file -docstring "Jump to the alternate c file (header/implementation)" %{
    466     c-family-alternative-file
    467 }
    468 define-command cpp-alternative-file -docstring "Jump to the alternate cpp file (header/implementation)" %{
    469     c-family-alternative-file
    470 }
    471 define-command objc-alternative-file -docstring "Jump to the alternate objc file (header/implementation)" %{
    472     c-family-alternative-file
    473 }
    474 
    475 §
    476 
    477 # Module aliases
    478 provide-module c %{ require-module c-family }
    479 provide-module cpp %{ require-module c-family }
    480 provide-module objc %{ require-module c-family }