function! lib#tab_zoom() if winnr('$') > 1 tab split elseif len(filter(map(range(tabpagenr('$')), 'tabpagebuflist(v:val + 1)'), \ 'index(v:val, '.bufnr('').') >= 0')) > 1 tabclose endif endfunction function! lib#buf_zoom() abort if exists('t:zoomed') && t:zoomed exec t:zoom_winrestcmd let t:zoomed = 0 else let t:zoom_winrestcmd = winrestcmd() resize vertical resize let t:zoomed = 1 endif endfunction " Peekabo Like functionality function! lib#reg() reg echo "Register: " let char = nr2char(getchar()) if char != "\" execute "normal! \"".char."p" endif redraw endfunction " " Verbatim matching for *. " function! lib#search() abort let regsave = @@ normal! gvy let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g') let @@ = regsave endfunction function! lib#search_all() abort call lib#search() call setqflist([]) execute 'bufdo vimgrepadd! /'. @/ .'/ %' endfunction " CREDIT: MHINZ " Switch to VCS root, if there is one. function! lib#cd() abort if &buftype =~# '\v(nofile|terminal)' || expand('%') =~# '^fugitive' return endif if !exists('s:cache') let s:cache = {} endif let dirs = [ '.git', '.hg', '.svn' ] let curdir = resolve(expand('%:p:h')) if !isdirectory(curdir) echohl WarningMsg | echo 'No such directory: '. curdir | echohl NONE return endif if has_key(s:cache, curdir) execute 'lcd' fnameescape(s:cache[curdir]) " echohl WarningMsg | echo 'Jumping to '. curdir | echohl NONE return endif for dir in dirs let founddir = finddir(dir, curdir .';') if !empty(founddir) break endif endfor let dir = empty(founddir) ? curdir : resolve(fnamemodify(founddir, ':p:h:h')) let s:cache[curdir] = dir execute 'lcd' fnameescape(dir) " echohl WarningMsg | echo 'Jumping to '. curdir | echohl NONE endfunction " " Smarter tag-based jumping. " function! lib#jump() abort if (&filetype == 'vim' && &buftype == 'nofile') || &buftype == 'quickfix' execute "normal! \" elseif &filetype == 'neoman' execute "normal \" else if exists('g:cscoped') try execute 'cscope find g' expand('') catch /E259/ echohl WarningMsg redraw | echomsg 'No match found: '. expand('') echohl NONE endtry else try execute "normal! g\" catch /E349/ " no identifier under cursor catch /E426/ echohl WarningMsg redraw | echomsg 'No match found: '. expand('') echohl NONE endtry endif normal! zvzt " call halo#run() endif endfunction ""---------------------------------------------------------------------------// " Windows ""---------------------------------------------------------------------------// " Auto resize Vim splits to active split to 70% - " https://stackoverflow.com/questions/11634804/vim-auto-resize-focused-window let g:auto_resize_on = 1 function! lib#auto_resize() if g:auto_resize_on == 1 let &winheight = &lines * 9 / 10 let &winwidth = &columns * 9 / 10 let g:auto_resize_on = 0 echom 'Auto resize on' else set winheight=30 set winwidth=30 let g:auto_resize_on = 1 endif endfunction " This keeps the cursor in place when using * or # function! lib#star_search(key) abort let g:_view = winsaveview() let out = a:key if mode() ==? 'v' let out = '"vy' . (a:key == '*' ? '/' : '?') . "\=" \. 'substitute(escape(@v, ''/\.*$^~[''), ''\_s\+'', ''\\_s\\+'', ''g'')' \. "\\" endif return out."N:\" \ .join(['let g:_pos = getpos(''.'')[1:2]', \ ':call winrestview(g:_view)', \ ':call cursor(g:_pos)', \ ':set hlsearch', \ ':unlet! g:_view', \ ':unlet! g:_pos'], "\")."\" endfunction "==========[ ModifyLineEndDelimiter ]========== " Description: " This function takes a delimiter character and: " - removes that character from the end of the line if the character at the end " of the line is that character " - removes the character at the end of the line if that character is a " delimiter that is not the input character and appends that character to " the end of the line " - adds that character to the end of the line if the line does not end with " a delimiter " " Delimiters: " - "," " - ";" "========================================== function! lib#ModifyLineEndDelimiter(character) let line_modified = 0 let line = getline('.') for character in [',', ';'] " check if the line ends in a trailing character if line =~ character . '$' let line_modified = 1 " delete the character that matches: " reverse the line so that the last instance of the character on the " line is the first instance let newline = join(reverse(split(line, '.\zs')), '') " delete the instance of the character let newline = substitute(newline, character, '', '') " reverse the string again let newline = join(reverse(split(newline, '.\zs')), '') " if the line ends in a trailing character and that is the " character we are operating on, delete it. if character != a:character let newline .= a:character endif break endif endfor " if the line was not modified, append the character if line_modified == 0 let newline = line . a:character endif call setline('.', newline) endfunction " saves all the visible windows if needed/possible function! lib#AutoSave() let this_window = winnr() windo if &buftype != "nofile" && expand('%') != '' && &modified | write | endif execute this_window . 'wincmd w' endfunction "line containing the match blinks function! lib#HLNext (blinktime) set invcursorline redraw exec 'sleep ' . float2nr(a:blinktime * 1000) . 'm' set invcursorline redraw endfunction ""---------------------------------------------------------------------------// " Credit: June Gunn - AutoSave {{{1 " ---------------------------------------------------------------------------- function! lib#buffer_autosave(enable) augroup autosave autocmd! if a:enable autocmd TextChanged,InsertLeave \ if empty(&buftype) && !empty(bufname('')) \| silent! update \| endif endif augroup END endfunction command! -bang AutoSave call lib#autosave(1) " custom text-object for numerical values function! lib#Numbers() call search('\d\([^0-9\.]\|$\)', 'cW') normal v call search('\(^\|[^0-9\.]\d\)', 'becW') endfunction