這個 funcion 需要上一篇的 Vim funcion:使用 closure compiler 壓縮 js 文件,還有 Vim 的 ruby 支援(其實只是用來 encodeURI ……)
原始碼展開如下,但以後更新會放在 gist: 546828 - [.vimrc] make bookmarklet。
command! -nargs=* Bookmarklet call Bookmarklet(<f-args>)
fun! Bookmarklet(...)
let result = JsCompress(0, 0, '--compilation_level=WHITESPACE_ONLY')
if len(getqflist()) == 0 && strlen(result) > 0
let reuse_win = 0
for winnr in tabpagebuflist(tabpagenr())
if bufname(winnr) == '[Bookmarklet]'
exec winnr . 'wincmd w'
let reuse_win = winnr
endif
endfor
if ! reuse_win
exec 'belowright 5new [Bookmarklet]'
setlocal noswapfile bufhidden=wipe winfixheight filetype=javascript
endif
let result = substitute(result, '[\n]', '', '')
setlocal buftype=
call setline(1, 'javascript:(function(){' . result . '})();')
if has('ruby')
ruby require("uri"); VIM::Buffer.current.line = URI.escape(VIM::Buffer.current.line);
else
echohl WarningMsg | echomsg "No ruby support. Can't do URI encoding." | echohl None
endif
setlocal buftype=nofile
endif
endf