vimrc cleanup (move templates, remove static scopes)

This commit is contained in:
Michael Campagnaro 2023-01-24 14:08:56 -05:00
parent 9e07b24cf7
commit cef7886859

157
vimrc
View File

@ -435,9 +435,10 @@ augroup campoCmds
autocmd BufWritePost .vimrc silent! source $MYVIMRC autocmd BufWritePost .vimrc silent! source $MYVIMRC
autocmd BufWritePost *.vim silent! source $MYVIMRC autocmd BufWritePost *.vim silent! source $MYVIMRC
autocmd BufWritePost ~/.vimrc.private silent! source $MYVIMRC autocmd BufWritePost ~/.vimrc.private silent! source $MYVIMRC
autocmd BufWritePost ~/.vimrc_templates.private silent! source $MYVIMRC
" Remove trailing whitespace when saving any file. " Remove trailing whitespace when saving any file.
function! s:StripTrailingWhitespaces() function! StripTrailingWhitespaces()
if g:campo_strip_trailing_whitespace == 1 if g:campo_strip_trailing_whitespace == 1
if len(g:campo_directories_to_ignore_when_stripping_trailing_whitespace) if len(g:campo_directories_to_ignore_when_stripping_trailing_whitespace)
for path in g:campo_directories_to_ignore_when_stripping_trailing_whitespace for path in g:campo_directories_to_ignore_when_stripping_trailing_whitespace
@ -484,127 +485,8 @@ augroup campoCmds
%s/\s\+$//e %s/\s\+$//e
call cursor(l, c) call cursor(l, c)
endfun endfun
autocmd BufWritePre * call s:StripTrailingWhitespaces() autocmd BufWritePre * call StripTrailingWhitespaces()
"////////////////////////////////////////////////////////////////
" FILE TEMPLATES
"////////////////////////////////////////////////////////////////
" C/C++ template.
function! s:CFileTemplate()
let s:env = {
\ 'filename': expand('%:t'),
\ 'creation_date': strftime('%Y-%m-%d'),
\ 'year': strftime('%Y'),
\ 'copyright_owner': 'Jelly Pixel, Inc. All Rights Reserved.'
\}
let l:template =<< trim EOS
/*==================================================================================================
File: ${filename}
Creation Date: ${creation_date}
Creator: Michael Campagnaro
Notice!: (C) Copyright ${year} by ${copyright_owner}
================================================================================================*/
////////////////////////////////////////////////////////////////////////////////////////////////////
// # Defines
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// # Globals
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// # Structs
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// # Macros
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// # Private API
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// # Public API
////////////////////////////////////////////////////////////////////////////////////////////////////
EOS
return map(l:template, { _, line -> substitute(line, '${\(.\{-}\)}', '\=get(s:env, submatch(1), submatch(1))', 'g') } )
endfunction
function! s:InsertCHeaderGates()
call append(0, '#pragma once')
"let l:gatename = substitute(toupper(expand("%:t")), '\\.', '_', 'g')
"call append(0, '#ifndef '. l:gatename)
"call append(line('$'), '#define '. l:gatename)
"call append(line('$'), '#endif')
endfunction
" sh template
function! s:ShellScriptTemplate()
let l:template =<< trim EOS
#!/usr/bin/env bash
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
BOLD="$(tput bold)"
DIM="\e[2m"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
BOLD=""
NORMAL=""
fi
error() {
printf "${BOLD}${RED}$1${NORMAL}\n"
}
abort() {
error "\nAborting..."
exit 1
}
set -e
cwd=$PWD
uname_s="$(uname -s)"
case "${uname_s}" in
Linux*) machine=Linux;;
Darwin*) machine=MacOS;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${uname_s}"
esac
printf "${YELLOW}Platform: $machine${NORMAL}\n"
EOS
return l:template
endfunction
" Shell script template.
autocmd BufNewFile *.sh call append(0, s:ShellScriptTemplate())
" C/C++ file.
autocmd BufNewFile *.{c,cc,cpp,h,hpp,inl} call append(0, s:CFileTemplate())
autocmd BufNewFile *.{h,hpp,inl} call s:InsertCHeaderGates()
augroup END augroup END
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@ -641,7 +523,7 @@ cmap %/ %:p:h/
" order to only call it once for a group of saves I've had to remap the " order to only call it once for a group of saves I've had to remap the
" various save commands to a function call. " various save commands to a function call.
function! s:CreateCtags() function! CreateCtags()
" Only allow one instance of ctags to run in this directory at any given time. " Only allow one instance of ctags to run in this directory at any given time.
let l:lock_file = "ctags.lock" let l:lock_file = "ctags.lock"
if filereadable(l:lock_file) || filereadable("newtags") if filereadable(l:lock_file) || filereadable("newtags")
@ -697,12 +579,12 @@ endfun
" file using <leader>rv or whatever. :ReloadVimrcError " file using <leader>rv or whatever. :ReloadVimrcError
function! DoSingleWrite() function! DoSingleWrite()
write! write!
call s:CreateCtags() call CreateCtags()
endfunction endfunction
function! DoSingleWriteThenQuit() function! DoSingleWriteThenQuit()
write! write!
call s:CreateCtags() call CreateCtags()
quit quit
endfunction endfunction
@ -713,7 +595,7 @@ function! DoMultiWrite()
bufdo wa bufdo wa
" Restore the last buffer because it may have changed. " Restore the last buffer because it may have changed.
exec "buffer " . l:current_buffer exec "buffer " . l:current_buffer
call s:CreateCtags() call CreateCtags()
endfunction endfunction
cnoreabbrev w :call DoSingleWrite() cnoreabbrev w :call DoSingleWrite()
@ -800,29 +682,6 @@ noremap <leader>v :e %:p:s,.h$,.X123X,:s,.cpp$,.h,:s,.X123X$,.cpp,<CR>
" Replace all instances of the highlighted text with whatever you enter. " Replace all instances of the highlighted text with whatever you enter.
nnoremap <c-g> :%s///g<left><left> nnoremap <c-g> :%s///g<left><left>
"////////////////////////////////////////////////////////////////
" QUICKLY OPEN C++ SOURCE OR HEADER FILE
"////////////////////////////////////////////////////////////////
function! s:CompleteFilenameWithoutExtension(ArgLead, CmdLine, CursorPos)
" Returns a matching filename without the period that separates the name
" from the extension.
let l:file = substitute(glob(a:ArgLead.'*', 0, 0), "[\.].*", "", "*")
return l:file
endfunction
" Custom command to open cpp and h files without typing an extension
"command! -nargs=+ -complete=custom,s:CompleteFilenameWithoutExtension "OpenCppSource exec ':e <args>.cpp'
":ca c OpenCppSource
":ca C OpenCppSource
"command! -nargs=+ -complete=custom,s:CompleteFilenameWithoutExtension "OpenCppHeader exec ':e <args>.h'
":ca h OpenCppHeader
":ca H OpenCppHeader
"command! -nargs=+ -complete=custom,s:CompleteFilenameWithoutExtension "OpenCppSourceAndHeader exec ':vsp | :e <args>.h | :sp <args>.cpp'
":ca b OpenCppSourceAndHeader
":ca B OpenCppSourceAndHeader
"//////////////////////////////////////////////////////////////// "////////////////////////////////////////////////////////////////
" MULTIPURPOSE TAB KEY " MULTIPURPOSE TAB KEY
@ -1533,7 +1392,7 @@ function! GlobalReplaceIt(confirm_replacement) range
silent! exec 'source _replace_session.vim | !rm _replace_session.vim &>/dev/null' silent! exec 'source _replace_session.vim | !rm _replace_session.vim &>/dev/null'
endif endif
call s:CreateCtags() call CreateCtags()
else else
call PrintError("Unable to search since you're not in a git repo!") call PrintError("Unable to search since you're not in a git repo!")
endif endif