mattn/vim-gist

[proposal]: live remote update while updating local file

pwang2 opened this issue · 1 comments

The motivation comes from when I have to work on different machines and update my vimrc file, the Gist -l and save to local file workflow sounds quite cumbersome to me. It would be much smoother if vimrc file can be downloaded from remote gist automatically then sourced when vimrc file is opened.

In current vim-gist, we already have the embed GistID: xxxx for updating. It would be great if we can using the same machanism to offer an option to automatically sync the remote gist file to local when open the file with an embeded GistID at the end of the file.

I have this poorman's implementation in my vimrc for a while. But I feel it would be great if we could have this in the plugin itself and use a simple configuration to opt in.

function! s:sync_gist()
  let b:fpath = expand('%:p')
  let b:fname = expand('%:t')
  let b:lastline = system('tail -n 1 '. b:fpath))
  if b:lastline =~ 'GistID'
    let b:gid = substitute(system('tail -n 1 '. b:fpath. " | awk -F': ' '{print $2}' ") ,'\n', '', 'g')
    let b:cmd = '%!curl -sSL -H "Cache-Control: no-cache" "https://gist.githubusercontent.com/xxx/'. b:gid. '/raw?t='. strftime('%s'). '"'
    call setreg('+', b:cmd)
    silent execute(b:cmd)

    if trim(getline(line('$'))) == trim(b:lastline)
      silent write
      if b:fname == 'init.lua' || b:fname == 'init-old.vim'"
        execute('source $MYVIMRC')
      endif
    else
      echoerr("Remote version not expected, reload disk version")
      execute(':e!')
    endif
  endif
endfunction

function! s:post_gist()
  if getbufinfo('%')[0].changed && getline('$') =~ "GistID" 
     execute "Gist" 
  endif
endfunction

  au BufReadPost  *                  call <SID>sync_gist()
  au BufWritePre  *                  call <SID>post_gist()

"GistID: xxxxxxxxxxx

switched to gist-paste command line.
https://github.com/dblevins/gistpaste