Skip to content

Instantly share code, notes, and snippets.

@hwayne
Created October 27, 2021 21:09
Show Gist options
  • Save hwayne/8dc0f20ca76f46f45b608046e98f0dac to your computer and use it in GitHub Desktop.
Save hwayne/8dc0f20ca76f46f45b608046e98f0dac to your computer and use it in GitHub Desktop.
My wonky vimscript functions
" TODO: MAKE THIS ALL USE AUTOLOAD
" Macro Handling {{{
let s:macro_file = g:exo . 'macros.json'
function! s:macro_list(ArgLead, CmdLine, CursorPos)
let macros = json_decode(readfile(s:macro_file))
return join(keys(macros), "\n")
endfunction
function! s:GetMacro(macro_name)
let macros = json_decode(readfile(s:macro_file))
echom macros[a:macro_name]
endfunction
command! -nargs=1 -complete=custom,s:macro_list GetMacro call s:GetMacro(<f-args>)
function! s:SetMacro(macro_name, reg)
let macros = json_decode(readfile(s:macro_file))
call setreg(a:reg, macros[a:macro_name])
endfunction
command! -nargs=+ -complete=custom,s:macro_list SetMacro call s:SetMacro(<f-args>)
function! s:SaveMacro(macro_name, reg)
let macros = json_decode(readfile(s:macro_file))
let macros[a:macro_name] = getreg(a:reg)
" Avoid clobbering the file if invalid
let s = json_encode(macros)
if s !=? ""
call writefile([s], s:macro_file)
endif
endfunction
command! -nargs=+ SaveMacro call s:SaveMacro(<f-args>)
" }}}
" Post Inserter {{{
let s:post_file = 'D:\website\data\posts.json'
function! PostList(ArgLead, CmdLine, CursorPos)
let bookmarks = json_decode(readfile(s:post_file))
return join(keys(bookmarks), "\n")
endfunction
function! g:GetPost(...)
let posts = json_decode(readfile(s:post_file))
if a:0 == 1
let @+ = posts[a:1]
else
for key in keys(posts)
echo key . ": ". posts[key]
endfor
endif
endfunction
command! -nargs=? -complete=custom,PostList Po call g:GetPost(<f-args>)
" Bookmarks {{{
let s:bookmark_file = g:exo . 'bookmarks.json'
function! s:bookmark_list(ArgLead, CmdLine, CursorPos)
let bookmarks = json_decode(readfile(s:bookmark_file))
return join(keys(bookmarks), "\n")
endfunction
function! s:GetBookmark(...)
let bookmarks = json_decode(readfile(s:bookmark_file))
if a:0 == 1
echom bookmarks[a:1]
else
for key in keys(bookmarks)
echo key . ": ". bookmarks[key]
endfor
endif
endfunction
command! -nargs=? -complete=custom,s:bookmark_list Bookmarks call s:GetBookmark(<f-args>)
function! s:GotoBookmark(bookmark_name)
let bookmarks = json_decode(readfile(s:bookmark_file))
execute "e " . bookmarks[a:bookmark_name]
endfunction
command! -nargs=1 -complete=custom,s:bookmark_list To call s:GotoBookmark(<f-args>)
function! s:SaveBookmark(bookmark_name)
set shellslash "to make windows play nice
let bookmarks = json_decode(readfile(s:bookmark_file))
let bookmarks[a:bookmark_name] = expand('%:p')
" Avoid clobbering the file if invalid
let s = json_encode(bookmarks)
if s !=? ""
call writefile([s], s:bookmark_file)
endif
"
set noshellslash
endfunction
command! -nargs=1 -complete=custom,s:bookmark_list SaveBookmark call s:SaveBookmark(<f-args>)
"STATUSLINE STUFF
" We need to do it as a normal mode injection
" Because %! uses the active buffer, not the local one
" IDEA: what about using let &statusline?
function! SetStatus(statusline)
exe "setlocal statusline=" . a:statusline
endfunction
" visual_words_or_all
" Used in ftplugin/markdown.vim
" Have to make global because statuslines are weird
" Can't use <SID> for some reason???
function! g:Exo_vwal()
let s:worddict = wordcount()
if has_key(s:worddict, "visual_words")
return s:worddict["visual_words"]
else
return s:worddict["words"]
endif
endfunction
function! GetSynStack()
" todo make this a lambda
echo map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")')
endfunction
com! GetSyn call GetSynStack()
" This can probably be luafied
function! ToggleConcealLevel()
if &conceallevel == 0
setlocal conceallevel=2
else
setlocal conceallevel=0
endif
endfunction
nnoremap <silent> U :call ToggleConcealLevel()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment