hook global BufCreate .*\.(cc|cpp|cxx|C|hh|hpp|hxx|H)$ %{ set buffer filetype cpp } hook global BufSetOption filetype=c\+\+ %{ set buffer filetype cpp } hook global BufCreate .*\.c$ %{ set buffer filetype c } hook global BufCreate .*\.h$ %{ try %{ exec -draft %{%s\b::\b|\btemplate\h*|\bclass\h+\w+|\b(typename|namespace)\b|\b(public|private|protected)\h*:} set buffer filetype cpp } catch %{ set buffer filetype c } } hook global BufCreate .*\.m %{ set buffer filetype objc } def -hidden c-family-trim-autoindent %[ eval -draft -itersel %[ # remove the line if it's empty when leaving the insert mode try %[ exec 1s^(\h+)$ d ] ] ] def -hidden c-family-indent-on-newline %< eval -draft -itersel %< exec \; try %< # if previous line closed a paren, copy indent of the opening paren line exec -draft k 1s(\))(\h+\w+)*\h*(\;\h*)?$ mJ s\`|.\' 1 > catch %< # else indent new lines with the same level as the previous one exec -draft K > # remove previous empty lines resulting from the automatic indent try %< exec -draft k ^\h+$ Hd > # indent after an opening brace try %< exec -draft k s\{\h*$ j > # indent after a label try %< exec -draft k s[a-zA-Z0-9_-]+:\h*$ j > # indent after a statement not followed by an opening brace try %< exec -draft k \b(if|else|for|while)\h*\(.+?\)\h*$ j > # align to the opening parenthesis or opening brace (whichever is first) # on a previous line if its followed by text on the same line try %< eval -draft %< # Go to opening parenthesis and opening brace, then select the most nested one try %< try %< exec [bZ[B > catch %< exec [B > > # Validate selection and get first and last char exec \`[{(](\h*\S+)+\n L s\`|.\' # Remove eventual indent from new line try %< exec -draft s\h+ d > # Now align that new line with the opening parenthesis/brace exec & > > > > def -hidden c-family-indent-on-opening-curly-brace %[ # align indent with opening paren when { is entered on a new line after the closing paren try %[ exec -draft -itersel h)M \`\(.*\)\h*\n\h*\{\' s \`|.\' 1 ] ] def -hidden c-family-indent-on-closing-curly-brace %[ # align to opening curly brace when alone on a line try %[ exec -itersel -draft ^\h+\}$hms\`|.\'1 ] ] def -hidden c-family-insert-on-closing-curly-brace %[ # add a semicolon after a closing brace if part of a class, union or struct definition try %[ exec -itersel -draft hmB\`\h*(class|struct|union) a\; ] ] def -hidden c-family-insert-on-newline %[ eval -draft %[ exec \; try %[ eval -draft %[ # copy the commenting prefix exec -save-regs '' k 1s^\h*(//+\h*) y try %[ # if the previous comment isn't empty, create a new one exec ^\h*//+\h*$ js^\h*P ] catch %[ # if there is no text in the previous comment, remove it completely exec d ] ] ] try %[ # if the previous line isn't within a comment scope, break exec -draft k ^(\h*/\*|\h+\*(?!/)) # find comment opening, validate it was not closed, and check its using star prefixes exec -draft /\* \*/ \`\h*/\*([^\n]*\n\h*\*)*[^\n]*\n\h*.\' try %[ # if the previous line is opening the comment, insert star preceeded by space exec -draft k^\h*/\* exec -draft i* ] catch %[ try %[ # if the next line is a comment line insert a star exec -draft j^\h+\* exec -draft i* ] catch %[ try %[ # if the previous line is an empty comment line, close the comment scope exec -draft k^\h+\*\h+$ 1s\*(\h*)c/ ] catch %[ # if the previous line is a non-empty comment line, add a star exec -draft i* ] ] ] # trim trailing whitespace on the previous line try %[ exec -draft 1s(\h+)$d ] # align the new star with the previous one exec J1s^[^*]*(\*)& ] ] ] # Regions definition are the same between c++ and objective-c %sh{ for ft in c cpp objc; do if [ "${ft}" = "objc" ]; then maybe_at='@?' else maybe_at='' fi printf %s\\n ' add-highlighter -group / regions -default code -match-capture FT \ string %{MAYBEAT(?%ggxs\.c_A_INCLUDEDggxyppI#ifndefjI#definejI#endif//O' ;; pragma) echo 'exec ggi#pragmaonce' ;; *);; esac } } hook global BufNewFile .*\.(h|hh|hpp|hxx|H) c-family-insert-include-guards decl -docstring "colon separated list of path in which header files will be looked for" \ str-list alt_dirs ".:.." def c-family-alternative-file -docstring "Jump to the alternate file (header/implementation)" %{ %sh{ alt_dirs=$(printf %s\\n "${kak_opt_alt_dirs}" | tr ':' '\n') file="${kak_buffile##*/}" file_noext="${file%.*}" dir=$(dirname "${kak_buffile}") case ${file} in *.c|*.cc|*.cpp|*.cxx|*.C|*.inl|*.m) for alt_dir in ${alt_dirs}; do for ext in h hh hpp hxx H; do altname="${dir}/${alt_dir}/${file_noext}.${ext}" if [ -f ${altname} ]; then printf 'edit %%{%s}\n' "${altname}" exit fi done done ;; *.h|*.hh|*.hpp|*.hxx|*.H) for alt_dir in ${alt_dirs}; do for ext in c cc cpp cxx C m; do altname="${dir}/${alt_dir}/${file_noext}.${ext}" if [ -f ${altname} ]; then printf 'edit %%{%s}\n' "${altname}" exit fi done done ;; *) echo "echo -color Error 'extension not recognized'" exit ;; esac echo "echo -color Error 'alternative file not found'" }}