Source

aurum / autoload / aurum.vim

"▶1 
scriptencoding utf-8
execute frawor#Setup('0.1', {'@%aurum/repo': '5.5',
            \                '@aurum/cache': '2.0',
            \            '@%aurum/cmdutils': '4.0',})
"▶1 getcrf
function s:F.id(val)
    return a:val
endfunction
function s:F.getcrf()
    let cbvar=s:_r.cache.getcbvar()
    if has_key(cbvar, '__relfname') && has_key(cbvar, 'repo') &&
                \localtime()-cbvar._timerepo<cbvar._maxtimerepo
        return [cbvar, cbvar.repo, cbvar.__relfname]
    endif
    try
        silent let [repo, rev, file]=s:_r.cmdutils.getrrf({'repo': ':'}, 0,
                    \                                     'getsilent')[1:]
    catch /^Frawor:[^:]\+:nrepo:/
        return [cbvar, 0, 0]
    endtry
    if repo isnot 0 && file isnot 0
        call s:_r.cache.get('repo', s:F.id, [repo], {})
        let cbvar.__relfname=file
    endif
    return [cbvar, repo, file]
endfunction
"▶1 unload
function s:._unload()
    for adesc in values(s:akeys)
        call adesc.repo.functions.aremove(adesc.repo, adesc.rcid)
    endfor
endfunction
"▶1 anew
let s:abuffers={}
let s:akeys={}
function s:F.anew(buf, repo, key, func, ...)
    if !has_key(s:abuffers, a:buf)
        let s:abuffers[a:buf]={}
    endif
    let key=a:func.string(a:000).a:repo.path
    if !has_key(s:akeys, key)
        let interval=s:_r.cache.getinterval(a:key)
        let rcid=call(a:repo.functions['a'.a:func], [a:repo,interval]+a:000, {})
        let s:akeys[key]={'repo': a:repo, 'rcid': rcid, 'buffers': {a:buf : 1},
                    \      'key': key}
    else
        let s:akeys[key].buffers[a:buf]=1
    endif
    augroup AuRemoveRC
        autocmd! BufWipeOut,BufFilePost <buffer> call s:F.abw(+expand('<abuf>'))
    augroup END
    let s:abuffers[a:buf][a:key]=s:akeys[key]
    return s:F.aget(s:akeys[key], 1)
endfunction
let s:_augroups+=['AuRemoveRC']
"▶1 aget
function s:F.aget(adesc, now)
    return a:adesc.repo.functions.aget(a:adesc.repo, a:adesc.rcid, a:now)
endfunction
"▶1 abw
function s:F.abw(buf)
    if has_key(s:abuffers, a:buf)
        for adesc in values(remove(s:abuffers, a:buf))
            unlet adesc.buffers[a:buf]
            if empty(adesc.buffers)
                unlet s:akeys[adesc.key]
                call adesc.repo.functions.aremove(adesc.repo, adesc.rcid)
            endif
        endfor
    endif
endfunction
"▶1 aswitch
let s:pexpr='map(values(v:val), '.
            \   '"v:val.repo.functions.%s(v:val.repo, v:val.rcid)")'
function s:F.aswitch()
    let bl=tabpagebuflist()
    call map(copy(s:abuffers), 'index(bl, +v:key)==-1 ? '.
                \                   printf(s:pexpr, 'apause').' : '.
                \                   printf(s:pexpr, 'aresume'))
endfunction
augroup AuRCSwitchBuffers
    autocmd BufEnter * :call s:F.aswitch()
augroup END
let s:_augroups+=['AuRCSwitchBuffers']
"▶1 aurum#repository
function aurum#repository()
    let repo=s:_r.cache.get('repo', s:_r.repo.get, [':'], {})
    if repo is 0
        return {}
    endif
    return repo
endfunction
let s:_functions+=['aurum#repository']
"▶1 aurum#changeset
function aurum#changeset(...)
    let repo=((a:0)?(a:1):(aurum#repository()))
    if empty(repo)
        return {}
    endif
    return s:_r.cache.get('cs', repo.functions.getwork, [repo], {})
endfunction
let s:_functions+=['aurum#changeset']
"▶1 filestatus
function s:F.filestatus(status)
    return get(keys(filter(copy(a:status), '!empty(v:val)')), 0, '')
endfunction
"▶1 aurum#status
function aurum#status(...)
    if !empty(&buftype)
        return ''
    endif
    let buf=bufnr('%')
    if has_key(s:abuffers, buf) && has_key(s:abuffers[buf], 'status')
        return s:F.filestatus(s:F.aget(s:abuffers[buf].status, 0))
    endif
    let [cbvar, repo, file]=s:F.getcrf()
    if repo is 0 || file is 0
        return ''
    endif
    if has_key(repo.functions, 'astatus')
        augroup AuInvalidateStatusCache
            autocmd! BufWritePost <buffer>
                        \ :call s:F.aget(s:abuffers[expand('<abuf>')].status, 1)
        augroup END
        return s:F.filestatus(s:F.anew(buf, repo, 'status', 'status',
                    \                  0, 0, [file], 1, 1))
    endif
    augroup AuInvalidateStatusCache
        autocmd! BufWritePost <buffer> :call s:_r.cache.del('status')
    augroup END
    return s:F.filestatus(s:_r.cache.get('status', repo.functions.status,
                \                        [repo, 0, 0, [file], 1, 1], {}))
endfunction
let s:_functions+=['aurum#status']
let s:_augroups+=['AuInvalidateStatusCache']
"▶1 aurum#branch
function aurum#branch(...)
    let buf=bufnr('%')
    if has_key(s:abuffers, buf) && has_key(s:abuffers[buf], 'branch')
        return s:F.aget(s:abuffers[buf].branch, 0)
    endif
    let repo=((a:0)?(a:1):(aurum#repository()))
    if empty(repo)
        return ''
    endif
    if has_key(repo.functions, 'agetrepoprop')
        return s:F.anew(buf, repo, 'branch', 'getrepoprop', 'branch')
    endif
    return s:_r.cache.get('branch', repo.functions.getrepoprop,
                \         [repo, 'branch'], {})
endfunction
let s:_functions+=['aurum#branch']
"▶1
call frawor#Lockvar(s:, '_r,abuffers,akeys')
" vim: ft=vim ts=4 sts=4 et fmr=▶,▲
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.