Last active
March 2, 2024 05:49
-
-
Save Konfekt/5748fbe2d72749ab9b2ea8362f703a7e to your computer and use it in GitHub Desktop.
Vim commands to pipe text to aichat and open a chat window
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 executable('aichat') | |
command! -range -nargs=+ -complete=customlist,s:AIChatRoleCompletion AIRole <line1>,<line2>call s:AIRole(<q-args>) | |
function! s:AIRole(instruction) range | |
let instruction = trim(a:instruction) | |
let i = match(instruction . ' ', '\s') | |
let role = instruction[0:i-1] | |
let instruction = trim(instruction[i:-1]) | |
let prompt = !empty(instruction) ? shellescape('Please consider: ' . instruction . '.') : '' | |
" term ++hidden ++open | |
exe a:firstline.','.a:lastline . 'term aichat --role ' . role . ' ' . prompt | |
endfunction | |
command! -range=0 -nargs=+ -complete=customlist,s:AIChatRoleCompletion AIEditRole <line1>,<line2>call s:AIEditRole(<count>, <q-args>) | |
function! s:AIEditRole(uses_range, instruction) range | |
let instruction = trim(a:instruction) | |
let i = match(instruction . ' ', '\s') | |
let role = instruction[0:i-1] | |
let instruction = trim(instruction[i:-1]) | |
let prompt = !empty(instruction) ? shellescape('Please consider: ' . instruction . '.') : '' | |
if a:uses_range | |
exe a:firstline.','.a:lastline . '!aichat --role ' . role . ' ' . prompt | |
else | |
exe 'read! aichat --role ' . role . ' ' . prompt . 'Your input is: ' . getline(line('.'))) | |
endif | |
endfunction | |
command! -range=0 -nargs=1 -complete=customlist,s:AIChatRoleCompletion AIChatRole call s:AIChatRole(<count>, <f-args>) | |
function! s:AIChatRole(uses_range, role) | |
let role = a:role | |
if a:uses_range | |
exe 'tabnew | tab ' . a:firstline.','.a:lastline . 'term ++curwin ++rows=' . &lines . ' ' . 'aichat --role ' . role | |
else | |
exe 'tabnew | tab term ++curwin ++rows=' . &lines . ' ' . 'aichat --role ' . role | |
else | |
endfunction | |
function! s:AIChatRoleCompletion(A,L,P) abort | |
let l:targets = systemlist('aichat --list-roles') | |
return filter(l:targets, 'v:val =~ "^' . a:A . '"') | |
endfunction | |
endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment