hook global BufCreate .*\.(cc|cpp|cxx|C|hh|hpp|hxx|H)$ %{ set buffer filetype cpp set buffer mimetype '' } hook global BufCreate .*\.c$ %{ set buffer filetype c set buffer mimetype '' } 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 } set buffer mimetype '' } hook global BufSetOption mimetype=text/x-c %{ set buffer filetype c } hook global BufSetOption mimetype=text/x-c\+\+ %{ set buffer filetype cpp } hook global BufCreate .*\.m %{ set buffer filetype objc set buffer mimetype '' } hook global BufSetOption mimetype=text/x-objc %{ 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 ^\h+$ d ] ## 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 when inserting a newline try %[ exec -draft {b \`\( L s\`|.\' & ] ] ] 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 %[ 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 %[ eval -draft %[ ## select the previous line exec k try %{ ## if the previous line isn't within a comment scope, break exec ^(\h*/\*|\h+\*[^/]) ## simple test to check that the previous comment has been left open exec \*/\h*$ try %[ ## if the next line is a comment line, add a star exec -draft 2j^\h+\* exec -draft js^\h*a* ] catch %[ try %[ ## if the previous line is an empty comment line, close the comment scope exec -draft ^\h+\*\h+$ 1s\*(\h*)c/ ] catch %[ ## if the previous line is a non-empty comment line, add a star exec -draft js^\h*a* ] ] ## 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 ' addhl -group / regions -default code FT \ string %{MAYBEAT(?%ggxs\.c_A_INCLUDEDggxyppI#ifndefjI#definejI#endif//O' ;; pragma) echo 'exec ggi#pragmaonce' ;; *);; esac } } hook global BufNew .*\.(h|hh|hpp|hxx|H) _c-family-insert-include-guards decl 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}" | sed -e 's/;/ /g') file=$(basename "${kak_buffile}") 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%.*}.${ext}" [ -f ${altname} ] && break done [ -f ${altname} ] && break 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%.*}.${ext}" [ -f ${altname} ] && break done [ -f ${altname} ] && break done ;; *) echo "echo -color Error 'extension not recognized'" exit ;; esac if [ -f ${altname} ]; then printf %s\\n "edit '${altname}'" else echo "echo -color Error 'alternative file not found'" fi }}