1 "=============================================================================
3 " Author: Yasuhiro Matsumoto <mattn.jp@gmail.com>
4 " Last Change: 20-Aug-2011.
6 " WebPage: http://github.com/mattn/gist-vim
11 " post current buffer to gist, using default privicy option
12 " (see g:gist_private)
15 " post selected text to gist., using default privicy option
16 " This applies to all permutations listed below (except multi)
17 " (see g:gist_private)
20 " create a private gist
23 " create a public gist
24 " (only relevant if you've set gists to be private by default)
27 " post whole text to gist as public
28 " This is only relevant if you've set gists to be private by default
30 " create a gist anonymously
33 " create a gist with all open buffers
36 " edit the gist. (you need to have opend the gist buffer first)
37 " you can update the gist with :w command on gist buffer
40 " delete the gist. (you need to have opend the gist buffer first)
41 " password authentication is needed
44 " fork the gist. (you need to have opend the gist buffer first)
45 " password authentication is needed
48 " edit the gist with name 'foo.js'. (you need to have opend the gist buffer first)
54 " get gist XXXXX and add to clipboard
57 " list your public gists
60 " list gists from mattn
63 " list all your (public and private) gists
66 " * if set g:gist_clip_command, gist.vim will copy the gist code
70 " let g:gist_clip_command = 'pbcopy'
73 " let g:gist_clip_command = 'xclip -selection clipboard'
76 " let g:gist_clip_command = 'putclip'
78 " * if you want to detect filetype from gist's filename...
80 " # detect filetype if vim failed auto-detection.
81 " let g:gist_detect_filetype = 1
83 " # detect filetype always.
84 " let g:gist_detect_filetype = 2
86 " * if you want to open browser after the post...
88 " let g:gist_open_browser_after_post = 1
90 " * if you want to change the browser...
92 " let g:gist_browser_command = 'w3m %URL%'
96 " let g:gist_browser_command = 'opera %URL% &'
98 " on windows, should work with original setting.
100 " * if you want to show your private gists with ':Gist -l'
102 " let g:gist_show_privates = 1
104 " * if don't you want to copy URL of the post...
106 " let g:gist_put_url_to_clipboard_after_post = 0
108 " or if you want to copy URL and add linefeed at the last of URL,
110 " let g:gist_put_url_to_clipboard_after_post = 2
112 " default value is 1.
116 " removed carriage return
117 " gist_browser_command enhancement
120 " GetLatestVimScripts: 2423 1 :AutoInstall: gist.vim
121 " script type: plugin
123 if &cp || (exists('g:loaded_gist_vim') && g:loaded_gist_vim)
126 let g:loaded_gist_vim = 1
128 if (!exists('g:github_user') || !exists('g:github_token')) && !executable('git')
129 echohl ErrorMsg | echomsg "Gist: require 'git' command" | echohl None
133 if !executable('curl')
134 echohl ErrorMsg | echomsg "Gist: require 'curl' command" | echohl None
138 if !exists('g:gist_open_browser_after_post')
139 let g:gist_open_browser_after_post = 0
142 if !exists('g:gist_put_url_to_clipboard_after_post')
143 let g:gist_put_url_to_clipboard_after_post = 1
146 if !exists('g:gist_curl_options')
147 let g:gist_curl_options = ""
150 if !exists('g:gist_browser_command')
151 if has('win32') || has('win64')
152 let g:gist_browser_command = "!start rundll32 url.dll,FileProtocolHandler %URL%"
154 let g:gist_browser_command = "open %URL%"
155 elseif executable('xdg-open')
156 let g:gist_browser_command = "xdg-open %URL%"
158 let g:gist_browser_command = "firefox %URL% &"
162 if !exists('g:gist_detect_filetype')
163 let g:gist_detect_filetype = 0
166 if !exists('g:gist_private')
167 let g:gist_private = 0
170 if !exists('g:gist_show_privates')
171 let g:gist_show_privates = 0
174 if !exists('g:gist_cookie_dir')
175 let g:gist_cookie_dir = substitute(expand('<sfile>:p:h'), '[/\\]plugin$', '', '').'/cookies'
178 function! s:nr2hex(nr)
182 let r = '0123456789ABCDEF'[n % 16] . r
188 function! s:encodeURIComponent(instr)
189 let instr = iconv(a:instr, &enc, "utf-8")
190 let len = strlen(instr)
195 if ch =~# '[0-9A-Za-z-._~!''()*]'
196 let outstr = outstr . ch
198 let outstr = outstr . '+'
200 let outstr = outstr . '%' . substitute('0' . s:nr2hex(char2nr(ch)), '^.*\(..\)$', '\1', '')
207 " Note: A colon in the file name has side effects on Windows due to NTFS Alternate Data Streams; avoid it.
208 let s:bufprefix = 'gist' . (has('unix') ? ':' : '_')
209 function! s:GistList(user, token, gistls, page)
210 if a:gistls == '-all'
211 let url = 'https://gist.github.com/gists'
212 elseif g:gist_show_privates && a:gistls == a:user
213 let url = 'https://gist.github.com/mine'
215 let url = 'https://gist.github.com/'.a:gistls
217 let winnum = bufwinnr(bufnr(s:bufprefix.a:gistls))
219 if winnum != bufwinnr('%')
220 exe winnum 'wincmd w'
224 exec 'silent split' s:bufprefix.a:gistls
227 let oldlines = getline(0, line('$'))
228 let url = url . '?page=' . a:page
231 setlocal foldmethod=manual
233 if g:gist_show_privates
234 echon 'Login to gist... '
236 let res = s:GistGetPage(url, a:user, '', '-L')
237 silent put =res.content
240 exec 'silent r! curl -s' g:gist_curl_options url
246 silent! %g/<pre/,/<\/pre/join!
247 silent! %g/<span class="date"/,/<\/span/join
248 silent! %g/^<span class="date"/s/> */>/g
249 silent! %v/^\(gist:\|<pre>\|<span class="date">\)/d _
250 silent! %s/<div[^>]*>/\r /g
251 silent! %s/<\/pre>/\r/g
252 silent! %g/^gist:/,/<span class="date"/join
253 silent! %s/<[^>]\+>//g
255 silent! %s/ / /g
256 silent! %s/"/"/g
257 silent! %s/&/\&/g
260 silent! %s/&#\(\d\d\);/\=nr2char(submatch(1))/g
261 silent! %g/^gist: /s/ //g
263 call append(0, oldlines)
267 let b:token = a:token
268 let b:gistls = a:gistls
270 setlocal buftype=nofile bufhidden=hide noswapfile
272 syntax match SpecialKey /^gist:/he=e-1
273 nnoremap <silent> <buffer> <cr> :call <SID>GistListAction()<cr>
275 cal cursor(1+len(oldlines),1)
276 setlocal foldmethod=expr
277 setlocal foldexpr=getline(v:lnum)=~'^\\(gist:\\\|more\\)'?'>1':'='
278 setlocal foldtext=getline(v:foldstart)
281 function! s:GistGetFileName(gistid)
282 let url = 'https://gist.github.com/'.a:gistid
283 let res = system('curl -s '.g:gist_curl_options.' '.url)
284 let res = substitute(res, '^.*<a href="/raw/[^"]\+/\([^"]\+\)".*$', '\1', '')
292 function! s:GistDetectFiletype(gistid)
293 let url = 'https://gist.github.com/'.a:gistid
294 let mx = '^.*<div class=".\{-}type-\([^"]\+\)">.*$'
295 let res = system('curl -s '.g:gist_curl_options.' '.url)
296 let res = substitute(matchstr(res, mx), mx, '\1', '')
297 let res = substitute(res, '.*\(\.[^\.]\+\)$', '\1', '')
298 let res = substitute(res, '-', '', 'g')
299 " TODO: more filetype detection that is specified in html.
300 if res == 'bat' | let res = 'dosbatch' | endif
301 if res == 'as' | let res = 'actionscript' | endif
302 if res == 'bash' | let res = 'sh' | endif
303 if res == 'cl' | let res = 'lisp' | endif
304 if res == 'rb' | let res = 'ruby' | endif
305 if res == 'viml' | let res = 'vim' | endif
306 if res == 'plain' || res == 'text' | let res = '' | endif
309 silent! exec "doau BufRead *".res
311 silent! exec "setlocal ft=".tolower(res)
315 function! s:GistWrite(fname)
316 if substitute(a:fname, '\\', '/', 'g') == expand("%:p:gs@\\@/@")
319 exe "w".(v:cmdbang ? "!" : "") fnameescape(v:cmdarg) fnameescape(a:fname)
320 silent! exe "file" fnameescape(a:fname)
321 silent! au! BufWriteCmd <buffer>
325 function! s:GistGet(user, token, gistid, clipboard)
326 let url = 'https://raw.github.com/gist/'.a:gistid
327 let winnum = bufwinnr(bufnr(s:bufprefix.a:gistid))
329 if winnum != bufwinnr('%')
330 exe winnum 'wincmd w'
334 exec 'silent split' s:bufprefix.a:gistid
338 exec 'silent 0r! curl -s' g:gist_curl_options url
340 setlocal buftype=acwrite bufhidden=delete noswapfile
342 doau StdinReadPost <buffer>
343 if (&ft == '' && g:gist_detect_filetype == 1) || g:gist_detect_filetype == 2
344 call s:GistDetectFiletype(a:gistid)
347 if exists('g:gist_clip_command')
348 exec 'silent w !'.g:gist_clip_command
354 au! BufWriteCmd <buffer> call s:GistWrite(expand("<amatch>"))
357 function! s:GistListAction()
358 let line = getline('.')
359 let mx = '^gist:\(\w\+\).*'
361 let gistid = substitute(line, mx, '\1', '')
362 call s:GistGet(g:github_user, g:github_token, gistid, 0)
365 if line =~# '^more\.\.\.$'
367 call s:GistList(b:user, b:token, b:gistls, b:page+1)
372 function! s:GistUpdate(user, token, content, gistid, gistnm)
373 if len(a:gistnm) == 0
374 let name = s:GistGetFileName(a:gistid)
378 let namemx = '^[^.]\+\(.\+\)$'
381 let ext = substitute(name, namemx, '\1', '')
385 \ 'file_ext[gistfile1%s]=%s',
386 \ 'file_name[gistfile1%s]=%s',
387 \ 'file_contents[gistfile1%s]=%s',
391 let squery = printf(join(query, '&'),
392 \ s:encodeURIComponent(ext), s:encodeURIComponent(ext),
393 \ s:encodeURIComponent(ext), s:encodeURIComponent(name),
394 \ s:encodeURIComponent(ext), s:encodeURIComponent(a:content),
395 \ s:encodeURIComponent(a:user),
396 \ s:encodeURIComponent(a:token))
399 let file = tempname()
400 call writefile([squery], file)
401 echon 'Updating it to gist... '
402 let quote = &shellxquote == '"' ? "'" : '"'
403 let url = 'https://gist.github.com/gists/'.a:gistid
404 let res = system('curl -i '.g:gist_curl_options.' -d @'.quote.file.quote.' '.url)
406 let headers = split(res, '\(\r\?\n\|\r\n\?\)')
407 let location = matchstr(headers, '^Location: ')
408 let location = substitute(location, '^[^:]\+: ', '', '')
409 if len(location) > 0 && location =~ '^\(http\|https\):\/\/gist\.github\.com\/'
412 echo 'Done: '.location
414 let message = matchstr(headers, '^Status: ')
415 let message = substitute(message, '^[^:]\+: [0-9]\+ ', '', '')
416 echohl ErrorMsg | echomsg 'Edit failed: '.message | echohl None
421 function! s:GistGetPage(url, user, param, opt)
422 if !isdirectory(g:gist_cookie_dir)
423 call mkdir(g:gist_cookie_dir, 'p')
425 let cookie_file = g:gist_cookie_dir.'/github'
428 call delete(cookie_file)
432 let quote = &shellxquote == '"' ? "'" : '"'
433 if !filereadable(cookie_file)
434 let password = inputsecret('Password:')
435 if len(password) == 0
439 let url = 'https://gist.github.com/login?return_to=gist'
440 let res = system('curl -L -s '.g:gist_curl_options.' -c '.quote.cookie_file.quote.' '.quote.url.quote)
441 let token = substitute(res, '^.* name="authenticity_token" type="hidden" value="\([^"]\+\)".*$', '\1', '')
444 \ 'authenticity_token=%s',
450 let squery = printf(join(query, '&'),
451 \ s:encodeURIComponent(token),
452 \ s:encodeURIComponent(a:user),
453 \ s:encodeURIComponent(password))
456 let file = tempname()
457 let command = 'curl -s '.g:gist_curl_options.' -i'
458 let command .= ' -b '.quote.cookie_file.quote
459 let command .= ' -c '.quote.cookie_file.quote
460 let command .= ' '.quote.'https://gist.github.com/session'.quote
461 let command .= ' -d @' . quote.file.quote
462 call writefile([squery], file)
463 let res = system(command)
465 let res = matchstr(split(res, '\(\r\?\n\|\r\n\?\)'), '^Location: ')
466 let res = substitute(res, '^[^:]\+: ', '', '')
468 call delete(cookie_file)
472 let command = 'curl -s '.g:gist_curl_options.' -i '.a:opt
474 let command .= ' -d '.quote.a:param.quote
476 let command .= ' -b '.quote.cookie_file.quote
477 let command .= ' '.quote.a:url.quote
478 let res = iconv(system(command), "utf-8", &encoding)
479 let pos = stridx(res, "\r\n\r\n")
481 let content = res[pos+4:]
483 let pos = stridx(res, "\n\n")
484 let content = res[pos+2:]
487 \ "header" : split(res[0:pos], '\r\?\n'),
488 \ "content" : content
492 function! s:GistDelete(user, token, gistid)
493 echon 'Deleting gist... '
494 let res = s:GistGetPage('https://gist.github.com/'.a:gistid, a:user, '', '')
496 echohl ErrorMsg | echomsg 'Wrong password? no response received from github trying to delete ' . a:gistid | echohl None
499 let mx = '^.* name="authenticity_token" type="hidden" value="\([^"]\+\)".*$'
500 let token = substitute(matchstr(res.content, mx), mx, '\1', '')
502 let res = s:GistGetPage('https://gist.github.com/delete/'.a:gistid, a:user, '_method=delete&authenticity_token='.token, '')
503 if len(res.content) > 0
507 let message = matchstr(res.header, '^Status: ')
508 let message = substitute(message, '^[^:]\+: [0-9]\+ ', '', '')
509 echohl ErrorMsg | echomsg 'Delete failed: '.message | echohl None
512 echohl ErrorMsg | echomsg 'Delete failed' | echohl None
518 " Post new gist to github
520 " if there is an embedded gist url or gist id in your file,
521 " it will just update it.
524 " embedded gist url format:
526 " Gist: https://gist.github.com/123123
528 " embedded gist id format:
532 function! s:GistPost(user, token, content, private)
534 " find GistID: in content, then we should just update
535 for l in split(a:content, "\n")
537 let gistid = matchstr(l, 'GistID:\s*[0-9a-z]\+')
539 if strlen(gistid) == 0
540 echohl WarningMsg | echo "GistID error" | echohl None
543 echo "Found GistID: " . gistid
545 cal s:GistUpdate(a:user, a:token, a:content, gistid, '')
547 elseif l =~ '\<Gist:'
548 let gistid = matchstr(l, 'Gist:\s*https://gist.github.com/[0-9a-z]\+')
550 if strlen(gistid) == 0
551 echohl WarningMsg | echo "GistID error" | echohl None
554 echo "Found GistID: " . gistid
556 cal s:GistUpdate(a:user, a:token, a:content, gistid, '')
561 let ext = expand('%:e')
562 let ext = len(ext) ? '.'.ext : ''
563 let name = expand('%:t')
566 \ 'file_ext[gistfile1]=%s',
567 \ 'file_name[gistfile1]=%s',
568 \ 'file_contents[gistfile1]=%s',
571 if len(a:user) > 0 && len(a:token) > 0
572 call add(query, 'login=%s')
573 call add(query, 'token=%s')
575 call add(query, '%.0s%.0s')
579 call add(query, 'action_button=private')
581 let squery = printf(join(query, '&'),
582 \ s:encodeURIComponent(ext),
583 \ s:encodeURIComponent(name),
584 \ s:encodeURIComponent(a:content),
585 \ s:encodeURIComponent(a:user),
586 \ s:encodeURIComponent(a:token))
589 let file = tempname()
590 call writefile([squery], file)
591 echon 'Posting it to gist... '
592 let quote = &shellxquote == '"' ? "'" : '"'
593 let url = 'https://gist.github.com/gists'
594 let res = system('curl -i '.g:gist_curl_options.' -d @'.quote.file.quote.' '.url)
596 let headers = split(res, '\(\r\?\n\|\r\n\?\)')
597 let location = matchstr(headers, '^Location: ')
598 let location = substitute(location, '^[^:]\+: ', '', '')
599 if len(location) > 0 && location =~ '^\(http\|https\):\/\/gist\.github\.com\/'
601 echo 'Done: '.location
603 let message = matchstr(headers, '^Status: ')
604 let message = substitute(message, '^[^:]\+: [0-9]\+ ', '', '')
605 echohl ErrorMsg | echomsg 'Post failed: '.message | echohl None
610 function! s:GistPostBuffers(user, token, private)
611 let bufnrs = range(1, bufnr("$"))
614 if len(a:user) > 0 && len(a:token) > 0
615 call add(query, 'login=%s')
616 call add(query, 'token=%s')
618 call add(query, '%.0s%.0s')
621 call add(query, 'action_button=private')
623 let squery = printf(join(query, "&"),
624 \ s:encodeURIComponent(a:user),
625 \ s:encodeURIComponent(a:token)) . '&'
628 \ 'file_ext[gistfile]=%s',
629 \ 'file_name[gistfile]=%s',
630 \ 'file_contents[gistfile]=%s',
632 let format = join(query, "&") . '&'
636 if !bufexists(bufnr) || buflisted(bufnr) == 0
639 echo "Creating gist content".index."... "
640 silent! exec "buffer!" bufnr
641 let content = join(getline(1, line('$')), "\n")
642 let ext = expand('%:e')
643 let ext = len(ext) ? '.'.ext : ''
644 let name = expand('%:t')
645 let squery .= printf(substitute(format, 'gistfile', 'gistfile'.index, 'g'),
646 \ s:encodeURIComponent(ext),
647 \ s:encodeURIComponent(name),
648 \ s:encodeURIComponent(content))
649 let index = index + 1
651 silent! exec "buffer!" bn
653 let file = tempname()
654 call writefile([squery], file)
655 echo "Posting it to gist... "
656 let quote = &shellxquote == '"' ? "'" : '"'
657 let url = 'https://gist.github.com/gists'
658 let res = system('curl -i '.g:gist_curl_options.' -d @'.quote.file.quote.' '.url)
660 let res = matchstr(split(res, '\(\r\?\n\|\r\n\?\)'), '^Location: ')
661 let res = substitute(res, '^.*: ', '', '')
662 if len(res) > 0 && res =~ '^\(http\|https\):\/\/gist\.github\.com\/'
666 echohl ErrorMsg | echomsg 'Post failed' | echohl None
671 function! Gist(line1, line2, ...)
672 if !exists('g:github_user')
673 let g:github_user = substitute(system('git config --global github.user'), "\n", '', '')
674 if strlen(g:github_user) == 0
675 let g:github_user = $GITHUB_USER
678 if !exists('g:github_token')
679 let g:github_token = substitute(system('git config --global github.token'), "\n", '', '')
680 if strlen(g:github_token) == 0
681 let g:github_token = $GITHUB_TOKEN
684 if strlen(g:github_user) == 0 || strlen(g:github_token) == 0
686 echomsg "You have no setting for github."
688 echo "git config --global github.user your-name"
689 echo "git config --global github.token your-token"
690 echo "or set g:github_user and g:github_token in your vimrc"
691 echo "or set shell env vars GITHUB_USER and GITHUB_TOKEN"
696 let bufname = bufname("%")
697 let user = g:github_user
698 let token = g:github_token
702 let private = g:gist_private
707 let listmx = '^\(-l\|--list\)\s*\([^\s]\+\)\?$'
708 let bufnamemx = '^' . s:bufprefix .'\([0-9a-f]\+\)$'
710 let args = (a:0 > 0) ? split(a:1, ' ') : []
712 if arg =~ '^\(-la\|--listall\)$\C'
714 elseif arg =~ '^\(-l\|--list\)$\C'
715 if g:gist_show_privates
718 let gistls = g:github_user
720 elseif arg == '--abandon\C'
721 call s:GistGetPage('', '', '', '')
723 elseif arg =~ '^\(-m\|--multibuffer\)$\C'
725 elseif arg =~ '^\(-p\|--private\)$\C'
727 elseif arg =~ '^\(-P\|--public\)$\C'
729 elseif arg =~ '^\(-a\|--anonymous\)$\C'
732 elseif arg =~ '^\(-c\|--clipboard\)$\C'
734 elseif arg =~ '^\(-d\|--delete\)$\C' && bufname =~ bufnamemx
736 let gistid = substitute(bufname, bufnamemx, '\1', '')
737 elseif arg =~ '^\(-e\|--edit\)$\C' && bufname =~ bufnamemx
739 let gistid = substitute(bufname, bufnamemx, '\1', '')
740 elseif arg =~ '^\(-f\|--fork\)$\C' && bufname =~ bufnamemx
741 let gistid = substitute(bufname, bufnamemx, '\1', '')
742 let res = s:GistGetPage("https://gist.github.com/fork/".gistid, g:github_user, '', '')
743 let loc = filter(res.header, 'v:val =~ "^Location:"')[0]
744 let loc = substitute(loc, '^[^:]\+: ', '', '')
745 let mx = '^https://gist.github.com/\([0-9a-z]\+\)$'
747 let gistid = substitute(loc, mx, '\1', '')
749 echohl ErrorMsg | echomsg 'Fork failed' | echohl None
752 elseif arg !~ '^-' && len(gistnm) == 0
753 if editpost == 1 || deletepost == 1
755 elseif len(gistls) > 0 && arg != '^\w\+$\C'
757 elseif arg =~ '^[0-9a-z]\+$\C'
760 echohl ErrorMsg | echomsg 'Invalid arguments' | echohl None
765 echohl ErrorMsg | echomsg 'Invalid arguments' | echohl None
771 "echo "gistid=".gistid
772 "echo "gistls=".gistls
773 "echo "gistnm=".gistnm
774 "echo "private=".private
775 "echo "clipboard=".clipboard
776 "echo "editpost=".editpost
777 "echo "deletepost=".deletepost
780 call s:GistList(user, token, gistls, 1)
781 elseif len(gistid) > 0 && editpost == 0 && deletepost == 0
782 call s:GistGet(user, token, gistid, clipboard)
786 let url = s:GistPostBuffers(user, token, private)
788 let content = join(getline(a:line1, a:line2), "\n")
790 let url = s:GistUpdate(user, token, content, gistid, gistnm)
791 elseif deletepost == 1
792 call s:GistDelete(user, token, gistid)
794 let url = s:GistPost(user, token, content, private)
798 if g:gist_open_browser_after_post
799 let cmd = substitute(g:gist_browser_command, '%URL%', url, 'g')
802 elseif cmd =~ '^:[A-Z]'
808 if g:gist_put_url_to_clipboard_after_post > 0
809 if g:gist_put_url_to_clipboard_after_post == 2
812 if exists('g:gist_clip_command')
813 call system(g:gist_clip_command, url)
814 elseif has('unix') && !has('xterm_clipboard')
825 command! -nargs=? -range=% Gist :call Gist(<line1>, <line2>, <f-args>)