I've written a little project abstraction script for Vim, but one
problem is that it throws a series of errors if the specified project
does not exist. At the exit of s:ProjGetInfo, you'll see that I've
raised the error, but I need this exception to be fatal -- the script
should not resume. How can I do that (or is there a safer way to code
the line that assigns ProjGetInfo's return value to project)?

let g:ProjFileBrowser = 'NERDTree'
let g:ProjFile = '~/.vimproj'

fun! s:OpenProj(name)
    let project = s:ProjGetInfo(a:name)
    exec 'cd ' . project['path']
    " Customization file is not required
    if has_key(project, 'vim') == 1
        exec 'so ' . project['vim']
    end
    exec g:ProjFileBrowser
endf

fun! s:ProjGetInfo(name)
    try
        let s:Config = readfile(expand(g:ProjFile))
    catch /E484.*/
        exec s:echoError('Could not read project file "' .
g:ProjFile . '"')
    endt

    let projects = {}
    let title = ''

    " Simple ini parser
    for line in s:Config
        let line = s:strip(line)
        if strlen(line) > 0
            if match(line, '[') == 0
                let title = strpart(line, 1, strlen(line) - 2)
                let projects[title] = {}
            else
                let option = map(split(line, '='), 's:strip(v:val)')
                let key = option[0]
                let projects[title][key] = option[1]
            end
        end
    endfo

    if has_key(projects, a:name) == 1
        return projects[a:name]
    else
        exec s:echoError('Project "' . a:name . '" not found in ' .
g:ProjFile)
    end
endf

" Stole these two functions from NERDTree
fun! s:echo(msg)
    redraw
    echomsg 'Proj: ' . a:msg
endf

fun! s:echoError(msg)
    echohl errormsg
    call s:echo(a:msg)
    echohl normal
endf
" But nothing else

fun! s:strip(text)
    return substitute(a:text, '\s', '', 'g')
endf

" Commands
command! -n=1 Proj :call s:OpenProj(<args>)
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to