2014-01-29 00:29:23 -05:00
|
|
|
scriptencoding utf-8
|
|
|
|
let s:save_cpo = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
|
|
|
|
|
|
|
function! s:_vital_loaded(V)
|
|
|
|
let s:V = a:V
|
2014-02-09 07:21:26 -05:00
|
|
|
let s:String = s:V.import("Over.String")
|
|
|
|
let s:Signals = s:V.import("Over.Signals")
|
2014-02-11 23:31:44 -05:00
|
|
|
let s:Module = s:V.import("Over.Commandline.Modules")
|
2014-02-09 07:21:26 -05:00
|
|
|
let s:base.variables.modules = s:Signals.make()
|
|
|
|
function! s:base.variables.modules.get_slot(val)
|
|
|
|
return a:val.slot.module
|
|
|
|
endfunction
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:_vital_depends()
|
2014-02-09 07:21:26 -05:00
|
|
|
return [
|
|
|
|
\ "Over.String",
|
|
|
|
\ "Over.Signals",
|
2014-02-11 23:31:44 -05:00
|
|
|
\ "Over.Commandline.Modules",
|
|
|
|
\ ]
|
2014-02-04 23:32:29 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-11 23:31:44 -05:00
|
|
|
function! s:make(...)
|
|
|
|
let result = deepcopy(s:base)
|
2014-02-17 06:44:26 -05:00
|
|
|
let result.set_prompt(get(a:, 1, ":"))
|
2014-02-09 07:21:26 -05:00
|
|
|
call result.connect(result, "_")
|
2014-01-30 01:31:25 -05:00
|
|
|
return result
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-11 23:31:44 -05:00
|
|
|
function! s:make_plain()
|
|
|
|
return deepcpy(s:base)
|
2014-01-30 01:31:25 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-01-29 00:29:23 -05:00
|
|
|
|
|
|
|
let s:base = {
|
|
|
|
\ "line" : {},
|
|
|
|
\ "variables" : {
|
2014-02-17 06:44:26 -05:00
|
|
|
\ "prompt" : "",
|
2014-01-29 00:29:23 -05:00
|
|
|
\ "char" : "",
|
|
|
|
\ "input" : "",
|
2014-02-04 23:32:29 -05:00
|
|
|
\ "tap_key" : "",
|
2014-01-30 01:31:25 -05:00
|
|
|
\ "exit" : 0,
|
2014-02-04 23:32:29 -05:00
|
|
|
\ "keymapping" : {},
|
2014-01-29 00:29:23 -05:00
|
|
|
\ },
|
|
|
|
\ "highlights" : {
|
2014-01-30 01:31:25 -05:00
|
|
|
\ "prompt" : "NONE",
|
2014-02-11 23:31:44 -05:00
|
|
|
\ "cursor" : "VitalOverCommandLineCursor",
|
|
|
|
\ "cursor_on" : "VitalOverCommandLineCursorOn",
|
|
|
|
\ "cursor_insert" : "VitalOverCommandLineOnCursor",
|
2014-01-29 00:29:23 -05:00
|
|
|
\ },
|
|
|
|
\}
|
|
|
|
|
2014-02-10 01:21:05 -05:00
|
|
|
if exists("s:Signals")
|
|
|
|
let s:base.variables.modules = s:Signals.make()
|
|
|
|
function! s:base.variables.modules.get_slot(val)
|
|
|
|
return a:val.slot.module
|
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
2014-01-29 00:29:23 -05:00
|
|
|
function! s:base.getline()
|
|
|
|
return self.line.str()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.setline(line)
|
|
|
|
return self.line.set(a:line)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.char()
|
|
|
|
return self.variables.char
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-11 23:31:44 -05:00
|
|
|
function! s:base.setchar(char, ...)
|
|
|
|
" 1 の場合は既に設定されていても上書きする
|
|
|
|
" 0 の場合は既に設定されていれば上書きしない
|
|
|
|
let overwrite = get(a:, 1, 1)
|
|
|
|
if overwrite || self.variables.input == self.char()
|
|
|
|
let self.variables.input = a:char
|
|
|
|
endif
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.getpos()
|
|
|
|
return self.line.pos()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.setpos(pos)
|
2014-02-04 23:32:29 -05:00
|
|
|
return self.line.set_pos(a:pos)
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:base.tap_keyinput(key)
|
|
|
|
let self.variables.tap_key = a:key
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:base.untap_keyinput(key)
|
|
|
|
if self.variables.tap_key == a:key
|
|
|
|
let self.variables.tap_key = ""
|
2014-01-29 00:29:23 -05:00
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:base.get_tap_key()
|
|
|
|
return self.variables.tap_key
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.is_input(key, ...)
|
|
|
|
let prekey = get(a:, 1, "")
|
2014-02-04 23:32:29 -05:00
|
|
|
return self.get_tap_key() == prekey
|
|
|
|
\ && self.char() == a:key
|
2014-02-06 09:04:12 -05:00
|
|
|
" \ && self.char() == (prekey . a:key)
|
2014-02-06 05:08:14 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.input_key()
|
|
|
|
return self.variables.input_key
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-17 06:44:26 -05:00
|
|
|
function! s:base.set_prompt(prompt)
|
|
|
|
let self.variables.prompt = a:prompt
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.get_prompt()
|
|
|
|
return self.variables.prompt
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-29 00:29:23 -05:00
|
|
|
function! s:base.insert(word, ...)
|
|
|
|
if a:0
|
|
|
|
call self.line.set(a:1)
|
|
|
|
endif
|
|
|
|
call self.line.input(a:word)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:base.forward()
|
|
|
|
return self.line.forward()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:base.backward()
|
|
|
|
return self.line.backward()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
function! s:base.connect(module, ...)
|
2014-02-04 23:32:29 -05:00
|
|
|
if type(a:module) == type("")
|
2014-02-11 23:31:44 -05:00
|
|
|
return call(self.connect, [s:Module.make(a:module)] + a:000, self)
|
|
|
|
endif
|
|
|
|
if empty(a:module)
|
|
|
|
return
|
2014-02-04 23:32:29 -05:00
|
|
|
endif
|
2014-02-09 07:21:26 -05:00
|
|
|
let name = a:0 > 0 ? a:1 : a:module.name
|
|
|
|
let slot = self.variables.modules.find_first_by("get(v:val.slot, 'name', '') == " . string(name))
|
|
|
|
if empty(slot)
|
|
|
|
call self.variables.modules.connect({ "name" : name, "module" : a:module })
|
|
|
|
else
|
|
|
|
let slot.slot.module = a:module
|
|
|
|
endif
|
|
|
|
" let self.variables.modules[name] = a:module
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
function! s:base.disconnect(name)
|
2014-02-09 07:21:26 -05:00
|
|
|
return self.variables.modules.disconnect_by(
|
|
|
|
\ "get(v:val.slot, 'name', '') == " . string(a:name)
|
|
|
|
\ )
|
|
|
|
" unlet self.variables.modules[a:name]
|
2014-01-30 01:31:25 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-11 23:31:44 -05:00
|
|
|
function! s:base.get_module(name)
|
|
|
|
let slot = self.variables.modules.find_first_by("get(v:val.slot, 'name', '') == " . string(a:name))
|
|
|
|
return empty(slot) ? {} : slot.slot.module
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-09 07:21:26 -05:00
|
|
|
function! s:base.callevent(event)
|
|
|
|
call self.variables.modules.sort_by("has_key(v:val.slot.module, 'priority') ? v:val.slot.module.priority('" . a:event . "') : 0")
|
|
|
|
return self.variables.modules.call(a:event, [self])
|
|
|
|
" call map(filter(copy(self.variables.modules), "has_key(v:val, a:event)"), "v:val." . a:event . "(self)")
|
|
|
|
endfunction
|
2014-01-29 00:29:23 -05:00
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:base.cmap(lhs, rhs)
|
|
|
|
let self.variables.keymapping[a:lhs] = a:rhs
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.cnoremap(lhs, rhs)
|
|
|
|
let self.variables.keymapping[a:lhs] = {
|
|
|
|
\ "key" : a:rhs,
|
|
|
|
\ "noremap" : 1,
|
|
|
|
\ }
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.cunmap(lhs)
|
|
|
|
unlet self.variables.keymapping[a:lhs]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.keymapping()
|
2014-01-29 00:29:23 -05:00
|
|
|
return {}
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-11 23:31:44 -05:00
|
|
|
function! s:base.execute(...)
|
|
|
|
let command = get(a:, 1, self.getline())
|
|
|
|
call self._execute(command)
|
|
|
|
" execute self.getline()
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.exit(...)
|
2014-01-30 01:31:25 -05:00
|
|
|
let self.variables.exit = 1
|
|
|
|
let self.variables.exit_code = get(a:, 1, 0)
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-06 05:08:14 -05:00
|
|
|
" function! s:base.cancel()
|
|
|
|
" call self.exit(1)
|
|
|
|
" call self._on_cancel()
|
|
|
|
" endfunction
|
2014-01-30 01:31:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
function! s:base.exit_code()
|
|
|
|
return self.variables.exit_code
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:base.hl_cursor_on()
|
2014-02-10 01:21:05 -05:00
|
|
|
if exists("self.variables.old_guicursor")
|
2014-02-11 00:17:04 -05:00
|
|
|
set guicursor&
|
2014-02-10 01:21:05 -05:00
|
|
|
let &guicursor = self.variables.old_guicursor
|
|
|
|
unlet self.variables.old_guicursor
|
2014-02-04 23:32:29 -05:00
|
|
|
endif
|
2014-02-10 01:21:05 -05:00
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
if exists("self.variables.old_t_ve")
|
|
|
|
let &t_ve = self.variables.old_t_ve
|
|
|
|
unlet self.variables.old_t_ve
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.hl_cursor_off()
|
2014-02-10 01:21:05 -05:00
|
|
|
if exists("self.variables.old_t_ve")
|
|
|
|
return
|
2014-02-04 23:32:29 -05:00
|
|
|
endif
|
2014-02-11 23:31:44 -05:00
|
|
|
|
2014-02-10 01:21:05 -05:00
|
|
|
let self.variables.old_guicursor = &guicursor
|
2014-02-11 23:31:44 -05:00
|
|
|
set guicursor=n:block-NONE
|
2014-02-04 23:32:29 -05:00
|
|
|
let self.variables.old_t_ve = &t_ve
|
|
|
|
set t_ve=
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-29 00:29:23 -05:00
|
|
|
function! s:base.start(...)
|
2014-01-30 01:31:25 -05:00
|
|
|
let exit_code = call(self._main, a:000, self)
|
2014-02-11 23:31:44 -05:00
|
|
|
return exit_code
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.__empty(...)
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base.get(...)
|
2014-02-11 23:31:44 -05:00
|
|
|
let Old_execute = self.execute
|
|
|
|
let self.execute = self.__empty
|
|
|
|
try
|
|
|
|
let exit_code = self.start()
|
|
|
|
if exit_code == 0
|
|
|
|
return self.getline()
|
|
|
|
endif
|
|
|
|
finally
|
|
|
|
let self.execute = Old_execute
|
|
|
|
endtry
|
2014-01-29 00:29:23 -05:00
|
|
|
return ""
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base._init()
|
2014-02-04 23:32:29 -05:00
|
|
|
let self.variables.tap_key = ""
|
2014-01-29 00:29:23 -05:00
|
|
|
let self.variables.char = ""
|
|
|
|
let self.variables.input = ""
|
2014-01-30 01:31:25 -05:00
|
|
|
let self.variables.exit = 0
|
|
|
|
let self.variables.exit_code = 1
|
2014-02-10 01:21:05 -05:00
|
|
|
call self.hl_cursor_off()
|
2014-02-11 23:31:44 -05:00
|
|
|
if !hlexists(self.highlights.cursor)
|
|
|
|
execute "highlight link " . self.highlights.cursor . " Cursor"
|
2014-02-10 01:21:05 -05:00
|
|
|
endif
|
2014-02-11 23:31:44 -05:00
|
|
|
if !hlexists(self.highlights.cursor_on)
|
|
|
|
execute "highlight link " . self.highlights.cursor_on . " " . self.highlights.cursor
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
2014-02-11 23:31:44 -05:00
|
|
|
if !hlexists(self.highlights.cursor_insert)
|
|
|
|
execute "highlight " . self.highlights.cursor_insert . " cterm=underline term=underline gui=underline"
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-11 23:31:44 -05:00
|
|
|
function! s:base._execute(command)
|
2014-02-09 07:21:26 -05:00
|
|
|
call self.callevent("on_execute_pre")
|
2014-01-29 00:29:23 -05:00
|
|
|
try
|
2014-02-11 23:31:44 -05:00
|
|
|
execute a:command
|
2014-01-29 00:29:23 -05:00
|
|
|
catch
|
|
|
|
echohl ErrorMsg
|
|
|
|
echo matchstr(v:exception, 'Vim\((\w*)\)\?:\zs.*\ze')
|
|
|
|
echohl None
|
2014-02-09 07:21:26 -05:00
|
|
|
call self.callevent("on_execute_failed")
|
2014-01-29 00:29:23 -05:00
|
|
|
finally
|
2014-02-09 07:21:26 -05:00
|
|
|
call self.callevent("on_execute")
|
2014-01-29 00:29:23 -05:00
|
|
|
endtry
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
function! s:base._main(...)
|
|
|
|
try
|
|
|
|
call self._init()
|
2014-02-04 23:32:29 -05:00
|
|
|
let self.line = deepcopy(s:String.make(get(a:, 1, "")))
|
2014-02-09 07:21:26 -05:00
|
|
|
call self.callevent("on_enter")
|
2014-01-30 01:31:25 -05:00
|
|
|
|
|
|
|
while !self._is_exit()
|
|
|
|
call s:_echo_cmdline(self)
|
|
|
|
|
2014-02-06 05:08:14 -05:00
|
|
|
let self.variables.input_key = s:_getchar()
|
|
|
|
let self.variables.char = s:_unmap(self._get_keymapping(), self.variables.input_key)
|
2014-02-06 09:04:12 -05:00
|
|
|
" let self.variables.char = s:_unmap(self._get_keymapping(), self.get_tap_key() . self.variables.input_key)
|
2014-02-04 23:32:29 -05:00
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
call self.setchar(self.variables.char)
|
|
|
|
|
2014-02-09 07:21:26 -05:00
|
|
|
call self.callevent("on_char_pre")
|
2014-01-30 01:31:25 -05:00
|
|
|
call self.insert(self.variables.input)
|
2014-02-09 07:21:26 -05:00
|
|
|
call self.callevent("on_char")
|
2014-01-30 01:31:25 -05:00
|
|
|
endwhile
|
|
|
|
catch
|
|
|
|
echohl ErrorMsg | echo v:throwpoint . " " . v:exception | echohl None
|
2014-02-04 23:32:29 -05:00
|
|
|
return -1
|
2014-01-30 01:31:25 -05:00
|
|
|
finally
|
|
|
|
call self._finish()
|
2014-02-09 07:21:26 -05:00
|
|
|
call self.callevent("on_leave")
|
2014-01-30 01:31:25 -05:00
|
|
|
endtry
|
|
|
|
return self.exit_code()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-29 00:29:23 -05:00
|
|
|
function! s:base._finish()
|
2014-02-04 23:32:29 -05:00
|
|
|
call self.hl_cursor_on()
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-01-30 01:31:25 -05:00
|
|
|
function! s:_echo_cmdline(cmdline)
|
2014-02-04 23:32:29 -05:00
|
|
|
call s:redraw()
|
2014-01-30 01:31:25 -05:00
|
|
|
execute "echohl" a:cmdline.highlights.prompt
|
2014-02-17 06:44:26 -05:00
|
|
|
echon a:cmdline.get_prompt()
|
2014-01-30 01:31:25 -05:00
|
|
|
echohl NONE
|
|
|
|
echon a:cmdline.backward()
|
|
|
|
if empty(a:cmdline.line.pos_word())
|
|
|
|
execute "echohl" a:cmdline.highlights.cursor
|
|
|
|
echon ' '
|
|
|
|
else
|
2014-02-10 01:21:05 -05:00
|
|
|
execute "echohl" a:cmdline.highlights.cursor_on
|
2014-01-30 01:31:25 -05:00
|
|
|
echon a:cmdline.line.pos_word()
|
|
|
|
endif
|
|
|
|
echohl NONE
|
|
|
|
echon a:cmdline.forward()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
function! s:base._is_exit()
|
|
|
|
return self.variables.exit
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:_as_key_config(config)
|
|
|
|
let base = {
|
|
|
|
\ "noremap" : 0,
|
|
|
|
\ "lock" : 0,
|
|
|
|
\ }
|
|
|
|
return type(a:config) == type({}) ? extend(base, a:config)
|
|
|
|
\ : extend(base, {
|
|
|
|
\ "key" : a:config,
|
|
|
|
\ })
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:_unmap(mapping, key)
|
|
|
|
if !has_key(a:mapping, a:key)
|
|
|
|
return a:key
|
|
|
|
endif
|
|
|
|
let rhs = s:_as_key_config(a:mapping[a:key])
|
|
|
|
let next = s:_as_key_config(get(a:mapping, rhs.key, {}))
|
|
|
|
if rhs.noremap && next.lock == 0
|
|
|
|
return rhs.key
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
2014-02-04 23:32:29 -05:00
|
|
|
return s:_unmap(a:mapping, rhs.key)
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:base._get_keymapping()
|
|
|
|
let result = {}
|
2014-02-09 07:21:26 -05:00
|
|
|
" for module in values(self.variables.modules)
|
|
|
|
for module in self.variables.modules.slots()
|
2014-02-04 23:32:29 -05:00
|
|
|
if has_key(module, "keymapping")
|
2014-02-09 07:21:26 -05:00
|
|
|
if module isnot self
|
|
|
|
call extend(result, module.keymapping(self))
|
|
|
|
endif
|
2014-01-29 00:29:23 -05:00
|
|
|
endif
|
2014-02-04 23:32:29 -05:00
|
|
|
endfor
|
|
|
|
return extend(extend(result, self.variables.keymapping), self.keymapping())
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:redraw()
|
|
|
|
redraw
|
|
|
|
echo ""
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-02-04 23:32:29 -05:00
|
|
|
function! s:_getchar()
|
|
|
|
let char = getchar()
|
|
|
|
return type(char) == type(0) ? nr2char(char) : char
|
2014-01-29 00:29:23 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
let &cpo = s:save_cpo
|
|
|
|
unlet s:save_cpo
|