dotfiles

<-- duh.
Log | Files | Refs | Submodules | LICENSE

commit da166cf1b90455aa6bb7e9dc93a7852f6f0c39ee
parent f98b232e26b24fd050336e50f9af3ec93a93e2a8
Author: hhvn <dev@hhvn.uk>
Date:   Sun, 27 Nov 2022 23:29:45 +0000

Comment precprocessor conditions with keybind

Diffstat:
M.config/nvim/init.vim | 67++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
D.config/nvim/modules/preproccomment.vim | 65-----------------------------------------------------------------
2 files changed, 66 insertions(+), 66 deletions(-)

diff --git a/.config/nvim/init.vim b/.config/nvim/init.vim @@ -12,6 +12,8 @@ set undodir=~/.local/share/nvim/undo set undofile set undolevels=1000 set undoreload=10000 +set backup +set backupdir=~/.local/share/nvim/backup "Leader let mapleader=";" @@ -63,6 +65,70 @@ noremap <silent> <c-s> :silent !plumb <cfile><CR> nmap <c-o> 0/<++><Enter>"_c4l imap <c-o> <Esc>0/<++><Enter>"_c4l +"Preprocessor comments +function! PreprocComment() + mark y + let saved_wrapscan=&wrapscan + set nowrapscan + let elsecomment="" + let endcomment="" + try + " Find the last #if in the buffer + $?^\s*#if + while 1 + " Build the comments for later use, based on current line + let content=getline('.') + let endcomment=BuildEndComment(content,endcomment) + " Change # into ## so we know we've already processed this one + " and don't find it again + s/^\s*\zs#/## + " Find the next #else, #elif, #endif which must belong to this #if + /^\s*#endif + let content=getline('.') + call setline('.',ReplaceComment(getline('.'),endcomment)) + s/^\s*\zs#/## + " Find the previous #if + ?^\s*#if + endwhile + catch /search hit TOP/ + " Once we have an error (pattern not found, i.e. no more left) + " Change all our ## markers back to # + silent! %s/^\s*\zs##/# + endtry + let &wrapscan=saved_wrapscan + normal `y +endfunc + +let s:PreProcCommentMatcher = '#\a\+\s\+\zs.\{-}\ze\(\s*\/\*.\{-}\*\/\)\?\s*$' + +function! BuildEndComment(content,previous) + let expression=escape(matchstr(a:content,s:PreProcCommentMatcher), '\~&') + if match(a:content,'#ifdef') != -1 + return "/* ".expression." */" + elseif match(a:content,'#ifndef') != -1 + return "/* not ".expression." */" + elseif match(a:content,'#if') != -1 + return "/* ".expression." */" + else + return "" + endif +endfunc + +function! ReplaceComment(content,comment) + let existing=escape(matchstr(a:content,'#\a\+\s\+\zs.\{-}\s*$'), '\~&') + if existing == "" + return substitute(a:content,'^\s*#\a\+\zs.*'," ".a:comment,'') + elseif existing != a:comment && match(existing,'XXX') == -1 + return a:content." /* XXX */" + else + return a:content + endif +endfunc +command PreprocComment call PreprocComment() +nnoremap <leader>ppc :PreprocComment<CR> + +nnoremap <leader>x :!chmod +x %s<CR> + "General stuff noremap dw diw noremap dew dw @@ -100,7 +166,6 @@ set clipboard=unnamed source ~/.config/nvim/modules/statusline.vim source ~/.config/nvim/modules/filetype.vim source ~/.config/nvim/modules/textmanipulation.vim -source ~/.config/nvim/modules/preproccomment.vim " quick-scope let g:qs_highlight_on_keys = ['f', 'F', 't', 'T'] diff --git a/.config/nvim/modules/preproccomment.vim b/.config/nvim/modules/preproccomment.vim @@ -1,65 +0,0 @@ -" Commenting of #endifs etc -" Author: Ben Schmidt, minor modifications by A. S. Budden. - -augroup preproc - au BufWrite *.[ch] :call SmartPreProcCommenter() -augroup END - -function! SmartPreProcCommenter() - mark y - let saved_wrapscan=&wrapscan - set nowrapscan - let elsecomment="" - let endcomment="" - try - " Find the last #if in the buffer - $?^\s*#if - while 1 - " Build the comments for later use, based on current line - let content=getline('.') - let endcomment=BuildEndComment(content,endcomment) - " Change # into ## so we know we've already processed this one - " and don't find it again - s/^\s*\zs#/## - " Find the next #else, #elif, #endif which must belong to this #if - /^\s*#endif - let content=getline('.') - call setline('.',ReplaceComment(getline('.'),endcomment)) - s/^\s*\zs#/## - " Find the previous #if - ?^\s*#if - endwhile - catch /search hit TOP/ - " Once we have an error (pattern not found, i.e. no more left) - " Change all our ## markers back to # - silent! %s/^\s*\zs##/# - endtry - let &wrapscan=saved_wrapscan - normal `y -endfunc - -let s:PreProcCommentMatcher = '#\a\+\s\+\zs.\{-}\ze\(\s*\/\*.\{-}\*\/\)\?\s*$' - -function! BuildEndComment(content,previous) - let expression=escape(matchstr(a:content,s:PreProcCommentMatcher), '\~&') - if match(a:content,'#ifdef') != -1 - return "/* ".expression." */" - elseif match(a:content,'#ifndef') != -1 - return "/* not ".expression." */" - elseif match(a:content,'#if') != -1 - return "/* ".expression." */" - else - return "" - endif -endfunc - -function! ReplaceComment(content,comment) - let existing=escape(matchstr(a:content,'#\a\+\s\+\zs.\{-}\s*$'), '\~&') - if existing == "" - return substitute(a:content,'^\s*#\a\+\zs.*'," ".a:comment,'') - elseif existing != a:comment && match(existing,'XXX') == -1 - return a:content." /* XXX */" - else - return a:content - endif -endfunc