./plugin/nobin.vim 0000644 0000765 0000024 00000012572 13671353742 015530 0 ustar anonymous staff 0000000 0000000 " Copyright (C) 2020 Xvezda
"
" MIT License
"
" Use of this source code is governed by an MIT-style
" license that can be found in the LICENSE file or at
" https://opensource.org/licenses/MIT.
"
" Location: plugin/nobin.vim
" Maintainer: Xvezda
let s:save_cpo = &cpo
set cpo&vim
" Source guard
if exists('g:loaded_nobin')
let &cpo = s:save_cpo
unlet s:save_cpo
finish
endif
let g:loaded_nobin = 1
" Include veast library
runtime! autoload/veast.vim
" Settings
if !exists('g:nobin_well_known_files')
let g:nobin_well_known_files = ['\.exe$', '\.o$']
else
if type(g:nobin_well_known_files) == type("")
let g:nobin_well_known_files = add([], g:nobin_well_known_files)
elseif type(g:nobin_well_known_files) != type([])
throw '`g:nobin_well_known_files` must be type of `string` or `list`'
endif
endif
let g:nobin_well_known_files = map(g:nobin_well_known_files,
\ '"\\m\\C" . v:val')
if !exists('g:nobin_except_files')
let g:nobin_except_files = ['\.ba\(k\|ckup\)$']
else
if type(g:nobin_except_files) == type("")
let g:nobin_except_files = add([], g:nobin_except_files)
elseif type(g:nobin_except_files) != type([])
throw '`g:nobin_except_files` must be type of `string` or `list`'
endif
endif
let g:nobin_except_files = map(g:nobin_except_files,
\ '"\\m\\C" . v:val')
" Helpers {{{1
function! s:getch() abort
return nr2char(getchar())
endfunction
function! s:match_bool(haystack, needle) abort
if match(a:haystack, a:needle) != -1
return 1
endif
return 0
endfunction
" 1}}}
function! s:echo_multiline(...) abort
" Keep variables to restore
let orig_shortmess = &shortmess
set shortmess=a
let orig_cmdheight = &cmdheight
let &cmdheight = 2
for i in range(a:0)
echo a:000[i]
endfor
" Restore
let &shortmess = orig_shortmess
let &cmdheight = orig_cmdheight
endfunction
function! s:silent_edit(filepath) abort
execute 'silent! :e ' . a:filepath
endfunction
function! nobin#find_source() abort
" Only if filetype is empty
if empty(&ft)
" Prevent double execution
if exists('b:inited_nobin')
return
endif
let b:inited_nobin = 1
" Get fullpath of opened file
let filepath = expand('%:p')
" Get only filename from the path
let filename = fnamemodify(filepath, ':t')
" Pass if file not opened or not executable and not in well known list
if empty(filepath)
\ || (!executable(filepath)
\ && !veast#some(map(g:nobin_well_known_files,
\ 's:match_bool(filename, v:val)')))
return
endif
" On windows, executable should contains `exe` or no extension
if has('win32') || has('win32unix') || executable('wslpath')
if matchend(filename, '\.exe') == -1 && match(filename, '\.') != -1
return
endif
endif
" If trailing dot not acceptable
if !exists('g:nobin_accept_trailing_dot') && filereadable(filepath)
return
endif
" All possible filenames
let fname_comb = [filename]
" Cutoff extension
let tmp_filename = filename
while 1
let tmp_filename = strpart(tmp_filename, 0, strridx(tmp_filename, '.'))
call add(fname_comb, tmp_filename)
if !s:match_bool(tmp_filename, '\.')
call add(fname_comb, tmp_filename)
break
endif
endwhile
let fname_comb = filter(fname_comb, 'v:val !=# ""')
let filelist = []
" Get all filenames, which has extension
for fname in fname_comb
call extend(filelist,
\ map(glob(fnamemodify(filepath, ':h') . '/' . fname . '.*', 1, 1),
\ 'fnamemodify(v:val, ":t")'))
endfor
" If there is no file with extension, then finish
if empty(filelist)
return
endif
" Skip filename in except patterns, even its on candidate
while 1
try
let target_file = remove(filelist, 0)
catch /^Vim\%((\a\+)\)\=:E684/ " E684 -> list index out of range
return
endtry
if !exists('g:nobin_except_files')
break
endif
let flag = 0
for pattern in g:nobin_except_files
if s:match_bool(target_file, pattern)
let flag = 1
break
endif
endfor
if flag
continue
endif
unlet flag
break
endwhile
if filename ==? target_file || target_file ==# '.'
return
endif
let target_filepath = fnamemodify(filepath, ':h')
\ . '/' . target_file
if !exists('g:nobin_always_yes')
let msg = "Seems like you accidentally opened "
\ . "executable rather than source code.\n"
\ . "Would you like to open following file instead?\n"
\ .'"' . target_file . '"'
if has('nvim')
" Get input from user
let select = confirm(msg, "&Yes\n&No", 2)
if select == 1
call s:silent_edit(target_filepath)
endif
else
echo msg . " [Y/n]: "
let select = s:getch()
if select ==? 'Y'
call s:silent_edit(target_filepath)
endif
endif
" Tiny hack to clean command line
echo ''
redraw!
else
call s:silent_edit(target_filepath)
endif
endif
endfunction
call nobin#find_source()
augroup nobin_init
autocmd!
autocmd BufEnter * call nobin#find_source() | execute "filetype detect"
augroup END
let &cpo = s:save_cpo
unlet s:save_cpo
" vim:set sts=2
./autoload/veast.vim 0000644 0000765 0000024 00000007406 13671205235 016050 0 ustar anonymous staff 0000000 0000000 " Copyright (C) 2020 Xvezda
"
" MIT License
"
" Use of this source code is governed by an MIT-style
" license that can be found in the LICENSE file or at
" https://opensource.org/licenses/MIT.
"
" Location: autoload/veast.vim
" Maintainer: Xvezda
" Last Change: 2020 Jan 11
" Source guard
if exists('g:loaded_veast')
finish
endif
let g:loaded_veast = 1
function! s:is_list(expr) abort
return type(a:expr) == type([])
endfunction
function! s:is_dict(expr) abort
return type(a:expr) == type({})
endfunction
function! s:is_str(expr) abort
return type(a:expr) == type("")
endfunction
function! s:is_func(expr) abort
return type(a:expr) == type(function("tr"))
endfunction
function! s:iter(expr) abort
if s:is_list(a:expr)
return a:expr
elseif s:is_dict(a:expr)
return values(a:expr)
elseif s:is_str(a:expr)
return split(a:expr, '\zs')
endif
throw 'argument `expr` is not iterable type'
endfunction
function! veast#every(...) abort
for i in range(a:0)
let expr = a:000[i]
if s:is_list(expr)
for subexpr in expr
if !veast#every(subexpr)
return 0
endif
endfor
elseif s:is_str(expr)
if !eval(expr)
return 0
endif
elseif s:is_dict(expr)
throw 'argument `expr` cannot be dictionary type'
else
if !expr
return 0
endif
endif
endfor
return 1
endfunction
function! veast#some(...) abort
for i in range(a:0)
let expr = a:000[i]
if s:is_list(expr)
for subexpr in expr
if veast#some(subexpr)
return 1
endif
endfor
elseif s:is_str(expr)
if eval(expr)
return 1
endif
elseif s:is_dict(expr)
throw 'argument `expr` cannot be dictionary type'
else
if expr
return 1
endif
endif
endfor
return 0
endfunction
function! veast#each(iter, expr) abort
let ret = []
for val in s:iter(a:iter)
call add(ret, eval(a:expr))
endfor
return ret
endfunction
function! veast#chunk(arr, ...) abort
let ret = []
" a:0 -> Number of va_args
if a:0 == 1
let size = a:000[0] " a:000 -> va_args only"
else
let size = 1
endif
let tmp = []
let size_cpy = size
for item in s:iter(a:arr)
if size_cpy > 0
call add(tmp, item)
else
call add(ret, tmp)
let tmp = [item]
let size_cpy = size
endif
let size_cpy -= 1
endfor
if !empty(tmp)
call add(ret, tmp)
endif
return ret
endfunction
function! veast#compact(arr) abort
if s:is_list(a:arr)
let ret = []
for item in a:arr
let flag = 0
if s:is_str(item) && strlen(item)
let flag = 1
elseif (s:is_list(item) || s:is_dict(item)) && len(item)
let flag = 1
elseif item
let flag = 1
endif
if flag
call add(ret, item)
endif
endfor
return ret
else
throw 'argument `arr` must be type of list'
endif
endfunction
function! veast#diff(a, b) abort
let a_cpy = deepcopy(a:a)
for b_item in s:iter(a:b)
let idx = index(a_cpy, b_item)
if idx == -1
continue
endif
call remove(a_cpy, idx)
endfor
return a_cpy
endfunction
function! veast#concat(...) abort
let ret = []
for i in range(a:0)
let item = a:000[i]
if type(item) == type([])
" Alias
let items = item
unlet item
for item in items
call add(ret, item)
endfor
else
call add(ret, item)
endif
endfor
return ret
endfunction
function! veast#drop(arr, ...) abort
if a:0 == 1
let cnt = a:000[0]
else
let cnt = 1
endif
let cpy = deepcopy(a:arr)
for _ in range(cnt)
if empty(cpy)
break
endif
call remove(cpy, 0)
endfor
return cpy
endfunction
" vim:sts=2
./Makefile 0000644 0000765 0000024 00000000575 13671205234 014040 0 ustar anonymous staff 0000000 0000000 VIMRC_PATH = $(shell vim -u NONE -n -es --noplugin \
-c 'verbose echo get(split(&rtp, ","), 0)' -c 'qall' 2>&1)
all: install
install:
mkdir -p $(VIMRC_PATH)/autoload
mkdir -p $(VIMRC_PATH)/plugin
cp autoload/veast.vim $(VIMRC_PATH)/autoload/
cp plugin/nobin.vim $(VIMRC_PATH)/plugin/
clean:
rm -f $(VIMRC_PATH)/autoload/veast.vim
rm -f $(VIMRC_PATH)/plugin/nobin.vim
./vipers.json 0000644 0000765 0000024 00000000365 13671426174 014610 0 ustar anonymous staff 0000000 0000000 {
"name": "vim-nobin",
"author": "Xvezda ",
"license": "MIT",
"type": "utility",
"required": "7.0",
"version": "1.0",
"summary": "Plugin to load original source code instead of executable binary."
}
./LICENSE 0000644 0000765 0000024 00000002047 13671205234 013401 0 ustar anonymous staff 0000000 0000000 MIT License
Copyright (c) 2020 Xvezda
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
./README.md 0000644 0000765 0000024 00000003063 13671205737 013662 0 ustar anonymous staff 0000000 0000000 # nobin.vim
![Demo](https://gist.githubusercontent.com/Xvezda/ee6613c4ae2ce743b445f2cfc6e837b2/raw/17ceb993c092c12207d0a0f12a62ad763723af61/vim-nobin.gif)
I made this plugin because of auto-completion on shell environment always lead me to open a binary, not a source code.
`gcc test.c -o test`
In this case, there are two files in the same directory. `test.c` and `test`.
`vim t`
Now, if I press tab to edit `test.c` again, `test` will appears.
I need to press tab again to make `test` to `test.c`
But most of times, I accidently open `test` binary instead.
It's way too frustrating. :(
This plugin will automatically finds original source code of executable binary, which you opened by mistake. :)
## Installation
Using [vim-plug](https://github.com/junegunn/vim-plug):
`Plug 'Xvezda/vim-nobin'`
or
`make install`
## Global options
| Flag | Description |
|-------------------------------|---------------------------------------------------------- |
| `g:nobin_always_yes` | Automatically select [y]es on select screen |
| `g:nobin_well_known_files` | Regular expression list for well known binary patterns |
| `g:nobin_accept_trailing_dot` | Accept trailing dot file name (Useful when you type and tab completion including dot) |
| `g:nobin_except_files` | Exception patterns of non-binary files |