# -*- coding: utf-8 -*-

import os.path
import vim
from citation_vim.utils import raiseError

def get_vim_context(context):

    context.mode = vim.eval("g:citation_vim_mode")
    if context.mode == "bibtex":
        context.bibtex_file = context.get_bibtex_file()
    elif context.mode == "zotero":
        context.zotero_path = get_zotero_path()
    else:
        raiseError(u"'g:citation_vim_mode' must be set to 'zotero' or 'bibtex'")

    try:
        context.cache_path = os.path.expanduser(vim.eval("g:citation_vim_cache_path"))
    except:
        raiseError(u"'g:citation_vim_cache_path' is not set")

    context.collection   = vim.eval("g:citation_vim_collection")
    context.key_format   = vim.eval("g:citation_vim_key_format")
    context.desc_format  = vim.eval("g:citation_vim_description_format")
    context.desc_fields  = vim.eval("g:citation_vim_description_fields")
    context.wrap_chars   = vim.eval("g:citation_vim_source_wrap")
    context.et_al_limit  = int(vim.eval("g:citation_vim_et_al_limit"))
    context.zotero_version = int(vim.eval("g:citation_vim_zotero_version"))
    context.source       = vim.eval("a:source")
    context.source_field = vim.eval("a:field")

    context.cache = True
    searchkeys_string = vim.eval("l:searchkeys")
    if len(searchkeys_string) > 0:
        context.cache = False
        context.searchkeys = searchkeys_string.split()
    else:
        context.searchkeys = []

    return context

def get_zotero_path():
    try:
        file = vim.eval("g:citation_vim_zotero_path")
        return os.path.expanduser(file)
    except:
        raiseError(u"global variable 'g:citation_vim_zotero_path' is not set")

def get_bibtex_file():
    try:
        file = vim.eval("g:citation_vim_bibtex_file")
        return os.path.expanduser(file)
    except:
        raiseError(u"'g:citation_vim_bibtex_file' is not set")

@@ -232,24 +232,16 @@
 _file = 
-context.
 get_bibt
@@ -250,16 +250,16 @@
 _file()%0A
+
     elif
@@ -425,29 +425,16 @@
 tex'%22)%0A%0A
-    try:%0A    
     cont
@@ -454,141 +454,25 @@
 h = 
-os.path.expanduser(vim.eval(%22g:citation_vim_cache_path%22))%0A    except:%0A        raiseError(u%22'g:citation_vim_cache_path' is not set%22
+get_cache_path(
 )%0A
-%0A
     
@@ -1068,208 +1068,68 @@
 ext.
-cache = True%0A    searchkeys_string = vim.eval(%22l:searchkeys%22)%0A    if len(searchkeys_string) %3E 0:%0A        context.cache = False%0A        context.searchkeys = searchkeys_string.split()%0A    else:%0A        
+searchkeys = get_searchkeys()%0A    context.cache = can_cache(
 cont
@@ -1146,13 +1146,9 @@
 keys
- = %5B%5D
+)
 %0A%0A  
@@ -1513,32 +1513,32 @@
 le)%0A    except:%0A
-
         raiseErr
@@ -1572,20 +1572,429 @@
 _file' is not set%22)%0A
+%0Adef get_cache_path():%0A    try:%0A        return os.path.expanduser(vim.eval(%22g:citation_vim_cache_path%22))%0A    except:%0A        raiseError(u%22'g:citation_vim_cache_path' is not set%22)%0A%0Adef get_searchkeys():%0A    searchkeys = vim.eval(%22l:searchkeys%22)%0A    if len(searchkeys) %3E 0:%0A        return searchkeys.split()%0A    return %5B%5D%0A%0Adef can_cache(searchkeys):%0A    if searchkeys %3E %5B%5D:%0A        return True%0A    return False%0A

refactor loader