Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions autoload/im_select.vim
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ endfunction
if has('nvim')
let s:ImSetJob = {}

function s:ImSetJob.on_stdout(job_id, data, event) abort
endfunction

function s:ImSetJob.on_stderr(job_id, data, event) abort
endfunction

function s:ImSetJob.wait() abort
call jobwait([self.id])
endfunction
Expand Down Expand Up @@ -82,6 +88,12 @@ if has('nvim')
else
let s:ImSetJob = {}

function s:ImSetJob.out_cb(channel, msg) abort
endfunction

function s:ImSetJob.err_cb(channel, msg) abort
endfunction

function s:ImSetJob.exit_cb(job, status) abort
let self.is_running = 0
endfunction
Expand All @@ -96,6 +108,8 @@ else
let object = copy(s:ImSetJob)
let object.cmd = a:cmd
let object.id = job_start(object.cmd, {
\ 'out_cb': object.out_cb,
\ 'err_cb': object.err_cb,
\ 'exit_cb': object.exit_cb
\ })
let object.is_running = 1
Expand Down Expand Up @@ -185,6 +199,10 @@ endfunction
function! im_select#on_insert_enter() abort
" let s:insert_enter_count += 1
" echomsg 'InsertEnter: ' . s:insert_enter_count . ', mode: ' . mode() . ', event: ' . string(v:event)
if get(g:, 'im_control_enabled', 1) && exists('g:im_control_command')
call im_select#im_control_on_insert_enter()
return
endif
if s:focus_event_enabled
if g:im_select_prev_im != ''
call im_select#set_im(g:im_select_prev_im)
Expand All @@ -204,6 +222,10 @@ endfunction
function! im_select#on_insert_leave() abort
" let s:insert_leave_count += 1
" echomsg 'InsertLeave: ' . s:insert_leave_count . ', mode: ' . mode() . ', event: ' . string(v:event)
if get(g:, 'im_control_enabled', 1) && exists('g:im_control_command')
call im_select#im_control_on_insert_leave()
return
endif
if s:focus_event_enabled
let j = im_select#get_and_set_prev_im('im_select#on_insert_leave_get_im_callback')
endif
Expand All @@ -219,6 +241,10 @@ endfunction
function! im_select#on_focus_gained() abort
" let s:focus_gained_count += 1
" echomsg 'FocusGained: ' . s:focus_gained_count
if get(g:, 'im_control_enabled', 1) && exists('g:im_control_command')
call im_select#im_control_on_focus_gained()
return
endif
if s:focus_event_enabled
if match(mode(), '^\(c\|i\|R\|s\|S\|t\|CTRL\-S\)') < 0
let j = im_select#get_and_set_prev_im('im_select#on_focus_gained_get_im_callback')
Expand All @@ -230,6 +256,9 @@ endfunction
function! im_select#on_focus_lost() abort
" let s:focus_lost_count += 1
" echomsg 'FocusLost: ' . s:focus_lost_count
if get(g:, 'im_control_enabled', 1) && exists('g:im_control_command')
return
endif
if s:focus_event_enabled
if match(mode(), '^\(c\|i\|R\|s\|S\|t\|CTRL\-S\)') < 0
if g:im_select_prev_im != ''
Expand All @@ -242,6 +271,10 @@ function! im_select#on_focus_lost() abort
endfunction

function! im_select#on_vim_leave_pre() abort
if get(g:, 'im_control_enabled', 1) && exists('g:im_control_command')
call im_select#im_control_on_vim_leave_pre()
return
endif
if s:gui
if match(mode(), '^\(c\|i\|R\|s\|S\|t\|CTRL\-S\)') < 0
if g:im_select_prev_im != ''
Expand All @@ -253,4 +286,37 @@ function! im_select#on_vim_leave_pre() abort
endif
endfunction

" ===== im-control mode ===== {{{1

function! im_select#im_control_set_mode(chinese) abort
if a:chinese
let cmd = [g:im_control_command, '-c', 'native']
else
let cmd = [g:im_control_command, '-c', 'alphanumeric']
endif
return s:ImSetJob.new(cmd)
endfunction

function! im_select#im_control_on_insert_enter() abort
call im_select#im_control_set_mode(v:false)
endfunction

function! im_select#im_control_on_insert_leave() abort
if s:focus_event_enabled
call im_select#im_control_set_mode(v:false)
endif
endfunction

function! im_select#im_control_on_focus_gained() abort
if s:focus_event_enabled
if match(mode(), '^\(c\|i\|R\|s\|S\|t\|CTRL\-S\)') < 0
call im_select#im_control_set_mode(v:false)
endif
endif
endfunction

function! im_select#im_control_on_vim_leave_pre() abort
call im_select#im_control_set_mode(v:false)
endfunction

" vim: ts=8 sts=4 sw=4 et
20 changes: 4 additions & 16 deletions plugin/im_select.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,16 @@ endif

let g:im_select_enable_for_gvim = get(g:, 'im_select_enable_for_gvim', 0)

if !has('nvim') && has('gui_running') && (has('win32') || has('win64') || has('gui_mavim')) && !g:im_select_enable_for_gvim
if !has('nvim') && has('gui_running') && has('gui_mavim') && !g:im_select_enable_for_gvim
finish
endif

" OS and IM detection
if !exists('g:im_select_get_im_cmd') || !exists('g:ImSelectSetImCmd')
if has('win32') || has('win64') || has('win32unix') || has('wsl') || $PATH =~ '/mnt/c/WINDOWS'
if !exists('g:im_select_command')
" let cmd = exepath('im-select.exe')
" if cmd == ''
" echohl ErrorMsg | echomsg 'im-select.exe is not found on your system. Please refer to https://github.com/daipeihust/im-select' | echohl None
" finish
" endif
let cmd = 'im-select.exe'
let g:im_select_command = cmd
endif

if !exists('g:im_select_default')
let g:im_select_default = '1033'
endif
let g:im_select_get_im_cmd = [g:im_select_command]
let g:ImSelectSetImCmd = {key -> [g:im_select_command, key]}
let g:im_select_default = '1033'
let g:im_select_get_im_cmd = ['cmd', '/c', 'echo', '1033']
let g:ImSelectSetImCmd = {key -> ['cmd', '/c', 'echo', '1033']}
elseif has('mac') || has('macunix') || has('osx') || has('osxdarwin')
if !exists('g:im_select_command')
" let cmd = exepath('im-select')
Expand Down