" # Utility Functions function! s:refreshView() let bufnr = expand('') " Add a space to input to avoid complaints call system("curl -X PUT -T - http://localhost:8090/ &>/dev/null &", \ ' '.s:bufGetContents(bufnr)) endfu function! s:startDaemon(initialMD) " Add a space to input to avoid complaints call system("instant-markdown-d &>/dev/null &", a:initialMD) endfu function! s:initDict() if !exists('s:buffers') let s:buffers = {} endif endfu function! s:pushBuffer(bufnr) call s:initDict() let s:buffers[a:bufnr] = 1 endfu function! s:popBuffer(bufnr) call s:initDict() call remove(s:buffers, a:bufnr) endfu function! s:killDaemon() call system("curl -s -X DELETE http://localhost:8090/ &>/dev/null &") endfu function! s:bufGetContents(bufnr) return join(getbufline(a:bufnr, 1, "$"), "\n") endfu " I really, really hope there's a better way to do this. fu! s:myBufNr() return str2nr(expand('')) endfu " # Functions called by autocmds " " ## push a new Markdown buffer into the system. " " 1. Track it so we know when to garbage collect the daemon " 2. Start daemon if we're on the first MD buffer. " 3. Initialize changedtickLast, possibly needlessly(?) fu! s:pushMarkdown() let bufnr = s:myBufNr() call s:initDict() if len(s:buffers) == 0 call s:startDaemon(s:bufGetContents(bufnr)) endif call s:pushBuffer(bufnr) let b:changedtickLast = b:changedtick endfu " ## pop a Markdown buffer " " 1. Pop the buffer reference " 2. Garbage collection " * daemon " * autocmds fu! s:popMarkdown() let bufnr = s:myBufNr() silent au! instant-markdown * call s:popBuffer(bufnr) if len(s:buffers) == 0 call s:killDaemon() endif endfu " ## Refresh if there's something new worth showing " " 'All things in moderation' fu! s:temperedRefresh() if !exists('b:changedtickLast') let b:changedtickLast = b:changedtick elseif b:changedtickLast != b:changedtick let b:changedtickLast = b:changedtick call s:refreshView() endif endfu " # Define the autocmds " aug instant-markdown au! * au BufEnter call s:refreshView() au CursorHold,CursorHoldI,CursorMoved,CursorMovedI call s:temperedRefresh() au BufWinLeave call s:popMarkdown() au BufwinEnter call s:pushMarkdown() aug END