Created
October 30, 2010 17:31
-
-
Save t9md/655556 to your computer and use it in GitHub Desktop.
emacs's anything like command launcher
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
# -*- coding: utf8 -*- | |
import vim | |
import sys | |
import re | |
import os | |
def normal(str): | |
vim.command("normal! "+str) | |
def select_all_buffer(): | |
"""Select all buffer """ | |
normal("ggVG") | |
# def current_line(): | |
def vim_eval(arg): | |
return vim.eval(arg) | |
def buffer_length(): | |
"""Return buffer length """ | |
return len(cb()) | |
def num_of_buffers(): | |
return len(vim.buffers) | |
def all_buffer_name(): | |
for b in vim.buffers: | |
print b.name | |
def num_of_windows(): | |
return len(vim.windows) | |
def all_window_name(): | |
for w in vim.windows: | |
print w | |
def vim_current_demo(): | |
print vim.current.line | |
print vim.current.buffer | |
print vim.current.window | |
print vim.current.range | |
def cb(): | |
return vim.current.buffer | |
def vim_append(lines): | |
vim.current.buffer.append(lines) | |
def delete_selected(): | |
del vim.current.range[:] | |
def selected_text(): | |
vim.current.range | |
def prompt(ask = 'input', mode = 'cui'): | |
if mode == 'cui': | |
input = vim_eval("input('%s')" % ask) | |
else: | |
input = vim_eval("inputdialog('%s')" % ask) | |
return input | |
def surround_text(header=None, footer=None): | |
"""Surround selected text with header and footer """ | |
# header, footer): | |
if not header: header = prompt("header? :") | |
if not footer: footer = prompt("footer? :") | |
cr = vim_get_range() | |
new_text = [header] + cr[:] + [footer] | |
cr[:] = None | |
insert_text(cr.start,new_text) | |
def translate_selection(f): | |
cr = vim_get_range() | |
new_text = [ f(line) for line in cr[:]] | |
cr[:] = None | |
insert_text(cr.start,new_text) | |
def html_escape_selection(): | |
"""HTML escape for selection""" | |
translate_selection(html_escape) | |
def html_unescape_selection(): | |
"""HTML unescape for selection""" | |
translate_selection(html_unescape) | |
def upper_selection(): | |
"""選択範囲を大文字にする。""" | |
translate_selection(lambda x: x.upper() ) | |
def lower_selection(): | |
"""選択範囲を小文字にする。""" | |
translate_selection(lambda x: x.lower() ) | |
def commentify_selection(): | |
"""選択範囲をコメントにする。""" | |
surround_text('"""','"""') | |
def methodize_selection(): | |
"""選択範囲を method にする。""" | |
surround_text(None,'') | |
def insert_text(num,line): | |
cb().range(num,num).append(line) | |
def edit_tmp(file="/tmp/hogebuffer", how="split"): | |
"""open temporary file """ | |
edit(file, how) | |
vim_cmd("setlocal bufhidden=delete") | |
vim_cmd("setlocal buftype=nofile") | |
vim_cmd("setlocal noswapfile") | |
vim_cmd("setlocal nobuflisted") | |
vim_cmd("setlocal modifiable") | |
vim_range = -1 | |
def vim_set_range(): | |
global vim_range | |
vim_range = vim.current.range | |
def vim_get_range(): | |
global vim_range | |
return vim_range | |
def vim_reset_range(): | |
global vim_range | |
vim_range = -1 | |
def vim_prompt(): | |
vim_cmd('echo') | |
vim_cmd('redraw') | |
def vim_cmd(vim_ex_command): | |
vim.command(vim_ex_command) | |
def edit(file, how="split"): | |
cmd = how + " " + file | |
vim_cmd(cmd) | |
def analyze_code(how="vsplit"): | |
tmp_xml = "/tmp/output.xml" | |
tmp_py = "/tmp/output.py" | |
src = cb().name | |
tchecker_cmd = "tchecker.py -o " + tmp_xml + " " + src | |
annot_cmd = "annot.py " + tmp_xml + " " + src | |
os.system(tchecker_cmd) | |
ret = os.popen(annot_cmd).read().split("\n") | |
edit(tmp_py, how) | |
clear_buffer() | |
insert_text(0, ret) | |
def clear_buffer(): | |
"""Erase buffers contents""" | |
del cb()[:] | |
def run_ipython(): | |
"""Load current buffer to ipython""" | |
cmd = "ruby $iterm.write('ipython %s')" % cb().name | |
vim_cmd(cmd) | |
def build_command_help(cmd_list): | |
list = [ " %-30s %-10s" % (cmd.__name__, cmd.__doc__) for cmd in cmd_list] | |
list[0] = '>' + list[0][1:] | |
return list | |
def vimpy_setup_key(): | |
lower_alpha = "abcdefghijklmnopqrstuvwxyz" | |
upper_alpha = "abcdefghijklmnopqrstuvwxyz".upper() | |
numbers = "0123456789" | |
punctuation = '<>`@#~!"$%&/()=+*-_.,;:?\\\'{}[] ' # and space | |
chars = lower_alpha + upper_alpha + numbers + punctuation | |
vim_cmd("setlocal timeout") | |
vim_cmd("setlocal timeoutlen=0") | |
vim_cmd("setlocal nonumber") | |
fmt = "nnoremap <silent> <buffer> <Char-%s> :python pressed(%s)<CR>" | |
list = [ fmt % (ord(char), ord(char)) for char in chars ] | |
# for cmd in list: print cmd | |
for cmd in list: vim_cmd(cmd) | |
fmt = "nnoremap <silent> <buffer> %s :python pressed_special('%s')<CR>" | |
special_keys = { | |
'Backspace' : '<BS>', | |
'Backspace' : '<C-h>', | |
'AcceptSelection' : '<CR>', | |
'Cancel' : '<C-c>', | |
'Cancel' : '<C-g>', | |
'NextLine' : '<C-n>', | |
'PreviousLine' : '<C-p>', | |
'ClearLine' : '<C-u>', | |
'DeleteBackwordWord' : '<C-w>', | |
} | |
list = [ fmt % (key, func) for func, key in special_keys.items()] | |
for cmd in list: vim_cmd(cmd) | |
current_input = "" | |
# Prompt handling mini function | |
def delete_char(): | |
global current_input | |
current_input = current_input[:-1] | |
update_candidate() | |
update_inputline() | |
def cancel_vimpy(): | |
clear_line() | |
vim_cmd("bdelete!") | |
vim_cmd("wincmd p") | |
vim_cmd("set timeoutlen=1000") | |
def next_line(): normal('j') | |
def previous_line(): normal('k') | |
def clear_line(): | |
global current_input | |
current_input = "" | |
update() | |
def delete_backward_word(): | |
global current_input | |
current_input = ' '.join(current_input.split()[:-1]) | |
update() | |
def do_action(): | |
"""Docstring for select_command""" | |
selected_cmd = vim_eval('expand("<cword>")') | |
clear_line() | |
vim_cmd("bdelete!") | |
vim_cmd("wincmd p") | |
vim_cmd("redraw!") | |
if not selected_cmd == 'NO': let = eval(selected_cmd + "()") | |
# vim_cmd("echo ") | |
print_result(let) | |
# vim_cmd("redraw!") | |
vim_cmd("set timeoutlen=1000") | |
special_keys_func = {} | |
special_keys_func['Backspace'] = delete_char | |
special_keys_func['Cancel'] = cancel_vimpy | |
special_keys_func['NextLine'] = next_line | |
special_keys_func['PreviousLine'] = previous_line | |
special_keys_func['AcceptSelection'] = do_action | |
special_keys_func['ClearLine'] = clear_line | |
special_keys_func['DeleteBackwordWord'] = delete_backward_word | |
def vim_py(): | |
global cmd_list | |
vim_set_range() | |
split_location = 'botright' | |
split_command = "silent! %s 10split" % split_location | |
edit_tmp("/tmp/cmdbuffer", split_command) | |
vim_cmd("syn match CommandDesc ' .*$'") | |
vim_cmd("syn match SelctedLine '> .*$'") | |
vim_cmd("syn match NotFound 'NO MATCH'") | |
vim_cmd('hi def link CommandDesc Function') | |
vim_cmd('hi def link NotFound WarningMsg') | |
vim_cmd('hi SelctedLine guibg=#293739') | |
vim_cmd('setlocal nocursorline') | |
vimpy_setup_key() | |
insert_text(0, build_command_help(cmd_list)) | |
vim_set_winheight(len(cmd_list)) | |
update_inputline() | |
def update(): | |
update_candidate() | |
update_inputline() | |
def pressed(key): | |
global current_input | |
input_key = chr(key) | |
current_input += input_key | |
update() | |
def pressed_special(key): | |
global special_keys_func | |
special_keys_func[key]() | |
def update_inputline(): | |
global current_input | |
vim_cmd('redraw') | |
ary = [ ('Comment', ">> "), ('Type', current_input), ('Function', '|') ] | |
for highight, text in ary: | |
vim_cmd("echohl " + highight) | |
vim_cmd("echon '%s'" % text) | |
vim_cmd('echohl None') | |
def print_result(arg): | |
vim_cmd('redraw') | |
ary = [ ('Comment', "[result] "), ('Directory', arg) ] | |
for highight, text in ary: | |
vim_cmd("echohl " + highight) | |
vim_cmd("echon '%s'" % text) | |
vim_cmd('echohl None') | |
def vim_set_winheight(num): | |
vim.current.window.height = num | |
def build_candidate(source, ptns): | |
if not len(ptns): | |
return source | |
result = [] | |
first_match = 8; middle_match = 5 | |
score = {} | |
for cmd in source: score[cmd] = 0 | |
limit = 0 | |
for ptn in ptns.split(): | |
limit += middle_match | |
search_first = "%s.*" % ptn | |
search_middle = ".*%s.*" % ptn | |
for cmd in source: | |
if re.match(search_first, cmd.__name__): | |
score[cmd] += first_match | |
if re.match(search_middle, cmd.__name__): | |
score[cmd] += middle_match | |
l = [ (key, val) for (key,val) in score.items() ] | |
result = [ (cmd ,score) for cmd, score in sorted(l, lambda x,y: cmp(-x[1], -y[1])) ] | |
return [ cmd for cmd, score in filter(lambda x: x[1] >= limit, result) ] | |
def update_candidate(): | |
global current_input | |
global cmd_list | |
clear_buffer() | |
search_pattern = '.*' + current_input + '.*' | |
candidate = build_candidate(cmd_list, current_input) | |
if len(candidate) == 0: | |
candidate = ['NO MATCH'] | |
else: | |
candidate = build_command_help(candidate) | |
insert_text(0, candidate) | |
vim_set_winheight(len(candidate)) | |
html_escape_table = { | |
"&": "&", | |
">": ">", | |
"<": "<", | |
} | |
def html_escape(text): | |
return "".join(html_escape_table.get(c,c) for c in text) | |
def html_unescape(s): | |
s = s.replace("<", "<") | |
s = s.replace(">", ">") | |
# this has to be last: | |
s = s.replace("&", "&") | |
return s | |
def nerd_tree(): | |
"""NERDTree""" | |
cmd = "NERDTree %s" % buffer_dir() | |
vim_cmd(cmd) | |
def buffer_dir(): | |
return os.path.dirname(cb().name) | |
def win_maximize_height(): | |
"""Maximize window height""" | |
vim_cmd('wincmd _') | |
def win_maximize_width(): | |
"""Maximize window width""" | |
vim_cmd('wincmd |') | |
def win_equalize(): | |
"""Equqlize window width""" | |
vim_cmd('wincmd =') | |
def translate_english_to_japanese(): | |
"""選択範囲を日本語に翻訳""" | |
#[TODO] implement via google translate-api | |
def e2j(s): | |
s = s.replace("apple", "リンゴ") | |
s = s.replace("orange", "オレンジ") | |
return s | |
translate_selection(html_escape) | |
def pwd(): | |
"""Print working directory""" | |
return vim_eval('getcwd()') | |
cmd_list = [ | |
select_all_buffer, buffer_length, clear_buffer, | |
edit_tmp, surround_text, run_ipython, commentify_selection, | |
upper_selection, lower_selection, html_escape_selection, | |
html_unescape_selection, nerd_tree, pwd, | |
win_maximize_height, | |
win_maximize_width, | |
win_equalize, | |
methodize_selection, | |
translate_english_to_japanese, | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment