mattn / vim-gist

Vim plugin for Gist
http://www.vim.org/scripts/script.php?script_id=2423
1.69k stars 131 forks source link

[proposal]: live remote update while updating local file #236

Closed pwang2 closed 1 year ago

pwang2 commented 2 years ago

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
pwang2 commented 1 year ago

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