Skip to content

Commit 882aa2b

Browse files
mattnvim-scripts
authored andcommitted
Version 6.2: This is an upgrade for Gist.vim: fixed some bugs.
1 parent b377b88 commit 882aa2b

File tree

2 files changed

+105
-73
lines changed

2 files changed

+105
-73
lines changed

autoload/gist.vim

Lines changed: 92 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"=============================================================================
22
" File: gist.vim
33
" Author: Yasuhiro Matsumoto <[email protected]>
4-
" Last Change: 01-Apr-2012.
5-
" Version: 6.1
4+
" Last Change: 02-Apr-2012.
5+
" Version: 6.2
66
" WebPage: http://github.com/mattn/gist-vim
77
" License: BSD
88
" Usage:
@@ -115,6 +115,13 @@
115115
let s:save_cpo = &cpo
116116
set cpo&vim
117117

118+
if !exists('g:github_user')
119+
let g:github_user = substitute(system('git config --get github.user'), "\n", '', '')
120+
if strlen(g:github_user) == 0
121+
let g:github_user = $GITHUB_USER
122+
end
123+
endif
124+
118125
function! s:get_browser_command()
119126
let gist_browser_command = get(g:, 'gist_browser_command', '')
120127
if gist_browser_command == ''
@@ -211,8 +218,15 @@ function! s:GistList(gistls, page)
211218
echohl ErrorMsg | echomsg 'Gists not found' | echohl None
212219
return
213220
endif
221+
let content = json#decode(res.content)
222+
if type(content) == 4 && has_key(content, 'message') && len(content.message)
223+
bw!
224+
redraw
225+
echohl ErrorMsg | echomsg content.message | echohl None
226+
return
227+
endif
214228

