Skip to content

Add a filter function for g= #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion autoload/scriptease.vim
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ function! s:opfunc(t) abort
return @@
endfunction

function! s:noop(...)
return a:1
endf

function! scriptease#filterop(...) abort
if !a:0
set opfunc=scriptease#filterop
Expand All @@ -234,7 +238,11 @@ function! scriptease#filterop(...) abort
try
set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus
let expr = s:opfunc(a:1)
let @@ = matchstr(expr, '^\_s\+').scriptease#dump(eval(s:gsub(expr,'\n%(\s*\\)=',''))).matchstr(expr, '\_s\+$')
let code = s:function(get(g:, 'scriptease_prefilter', 's:noop'))(expr)
let filter_modified = code != expr
let code = s:gsub(code,'\n%(\s*\\)=','')
let @@ = matchstr(expr, '^\_s\+').scriptease#dump(eval(code)).matchstr(expr, '\_s\+$')
let @@ = s:function(get(g:, 'scriptease_postfilter', 's:noop'))(@@, filter_modified, expr)
if @@ !~# '^\n*$'
normal! gvp
endif
Expand Down
20 changes: 20 additions & 0 deletions doc/scriptease.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ g== replace it with the result. If you press g== on a
{Visual}g= Call |eval()| on the selection and replace it with the
result.

*g:scriptease_prefilter*
*g:scriptease_postfilter*
Define these filter functions to modify selection before it's passed to
|eval()| so you can convert your expressions to vim-compatible ones.
>
function! StripFloatSuffix_Pre(input)
" Strip out f float suffixes.
return substitute(a:input, '\v\C(\d)f>', '\1', 'g')
endf
function! StripFloatSuffix_Post(expr, filter_modified, input)
if a:filter_modified
" We had f suffixes, so restore them.
return substitute(a:expr, '\v\C([0-9.]+)>', '\1f', 'g')
endif
return a:expr
endf
let g:scriptease_prefilter = 'StripFloatSuffix_Pre'
let g:scriptease_postfilter = 'StripFloatSuffix_Post'
<

*scriptease-g!*
g! Old alias for |g=|.

Expand Down