view vim/autoload/ludo.vim @ 495:232351531855

Vim config improvements - Support for gui vs non gui plugin exclusions rules - Move HasPlugin to an autoload function that can be used in vimrc-local - Don't load the local bundle if it doesn't exist - Handle VS vs UE for building projects
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 12 Nov 2021 10:51:57 -0800
parents 1a54ffbc3b15
children
line wrap: on
line source

let g:ludo_trace = 0

" Get the platform we're running on.
if has("win32") || has("win64") || has("dos32")
    let s:vim_platform = "windows"
    let s:path_sep = "\\"
elseif has("mac")
    let s:vim_platform = "mac"
    let s:path_sep = '/'
else
    let s:vim_platform = "unix"
    let s:path_sep = '/'
endif
function! ludo#platform() abort
    return s:vim_platform
endfunction

" Get our vim directory. 
let s:vim_home = expand("<sfile>:h:h")

" Get local path.
function! ludo#localpath(...) abort
    return s:vim_home.s:path_sep.join(a:000, s:path_sep)
endfunction

" Debug logging.
function! ludo#trace(msg) abort
    if g:ludo_trace
        echom a:msg
    endif
endfunction

" Warning message.
function! ludo#warn(msg) abort
    echohl WarningMsg
    echomsg "ludo: Warning: ".a:msg
    echohl None
endfunction

" Error message.
function! ludo#error(msg) abort
    let v:errmsg = "ludo: Error: ".a:msg
    echohl ErrorMsg
    echom v:errmsg
    echohl None
endfunction

" Returns whether a plugin file exists in the runtime path.
function! ludo#has_plugin(plugname, ...) abort
    let l:dirname = 'plugin/'
    if a:0 && a:1
        let l:dirname = 'autoload/'
    endif
    return globpath(&runtimepath, l:dirname.a:plugname.'.vim') !=# ''
endfunction

" Loads `pathogenrc` files in each bundle directory and, if found,
" builds an exclude list based on the glob patterns found in them.
"
function! ludo#setup_pathogen(bundle_dirs) abort
    for bundle_dir in a:bundle_dirs
        let l:rcfile = bundle_dir.'.pathogenrc'
        if !filereadable(l:rcfile)
            call ludo#trace("No bundle configuration file: ".l:rcfile)
            continue
        endif

        let l:included = []
        let l:excluded = []
        call ludo#trace("Reading bundle configuration file: ".l:rcfile)
        let l:rclines = readfile(l:rcfile)
        for line in l:rclines
            if line[0] == '#'
                continue
            endif

            if strcharpart(line, 0, 4) == "gui:"
                if !has('gui')
                    call ludo#trace("Ignoring gui-only line: ".line) 
                    continue
                else
                    let line = line[4:]
                endif
            endif
            if strcharpart(line, 0, 6) == "nogui:"
                if has('gui')
                    call ludo#trace("Ignoring terminal-only line: ".line) 
                    continue
                else
                    let line = line[6:]
                endif
            endif

            let l:add_to = l:included
            let l:remove_from = l:excluded
            if line[0] == '-'
                let l:add_to = l:excluded
                let l:remove_from = l:included
                let line = line[1:]
            endif

            let l:incls = glob(bundle_dir.'/'.line, 1, 1)
            for incl in l:incls
                let l:idx = index(l:remove_from, incl)
                if l:idx >= 0
                    call remove(l:remove_from, l:idx)
                endif
                call add(l:add_to, incl)
            endfor
        endfor

        for excl in l:excluded
            if isdirectory(excl)
                let l:excl_name = fnamemodify(excl, ':t')
                call add(g:pathogen_disabled, l:excl_name)
            endif
        endfor
    endfor
    call ludo#trace("Exclude list: ".join(g:pathogen_disabled, ', '))
endfunction

" Toggle quicklist window
"
function! ludo#toggle_quicklist() abort
	let l:qlwin = -1
	let l:wincount = winnr('$')
	for l:winidx in range(1, l:wincount)
		let l:winbuftype = getwinvar(l:winidx, '&buftype', '')
		if l:winbuftype == 'quickfix'
			let l:qlwin = l:winidx
			break
		endif
	endfor

	if l:qlwin < 0
		copen
	else
		cclose
	endif
endfunction

" Better tags browser using FZF
"
function! ludo#run_fzf_tags(args, bang) abort
    let l:tag_files = tagfiles()
    let l:tag_lister = 
                \'python '.
                \ludo#localpath('scripts', 'list_tags.py').' -a 24 '.
                \join(map(l:tag_files, "shellescape(v:val)"))
    let options = {
                \'source': l:tag_lister,
                \'sink': function('s:tags_sink'),
                \'options': '--algo=v1 --tiebreak=begin --prompt "Tags> "'
                \}
    return fzf#run(fzf#wrap('tags', options, a:bang))
endfunction

function! s:tags_sink(line) abort
	let l:tokens = matchlist(
                \a:line, 
                \'\v^(\S+)\s+([^\t]+)\t([^\t]{-1,})(;")?\t[a-z]+\tline\:([0-9]+)')
    if len(l:tokens) < 5
        call fb#warn("Couldn't parse line '".a:line."', got '".string(l:tokens)."'")
        return
    endif

    let [l:tag, l:source_path, l:regex, l:line] = 
                \[l:tokens[1], tolower(l:tokens[2]), l:tokens[3], l:tokens[4]]
    let l:cur_path = fnamemodify(bufname('%'), ':p')
    let l:candidates = taglist(l:tag, l:cur_path)

    if len(l:candidates) == 0
        call fb#warn("No candidates found for '".l:tag."'")
        return
    endif

    if len(l:candidates) == 1
        execute ':tjump '.l:tag
        return
    endif

    let l:tagidx = 0
    for cndd in l:candidates
        let l:tagidx += 1
        if tolower(cndd['filename']) != l:source_path
            continue
        endif
        if cndd['cmd'] != l:regex
            continue
        endif
        break
    endfor
    execute ':'.string(l:tagidx).'tag '.l:tag
endfunction

function! ludo#build_vs_or_ue() abort
    if !empty(get(g:, 'unreal_branch_dir', ''))
        UnrealBuild
    elseif !empty(get(g: 'vimcrosoft_current_sln', ''))
        VimcrosoftBuildActiveProject
    else
        call ludo#warn("No VS or UE project active")
    endif
endfunction