215-
let lines = map(json#decode(res.content), 's:format_gist(v:val)')
229+
let lines = map(content, 's:format_gist(v:val)')
216230
call setline(1, split(join(lines, "\n"), "\n"))
217231

218232
$put='more...'
@@ -261,67 +275,75 @@ function! s:GistWrite(fname)
261275
endfunction
262276

263277
function! s:GistGet(gistid, clipboard)
264-
let winnum = bufwinnr(bufnr(s:bufprefix.a:gistid))
265-
if winnum != -1
266-
if winnum != bufwinnr('%')
267-
exe winnum 'wincmd w'
268-
endif
269-
setlocal modifiable
270-
else
271-
exec 'silent noautocmd split' s:bufprefix.a:gistid
272-
endif
273-
let old_undolevels = &undolevels
274-
set undolevels=-1
275-
filetype detect
276-
silent %d _
278+
redraw | echon 'Getting gist... '
277279
let res = http#get('https://api.github.com/gists/'.a:gistid, '', { "Authorization": s:GetAuthHeader() })
278280
let status = matchstr(matchstr(res.header, '^Status:'), '^[^:]\+: \zs.*')
279281
if status =~ '^2'
280-
call writefile(split(res.content, "\n"), "myjson.js")
281-
try
282-
let gist = json#decode(res.content)
283-
let filename = sort(keys(gist.files))[0]
284-
let content = gist.files[filename].content
285-
call setline(1, split(content, "\n"))
286-
let b:gist = {
287-
\ "filename": filename,
288-
\ "id": gist.id,
289-
\ "description": gist.description,
290-
\ "private": gist.public =~ 'true',
291-
\}
292-
catch
282+
let gist = json#decode(res.content)
283+
if get(g:, 'gist_get_mutiplefile', 0) != 0
284+
let num_file = len(keys(gist.files))
285+
else
286+
let num_file = 1
287+
endif
288+
for n in range(num_file)
289+
try
290+
let filename = sort(keys(gist.files))[n]
291+
292+
let winnum = bufwinnr(bufnr(s:bufprefix.a:gistid."/".filename))
293+
if winnum != -1
294+
if winnum != bufwinnr('%')
295+
exe winnum 'wincmd w'
296+
endif
297+
setlocal modifiable
298+
else
299+
exec 'silent noautocmd split' s:bufprefix.a:gistid."/".filename
300+
endif
301+
let old_undolevels = &undolevels
302+
set undolevels=-1
303+
filetype detect
304+
silent %d _
305+
306+
let content = gist.files[filename].content
307+
call setline(1, split(content, "\n"))
308+
let b:gist = {
309+
\ "filename": filename,
310+
\ "id": gist.id,
311+
\ "description": gist.description,
312+
\ "private": gist.public =~ 'true',
313+
\}
314+
catch
315+
let &undolevels = old_undolevels
316+
bw!
317+
redraw
318+
echohl ErrorMsg | echomsg 'Gist contains binary' | echohl None
319+
return
320+
endtry
293321
let &undolevels = old_undolevels
294-
bw!
295-
redraw
296-
echohl ErrorMsg | echomsg 'Gist contains binary' | echohl None
297-
return
298-
endtry
322+
setlocal buftype=acwrite bufhidden=delete noswapfile
323+
setlocal nomodified
324+
doau StdinReadPost <buffer>
325+
let gist_detect_filetype = get(g:, 'gist_detect_filetype', 0)
326+
if (&ft == '' && gist_detect_filetype == 1) || gist_detect_filetype == 2
327+
call s:GistDetectFiletype(a:gistid)
328+
endif
329+
if a:clipboard
330+
if exists('g:gist_clip_command')
331+
exec 'silent w !'.g:gist_clip_command
332+
elseif has('clipboard')
333+
silent! %yank +
334+
else
335+
%yank
336+
endif
337+
endif
338+
1
339+
au! BufWriteCmd <buffer> call s:GistWrite(expand("<amatch>"))
340+
endfor
299341
else
300-
let &undolevels = old_undolevels
301342
bw!
302343
redraw
303344
echohl ErrorMsg | echomsg 'Gist not found' | echohl None
304345
return
305346
endif
306-
let &undolevels = old_undolevels
307-
setlocal buftype=acwrite bufhidden=delete noswapfile
308-
setlocal nomodified
309-
doau StdinReadPost <buffer>
310-
let gist_detect_filetype = get(g:, 'gist_detect_filetype', 0)
311-
if (&ft == '' && gist_detect_filetype == 1) || gist_detect_filetype == 2
312-
call s:GistDetectFiletype(a:gistid)
313-
endif
314-
if a:clipboard
315-
if exists('g:gist_clip_command')
316-
exec 'silent w !'.g:gist_clip_command
317-
elseif has('clipboard')
318-
silent! %yank +
319-
else
320-
%yank
321-
endif
322-
endif
323-
1
324-
au! BufWriteCmd <buffer> call s:GistWrite(expand("<amatch>"))
325347
endfunction
326348

327349
function! s:GistListAction(shift)
@@ -490,11 +512,9 @@ endfunction
490512

491513
function! gist#Gist(count, line1, line2, ...)
492514
redraw
493-
if !exists('g:github_user')
494-
let g:github_user = substitute(system('git config --global github.user'), "\n", '', '')
495-
if strlen(g:github_user) == 0
496-
let g:github_user = $GITHUB_USER
497-
end
515+
if strlen(g:github_user) == 0
516+
echohl ErrorMsg | echomsg "You don't have github account. read ':help gist-vim-setup'." | echohl None
517+
return
498518
endif
499519
let bufname = bufname("%")
500520
let gistid = ''
@@ -508,7 +528,7 @@ function! gist#Gist(count, line1, line2, ...)
508528
let editpost = 0
509529
let anonymous = 0
510530
let listmx = '^\%(-l\|--list\)\s*\([^\s]\+\)\?$'
511-
let bufnamemx = '^' . s:bufprefix .'\zs\([0-9a-f]\+\)\ze$'
531+
let bufnamemx = '^' . s:bufprefix .'\zs\([0-9a-f]\+\|[0-9a-f]\+[/\\].*\)\ze$'
512532

513533
let args = (a:0 > 0) ? s:shellwords(a:1) : []
514534
for arg in args
@@ -580,12 +600,12 @@ function! gist#Gist(count, line1, line2, ...)
580600
elseif arg =~ '^[0-9a-z]\+$\C'
581601
let gistid = arg
582602
else
583-
echohl ErrorMsg | echomsg 'Invalid arguments' | echohl None
603+
echohl ErrorMsg | echomsg 'Invalid arguments: '.arg | echohl None
584604
unlet args
585605
return 0
586606
endif
587607
elseif len(arg) > 0
588-
echohl ErrorMsg | echomsg 'Invalid arguments' | echohl None
608+
echohl ErrorMsg | echomsg 'Invalid arguments: '.arg | echohl None
589609
unlet args
590610
return 0
591611
endif
@@ -618,6 +638,12 @@ function! gist#Gist(count, line1, line2, ...)
618638
let content = @"
619639
call setreg('"', save_regcont, save_regtype)
620640
endif
641+
" find GistID: in content , then we should just update
642+
let id = matchstr(content, '\(GistID:\s*\)\@<=[0-9]\+')
643+
if len(id) > 0
644+
let gistid = id
645+
let editpost = 1
646+
endif
621647
if editpost == 1
622648
let url = s:GistUpdate(content, gistid, gistnm, gistdesc)
623649
elseif deletepost == 1
@@ -652,6 +678,10 @@ function! gist#Gist(count, line1, line2, ...)
652678
endfunction
653679

654680
function! s:GetAuthHeader()
681+
if get(g:, 'gist_use_password_in_gitconfig', 0) != 0
682+
let password = substitute(system('git config --get github.password'), "\n", '', '')
683+
return printf("basic %s", base64#b64encode(g:github_user.":".password))
684+
endif
655685
let auth = ""
656686
let configfile = expand('~/.gist-vim')
657687
if filereadable(configfile)
@@ -671,12 +701,6 @@ function! s:GetAuthHeader()
671701
echohl None
672702
let api = inputlist(['Which API:', '1. basic auth', '2. oauth2'])
673703
if api == 1
674-
if !exists('g:github_user')
675-
let g:github_user = substitute(system('git config --global github.user'), "\n", '', '')
676-
if strlen(g:github_user) == 0
677-
let g:github_user = $GITHUB_USER
678-
end
679-
endif
680704
redraw | echo "\r"
681705
let password = inputsecret("Password:")
682706
let secret = printf("basic %s", base64#b64encode(g:github_user.":".password))

doc/gist-vim.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ If you want to show your private gists with ":Gist -l": >
142142
143143
let g:gist_show_privates = 1
144144
<
145-
You need to either set global git config
146-
>
147-
$ git config --global github.user Username
148-
<
149145
==============================================================================
150146
REQUIREMENTS *gist-vim-requirements*
151147

@@ -201,7 +197,19 @@ gist-vim have two ways to access APIs.
201197
|BasicAuth|
202198

203199
Require github user ID and password. This is easy but not secure.
204-
200+
You need to either set global git config
201+
>
202+
$ git config --global github.user Username
203+
<
204+
If you want to use password written in ~/.gitconfig like below:
205+
>
206+
[github]
207+
password = xxxxx
208+
<
209+
Add following into your ~/.vimrc
210+
>
211+
let g:gist_use_password_in_gitconfig = 1
212+
<
205213
|OAuth2|
206214

207215
1. Register your application.

0 commit comments

Comments
 (0)