Created
January 25, 2024 10:57
-
-
Save niyarin/57876c5c12b0cf527cdc2e2783c3c062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"if exists('g:loaded_coding_time_metaprogramming') | |
" finish | |
"endif | |
"let g:loaded_coding_time_metaprogramming = 1 | |
function! CheckSexp(exp) abort | |
return system("bb '(try (do (read-string \"" . a:exp . "\") 1) (catch Exception ex 0))'") | |
endfunction | |
function! OpenMacroWindow() abort | |
"let winid = popup_create("Hello world popup!!!", {}) | |
"let selected = inputlist(['aaaa:','bbbb','cccc']) | |
let g:from_buffer_id = bufnr() | |
vnew | |
let l:insert_code = "(->> <INPUT>)" | |
execute ":normal i" . insert_code | |
set filetype=clojure | |
let g:to_buffer_id = bufnr() | |
endfunction | |
"ユーザ定義関数は、大文字から始めるか、s:を付けないといけない。 | |
"abortをつけるとエラーが生じるとそのばで終了する | |
function! Metaprogram() range | |
let l:tmp_z = @z | |
"noautocmd normal! gv"zy | |
noautocmd normal! gv"zd | |
let g:c_pos = getpos(".") | |
let result = @z | |
"execute 'noautocmd normal! a ' . result . "\<esc>" | |
let g:target_s_exp = result | |
let l:is_sexp = CheckSexp(result) | |
if is_sexp | |
call OpenMacroWindow() | |
endif | |
let @z = l:tmp_z | |
endfunction | |
function! RunMacro() abort | |
if bufnr() == g:to_buffer_id | |
let l:coding_lines = getline(1,'$') | |
let l:coding_script = join(l:coding_lines, " ") | |
"文字列リテラルを考慮しないといけない | |
let l:run_code = substitute(l:coding_script,'<INPUT>', "\\'" . g:target_s_exp, 'g') | |
let l:bb_command = "bb " . "\"" . l:run_code . "\"" | |
let g:expanded_s_exp = system(l:bb_command) | |
bd! | |
let g:to_buffer_id = -1 | |
call win_gotoid(win_findbuf(g:from_buffer_id)[0]) | |
call setpos(".", g:c_pos) | |
execute ":normal i" . g:expanded_s_exp | |
endif | |
endfunction | |
let g:to_buffer_id = -1 | |
let g:from_buffer_id = -1 | |
let g:target_s_exp = "" | |
let g:expanded_s_exp = "" | |
let g:c_pos = [1,1] | |
"頭に-rangeをつけると範囲指定のコマンドがつくれる | |
command! -range Metapro call Metaprogram() | |
vnoremap <C-m> :Metapro<Enter> | |
"auto BufLeave * call RunMacro() | |
auto WinLeave * call RunMacro() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment