2013-09-30 04:19:31 -04:00
|
|
|
" vim: et sw=2 sts=2
|
|
|
|
|
2013-08-19 11:36:16 -04:00
|
|
|
scriptencoding utf-8
|
|
|
|
|
2015-05-20 08:00:39 -04:00
|
|
|
" Function: #escape {{{1
|
|
|
|
function! sy#util#escape(path) abort
|
|
|
|
if exists('+shellslash')
|
|
|
|
let old_ssl = &shellslash
|
|
|
|
if fnamemodify(&shell, ':t') == 'cmd.exe'
|
|
|
|
set noshellslash
|
|
|
|
else
|
|
|
|
set shellslash
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
let path = shellescape(a:path)
|
|
|
|
|
|
|
|
if exists('old_ssl')
|
|
|
|
let &shellslash = old_ssl
|
|
|
|
endif
|
|
|
|
|
|
|
|
return path
|
|
|
|
endfunction
|
|
|
|
|
2014-11-02 09:57:50 -05:00
|
|
|
" Function: #refresh_windows {{{1
|
|
|
|
function! sy#util#refresh_windows() abort
|
2016-12-09 09:17:34 -05:00
|
|
|
if exists('*win_getid')
|
|
|
|
let winid = win_getid()
|
|
|
|
else
|
|
|
|
let winnr = winnr()
|
|
|
|
endif
|
|
|
|
|
2017-03-02 10:46:38 -05:00
|
|
|
if !get(g:, 'signify_cmdwin_active')
|
2019-03-15 11:55:37 -04:00
|
|
|
keepjumps windo if exists('b:sy') | call sy#start() | endif
|
2017-03-02 10:46:38 -05:00
|
|
|
endif
|
2016-12-09 09:17:34 -05:00
|
|
|
|
|
|
|
if exists('winid')
|
|
|
|
call win_gotoid(winid)
|
|
|
|
else
|
|
|
|
execute winnr .'wincmd w'
|
|
|
|
endif
|
2014-11-02 09:57:50 -05:00
|
|
|
endfunction
|
2014-11-05 08:16:44 -05:00
|
|
|
|
|
|
|
" Function: #hunk_text_object {{{1
|
|
|
|
function! sy#util#hunk_text_object(emptylines) abort
|
2018-04-15 16:00:17 -04:00
|
|
|
execute sy#util#return_if_no_changes()
|
2014-11-05 08:16:44 -05:00
|
|
|
|
|
|
|
let lnum = line('.')
|
|
|
|
let hunks = filter(copy(b:sy.hunks), 'v:val.start <= lnum && v:val.end >= lnum')
|
|
|
|
|
|
|
|
if empty(hunks)
|
2018-04-15 16:00:17 -04:00
|
|
|
echomsg 'signify: Here is no hunk.'
|
2014-11-05 08:16:44 -05:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
execute hunks[0].start
|
|
|
|
normal! V
|
|
|
|
|
|
|
|
if a:emptylines
|
|
|
|
let lnum = hunks[0].end
|
|
|
|
while getline(lnum+1) =~ '^$'
|
|
|
|
let lnum += 1
|
|
|
|
endwhile
|
|
|
|
execute lnum
|
|
|
|
else
|
|
|
|
execute hunks[0].end
|
|
|
|
endif
|
|
|
|
endfunction
|
2014-12-30 16:10:43 -05:00
|
|
|
|
2015-07-28 09:17:15 -04:00
|
|
|
" Function: #shell_redirect {{{1
|
|
|
|
function! sy#util#shell_redirect(path) abort
|
|
|
|
" if shellredir contains a %s it is replaced with the path
|
|
|
|
" otherwise, just append it (from :help shellredir:
|
|
|
|
" The name of the temporary file can be represented by '%s' if necessary
|
|
|
|
" (the file name is appended automatically if no %s appears in the value
|
|
|
|
" of this option)
|
|
|
|
if &shellredir =~# '%s'
|
|
|
|
return substitute(&shellredir, '\C%s', a:path, 'g')
|
|
|
|
else
|
|
|
|
return &shellredir .' '. a:path
|
|
|
|
endif
|
|
|
|
endfunction
|
2017-01-18 11:15:15 -05:00
|
|
|
|
|
|
|
" Function: #chdir {{{1
|
|
|
|
function! sy#util#chdir() abort
|
|
|
|
let chdir = haslocaldir()
|
|
|
|
\ ? 'lcd'
|
|
|
|
\ : (exists(':tcd') && haslocaldir(-1, 0)) ? 'tcd' : 'cd'
|
|
|
|
return [getcwd(), chdir]
|
|
|
|
endfunction
|
2018-04-15 16:00:17 -04:00
|
|
|
|
|
|
|
" Function: #has_changes {{{1
|
|
|
|
function! sy#util#return_if_no_changes() abort
|
|
|
|
if !exists('b:sy') || empty(b:sy.hunks)
|
|
|
|
echomsg 'signify: There are no changes.'
|
|
|
|
return 'return'
|
|
|
|
endif
|
|
|
|
return ''
|
|
|
|
endfunction
|
2019-03-28 09:57:52 -04:00
|
|
|
|
|
|
|
" Function: #execute {{{1
|
|
|
|
function! sy#util#execute(cmd) abort
|
|
|
|
let lang = v:lang
|
|
|
|
redir => output
|
|
|
|
silent! execute a:cmd
|
|
|
|
redir END
|
|
|
|
silent! execute 'language message' lang
|
|
|
|
return output
|
|
|
|
endfunction
|
2019-08-09 09:37:44 -04:00
|
|
|
|
2019-08-13 15:16:13 -04:00
|
|
|
let s:popup_window = 0
|
2019-08-09 09:37:44 -04:00
|
|
|
|
2019-08-14 08:15:31 -04:00
|
|
|
" Function: #popup_close {{{1
|
2019-08-13 15:16:13 -04:00
|
|
|
function! sy#util#popup_close() abort
|
|
|
|
if s:popup_window
|
|
|
|
call nvim_win_close(s:popup_window, 1)
|
|
|
|
let s:popup_window = 0
|
2019-08-09 09:37:44 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2019-08-14 08:15:31 -04:00
|
|
|
" Function: #popup_create {{{1
|
2019-08-13 15:32:45 -04:00
|
|
|
function! sy#util#popup_create(hunkdiff) abort
|
2019-08-14 08:15:31 -04:00
|
|
|
let max_height = winheight('%') - winline()
|
2019-08-13 15:16:13 -04:00
|
|
|
let height = len(a:hunkdiff)
|
2019-08-09 09:37:44 -04:00
|
|
|
let height = (height > max_height) ? max_height : height
|
2019-08-14 08:15:31 -04:00
|
|
|
let offset = s:offset()
|
2019-08-13 15:16:13 -04:00
|
|
|
|
|
|
|
if exists('*nvim_open_win')
|
|
|
|
call sy#util#popup_close()
|
|
|
|
let buf = nvim_create_buf(0, 1)
|
|
|
|
call nvim_buf_set_option(buf, 'syntax', 'diff')
|
|
|
|
call nvim_buf_set_lines(buf, 0, -1, 0, a:hunkdiff)
|
2019-08-13 15:48:22 -04:00
|
|
|
let s:popup_window = nvim_open_win(buf, v:false, {
|
2019-08-14 08:15:31 -04:00
|
|
|
\ 'relative': 'win',
|
|
|
|
\ 'row': winline(),
|
|
|
|
\ 'col': offset - 1,
|
|
|
|
\ 'width': winwidth('%') - offset,
|
2019-08-13 15:16:13 -04:00
|
|
|
\ 'height': height,
|
2019-08-13 15:48:22 -04:00
|
|
|
\ })
|
2019-08-13 15:16:13 -04:00
|
|
|
call nvim_win_set_option(s:popup_window, 'cursorline', v:false)
|
2019-08-14 08:15:31 -04:00
|
|
|
call nvim_win_set_option(s:popup_window, 'foldcolumn', 0)
|
2019-08-13 15:16:13 -04:00
|
|
|
call nvim_win_set_option(s:popup_window, 'foldenable', v:false)
|
|
|
|
call nvim_win_set_option(s:popup_window, 'number', v:false)
|
|
|
|
call nvim_win_set_option(s:popup_window, 'relativenumber', v:false)
|
|
|
|
call nvim_win_set_option(s:popup_window, 'wrap', v:true)
|
|
|
|
autocmd CursorMoved * ++once call sy#util#popup_close()
|
|
|
|
elseif exists('*popup_create')
|
|
|
|
let s:popup_window = popup_create(a:hunkdiff, {
|
2019-08-14 08:15:31 -04:00
|
|
|
\ 'line': 'cursor+1',
|
|
|
|
\ 'col': offset,
|
|
|
|
\ 'maxwidth': winwidth('%'),
|
2019-08-13 15:16:13 -04:00
|
|
|
\ 'maxheight': height,
|
|
|
|
\ 'moved': 'any',
|
|
|
|
\ 'zindex': 1000,
|
|
|
|
\ })
|
2019-08-14 08:15:31 -04:00
|
|
|
call setbufvar(winbufnr(s:popup_window), '&syntax', 'diff')
|
2019-08-13 15:16:13 -04:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
return 1
|
|
|
|
endfunction
|
2019-08-14 08:15:31 -04:00
|
|
|
|
|
|
|
" Function: s:offset {{{1
|
|
|
|
function! s:offset() abort
|
|
|
|
let offset = &foldcolumn
|
|
|
|
let offset += 2 " FIXME: Find better way to calculate the sign column width.
|
|
|
|
if &number
|
|
|
|
let l = len(line('$')) + 1
|
|
|
|
let offset += (&numberwidth > l) ? &numberwidth : l
|
|
|
|
elseif &relativenumber
|
|
|
|
let l = len(winheight('%')) + 1
|
|
|
|
let offset += (&numberwidth > l) ? &numberwidth : l
|
|
|
|
endif
|
|
|
|
return offset
|
|
|
|
endfunction
|