Created
October 29, 2010 11:42
-
-
Save t9md/653387 to your computer and use it in GitHub Desktop.
Interchange variable between python and vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"=========================================== | |
" help Python | |
"=========================================== | |
" vim.eval(str) *python-eval* | |
" Evaluates the expression str using the vim internal expression | |
" evaluator (see |expression|). Returns the expression result as: | |
" - a string if the Vim expression evaluates to a string or number | |
" - a list if the Vim expression evaluates to a Vim list | |
" - a dictionary if the Vim expression evaluates to a Vim dictionary | |
" Dictionaries and lists are recursively expanded. | |
" | |
" vim.eval(str) *python-eval* | |
" vim内の式評価を使って、式を評価します(|expression|を参照)。戻り値は、 | |
" 次の通り: | |
" - Vimの式を評価した結果が文字列か数値ならば文字列 | |
" - Vimの式を評価した結果がリストならばリスト | |
" - Vimの式を評価した結果がVimの辞書ならば辞書 | |
" 辞書とリストは再帰的に展開されます。 | |
" | |
python << EOS | |
def vim2py(name): | |
"""Get Vim's variable from Python's world""" | |
return vim.eval(name) | |
def py2vim(name): | |
"""Export Python's variable to Vim world""" | |
cmd = "let %s = %s" % (name , repr(eval(name))) | |
vim.command(cmd) | |
def export_var_to_vim(*var_names): | |
for var_name in var_names: py2vim(var_name) | |
def import_var_from_vim(*var_names): | |
"""Import Vim's variable to Python world""" | |
result = {} | |
for var_name in var_names: result[var_name] = vim2py(var_name) | |
return result | |
EOS | |
echo "----------------------------------------" | |
echo "* Import Vim's variable to Python world" | |
echo "----------------------------------------" | |
let g:vim_num = 1 | |
let g:vim_str = "This is String" | |
let g:vim_list = [1, 2, 3, 4, 5, 6] | |
let g:vim_dict = { | |
\ 'port' : 80, | |
\ 'bind' : "localhost", | |
\ 'https' : 'none', | |
\ } | |
python <<EOS | |
import_var = [ "g:vim_num", "g:vim_str", "g:vim_list", "g:vim_dict" ] | |
vim_var = import_var_from_vim(*import_var) | |
EOS | |
echo "[Number]" | |
python print vim_var['g:vim_num'] | |
echo "" | |
echo "[String]" | |
python print vim_var['g:vim_str'] | |
echo "" | |
echo "[List]" | |
python print vim_var['g:vim_list'] | |
echo "" | |
echo "[Dict]" | |
python print vim_var['g:vim_dict'] | |
echo "" | |
echo "----------------------------------------" | |
echo "*Export Python's variable to Vim world" | |
echo "----------------------------------------" | |
python <<EOS | |
py_num = 1 | |
py_str = "This is String" | |
py_list = [1, 2, 3, 4, 5, 6] | |
py_dict = { | |
'port' : 80, | |
'bind' : "localhost", | |
'https' : 'none', | |
} | |
export_var = ["py_num", "py_str", "py_list", "py_dict"] | |
export_var_to_vim(*export_var) | |
EOS | |
echo "[Number]" | |
echo py_num | |
echo "" | |
echo "[String]" | |
echo py_str | |
echo "" | |
echo "[List]" | |
echo py_list | |
echo "" | |
echo "[Dict]" | |
echo py_dict | |
echo "" | |
"======================================== | |
" Result | |
"======================================== | |
"---------------------------------------- | |
"* Import Vim's variable to Python world | |
"---------------------------------------- | |
"[Number] | |
"1 | |
" | |
"[String] | |
"This is String | |
" | |
"[List] | |
"['1', '2', '3', '4', '5', '6'] | |
" | |
"[Dict] | |
"{'bind': 'localhost', 'port': '80', 'https': 'none'} | |
" | |
"---------------------------------------- | |
"*Export Python's variable to Vim world | |
"---------------------------------------- | |
"[Number] | |
"1 | |
" | |
"[String] | |
"This is String | |
" | |
"[List] | |
"[1, 2, 3, 4, 5, 6] | |
" | |
"[Dict] | |
"{'https': 'none', 'bind': 'localhost', 'port': 80} | |
" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment