-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathgist.vim
1185 lines (1121 loc) · 35 KB
/
gist.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"=============================================================================
" File: gist.vim
" Author: Yasuhiro Matsumoto <[email protected]>
" Last Change: 10-Oct-2016.
" Version: 7.3
" WebPage: http://github.com/mattn/vim-gist
" License: BSD
let s:save_cpo = &cpoptions
set cpoptions&vim
if exists('g:gist_disabled') && g:gist_disabled == 1
function! gist#Gist(...) abort
endfunction
finish
endif
if !exists('g:github_user') && !executable('git')
echohl ErrorMsg | echomsg 'Gist: require ''git'' command' | echohl None
finish
endif
if !executable('curl')
echohl ErrorMsg | echomsg 'Gist: require ''curl'' command' | echohl None
finish
endif
if globpath(&rtp, 'autoload/webapi/http.vim') ==# ''
echohl ErrorMsg | echomsg 'Gist: require ''webapi'', install https://github.com/mattn/webapi-vim' | echohl None
finish
else
call webapi#json#true()
endif
let s:gist_token_file = expand(get(g:, 'gist_token_file', '~/.gist-vim'))
let s:system = function(get(g:, 'webapi#system_function', 'system'))
if !exists('g:github_user')
let g:github_user = substitute(s:system('git config --get github.user'), "\n", '', '')
if strlen(g:github_user) == 0
let g:github_user = $GITHUB_USER
end
endif
if !exists('g:gist_api_url')
let g:gist_api_url = substitute(s:system('git config --get github.apiurl'), "\n", '', '')
if strlen(g:gist_api_url) == 0
let g:gist_api_url = 'https://api.github.com/'
end
if exists('g:github_api_url') && !exists('g:gist_shutup_issue154')
if matchstr(g:gist_api_url, 'https\?://\zs[^/]\+\ze') != matchstr(g:github_api_url, 'https\?://\zs[^/]\+\ze')
echohl WarningMsg
echo '--- Warning ---'
echo 'It seems that you set different URIs for github_api_url/gist_api_url.'
echo 'If you want to remove this message: let g:gist_shutup_issue154 = 1'
echohl None
if confirm('Continue?', '&Yes\n&No') != 1
let g:gist_disabled = 1
finish
endif
redraw!
endif
endif
endif
if g:gist_api_url !~# '/$'
let g:gist_api_url .= '/'
endif
if !exists('g:gist_update_on_write')
let g:gist_update_on_write = 1
endif
function! s:get_browser_command() abort
let gist_browser_command = get(g:, 'gist_browser_command', '')
if gist_browser_command ==# ''
if has('win32') || has('win64')
let gist_browser_command = '!start rundll32 url.dll,FileProtocolHandler %URL%'
elseif has('mac') || has('macunix') || has('gui_macvim') || system('uname') =~? '^darwin'
let gist_browser_command = 'open %URL%'
elseif executable('xdg-open')
let gist_browser_command = 'xdg-open %URL%'
elseif executable('firefox')
let gist_browser_command = 'firefox %URL% &'
else
let gist_browser_command = ''
endif
endif
return gist_browser_command
endfunction
function! s:open_browser(url) abort
let cmd = s:get_browser_command()
if len(cmd) == 0
redraw
echohl WarningMsg
echo 'It seems that you don''t have general web browser. Open URL below.'
echohl None
echo a:url
return
endif
let quote = &shellxquote == '"' ? "'" : '"'
if cmd =~# '^!'
let cmd = substitute(cmd, '%URL%', '\=quote.a:url.quote', 'g')
silent! exec cmd
elseif cmd =~# '^:[A-Z]'
let cmd = substitute(cmd, '%URL%', '\=a:url', 'g')
exec cmd
else
let cmd = substitute(cmd, '%URL%', '\=quote.a:url.quote', 'g')
call system(cmd)
endif
endfunction
function! s:shellwords(str) abort
let words = split(a:str, '\%(\([^ \t\''"]\+\)\|''\([^\'']*\)''\|"\(\%([^\"\\]\|\\.\)*\)"\)\zs\s*\ze')
let words = map(words, 'substitute(v:val, ''\\\([\\ ]\)'', ''\1'', "g")')
let words = map(words, 'matchstr(v:val, ''^\%\("\zs\(.*\)\ze"\|''''\zs\(.*\)\ze''''\|.*\)$'')')
return words
endfunction
function! s:truncate(str, num)
let mx_first = '^\(.\)\(.*\)$'
let str = a:str
let ret = ''
let width = 0
while 1
let char = substitute(str, mx_first, '\1', '')
let cells = strdisplaywidth(char)
if cells == 0 || width + cells > a:num
break
endif
let width = width + cells
let ret .= char
let str = substitute(str, mx_first, '\2', '')
endwhile
while width + 1 <= a:num
let ret .= ' '
let width = width + 1
endwhile
return ret
endfunction
function! s:format_gist(gist) abort
let files = sort(keys(a:gist.files))
if empty(files)
return ''
endif
let file = a:gist.files[files[0]]
let name = file.filename
if has_key(file, 'content')
let code = file.content
let code = "\n".join(map(split(code, "\n"), '" ".v:val'), "\n")
else
let code = ''
endif
let desc = type(a:gist.description)==0 || a:gist.description ==# '' ? '' : a:gist.description
let name = substitute(name, '[\r\n\t]', ' ', 'g')
let name = substitute(name, ' ', ' ', 'g')
let desc = substitute(desc, '[\r\n\t]', ' ', 'g')
let desc = substitute(desc, ' ', ' ', 'g')
" Display a nice formatted (and truncated if needed) table of gists on screen
" Calculate field lengths for gist-listing formatting on screen
redir =>a |exe 'sil sign place buffer='.bufnr('')|redir end
let signlist = split(a, '\n')
let width = winwidth(0) - ((&number||&relativenumber) ? &numberwidth : 0) - &foldcolumn - (len(signlist) > 2 ? 2 : 0)
let idlen = 33
let namelen = get(g:, 'gist_namelength', 30)
let desclen = width - (idlen + namelen + 10)
return printf('gist: %s %s %s', s:truncate(a:gist.id, idlen), s:truncate(name, namelen), s:truncate(desc, desclen))
endfunction
" Note: A colon in the file name has side effects on Windows due to NTFS Alternate Data Streams; avoid it.
let s:bufprefix = 'gist' . (has('unix') ? ':' : '_')
function! s:GistList(gistls, page, pagelimit) abort
if a:gistls ==# '-all'
let url = g:gist_api_url.'gists/public'
elseif get(g:, 'gist_show_privates', 0) && a:gistls ==# 'starred'
let url = g:gist_api_url.'gists/starred'
elseif get(g:, 'gist_show_privates') && a:gistls ==# 'mine'
let url = g:gist_api_url.'gists'
else
let url = g:gist_api_url.'users/'.a:gistls.'/gists'
endif
let winnum = bufwinnr(bufnr(s:bufprefix.a:gistls))
if winnum != -1
if winnum != bufwinnr('%')
exe winnum 'wincmd w'
endif
setlocal modifiable
else
if get(g:, 'gist_list_vsplit', 0)
exec 'silent noautocmd vsplit +set\ winfixwidth ' s:bufprefix.a:gistls
elseif get(g:, 'gist_list_rightbelow', 0)
exec 'silent noautocmd rightbelow 5 split +set\ winfixheight ' s:bufprefix.a:gistls
else
exec 'silent noautocmd split' s:bufprefix.a:gistls
endif
endif
let url = url . '?per_page=' . a:pagelimit
if a:page > 1
let oldlines = getline(0, line('$'))
let url = url . '&page=' . a:page
endif
setlocal modifiable
let old_undolevels = &undolevels
let oldlines = []
silent %d _
redraw | echon 'Listing gists... '
let auth = s:GistGetAuthHeader()
if len(auth) == 0
bw!
redraw
echohl ErrorMsg | echomsg v:errmsg | echohl None
return
endif
let res = webapi#http#get(url, '', { 'Authorization': auth })
if v:shell_error != 0
bw!
redraw
echohl ErrorMsg | echomsg 'Gists not found' | echohl None
return
endif
let content = webapi#json#decode(res.content)
if type(content) == 4 && has_key(content, 'message') && len(content.message)
bw!
redraw
echohl ErrorMsg | echomsg content.message | echohl None
if content.message ==# 'Bad credentials'
call delete(s:gist_token_file)
endif
return
endif
let lines = map(filter(content, '!empty(v:val.files)'), 's:format_gist(v:val)')
call setline(1, split(join(lines, "\n"), "\n"))
$put='more...'
let b:gistls = a:gistls
let b:page = a:page
setlocal buftype=nofile bufhidden=hide noswapfile
setlocal cursorline
setlocal nomodified
setlocal nomodifiable
syntax match SpecialKey /^gist:/he=e-1
syntax match Title /^gist: \S\+/hs=s+5 contains=ALL
nnoremap <silent> <buffer> <cr> :call <SID>GistListAction(0)<cr>
nnoremap <silent> <buffer> o :call <SID>GistListAction(0)<cr>
nnoremap <silent> <buffer> b :call <SID>GistListAction(1)<cr>
nnoremap <silent> <buffer> y :call <SID>GistListAction(2)<cr>
nnoremap <silent> <buffer> p :call <SID>GistListAction(3)<cr>
nnoremap <silent> <buffer> <esc> :bw<cr>
nnoremap <silent> <buffer> <s-cr> :call <SID>GistListAction(1)<cr>
cal cursor(1+len(oldlines),1)
nohlsearch
redraw | echo ''
endfunction
function! gist#list_recursively(user, ...) abort
let use_cache = get(a:000, 0, 1)
let limit = get(a:000, 1, -1)
let verbose = get(a:000, 2, 1)
if a:user ==# 'mine'
let url = g:gist_api_url . 'gists'
elseif a:user ==# 'starred'
let url = g:gist_api_url . 'gists/starred'
else
let url = g:gist_api_url.'users/'.a:user.'/gists'
endif
let auth = s:GistGetAuthHeader()
if len(auth) == 0
" anonymous user cannot get gists to prevent infinite recursive loading
return []
endif
if use_cache && exists('g:gist_list_recursively_cache')
if has_key(g:gist_list_recursively_cache, a:user)
return webapi#json#decode(g:gist_list_recursively_cache[a:user])
endif
endif
let page = 1
let gists = []
let lastpage = -1
function! s:get_lastpage(res) abort
let links = split(a:res.header[match(a:res.header, 'Link')], ',')
let link = links[match(links, 'rel=[''"]last[''"]')]
let page = str2nr(matchlist(link, '\%(page=\)\(\d\+\)')[1])
return page
endfunction
if verbose > 0
redraw | echon 'Loading gists...'
endif
while limit == -1 || page <= limit
let res = webapi#http#get(url.'?page='.page, '', {'Authorization': auth})
if limit == -1
" update limit to the last page
let limit = s:get_lastpage(res)
endif
if verbose > 0
redraw | echon 'Loading gists... ' . page . '/' . limit . ' pages has loaded.'
endif
let gists = gists + webapi#json#decode(res.content)
let page = page + 1
endwhile
let g:gist_list_recursively_cache = get(g:, 'gist_list_recursively_cache', {})
let g:gist_list_recursively_cache[a:user] = webapi#json#encode(gists)
return gists
endfunction
function! gist#list(user, ...) abort
let page = get(a:000, 0, 0)
if a:user ==# '-all'
let url = g:gist_api_url.'gists/public'
elseif get(g:, 'gist_show_privates', 0) && a:user ==# 'starred'
let url = g:gist_api_url.'gists/starred'
elseif get(g:, 'gist_show_privates') && a:user ==# 'mine'
let url = g:gist_api_url.'gists'
else
let url = g:gist_api_url.'users/'.a:user.'/gists'
endif
let auth = s:GistGetAuthHeader()
if len(auth) == 0
return []
endif
let res = webapi#http#get(url, '', { 'Authorization': auth })
return webapi#json#decode(res.content)
endfunction
function! s:GistGetFileName(gistid) abort
let auth = s:GistGetAuthHeader()
if len(auth) == 0
return ''
endif
let res = webapi#http#get(g:gist_api_url.'gists/'.a:gistid, '', { 'Authorization': auth })
let gist = webapi#json#decode(res.content)
if has_key(gist, 'files')
return sort(keys(gist.files))[0]
endif
return ''
endfunction
function! s:GistDetectFiletype(gistid) abort
let auth = s:GistGetAuthHeader()
if len(auth) == 0
return ''
endif
let res = webapi#http#get(g:gist_api_url.'gists/'.a:gistid, '', { 'Authorization': auth })
let gist = webapi#json#decode(res.content)
let filename = sort(keys(gist.files))[0]
let ext = fnamemodify(filename, ':e')
if has_key(s:extmap, ext)
let type = s:extmap[ext]
else
let type = get(gist.files[filename], 'type', 'text')
endif
silent! exec 'setlocal ft='.tolower(type)
endfunction
function! s:GistWrite(fname) abort
if substitute(a:fname, '\\', '/', 'g') == expand("%:p:gs@\\@/@")
if g:gist_update_on_write != 2 || v:cmdbang
Gist -e
else
echohl ErrorMsg | echomsg 'Please type ":w!" to update a gist.' | echohl None
endif
else
exe 'w'.(v:cmdbang ? '!' : '') fnameescape(v:cmdarg) fnameescape(a:fname)
silent! exe 'file' fnameescape(a:fname)
silent! au! BufWriteCmd <buffer>
endif
endfunction
function! s:GistGet(gistid, clipboard) abort
redraw | echon 'Getting gist... '
let res = webapi#http#get(g:gist_api_url.'gists/'.a:gistid, '', { 'Authorization': s:GistGetAuthHeader() })
if res.status =~# '^2'
try
let gist = webapi#json#decode(res.content)
catch
redraw
echohl ErrorMsg | echomsg 'Gist seems to be broken' | echohl None
return
endtry
if get(g:, 'gist_get_multiplefile', 0) != 0
let num_file = len(keys(gist.files))
else
let num_file = 1
endif
redraw
if num_file > len(keys(gist.files))
echohl ErrorMsg | echomsg 'Gist not found' | echohl None
return
endif
augroup GistWrite
au!
augroup END
for n in range(num_file)
try
let old_undolevels = &undolevels
let filename = sort(keys(gist.files))[n]
let winnum = bufwinnr(bufnr(s:bufprefix.a:gistid.'/'.filename))
if winnum != -1
if winnum != bufwinnr('%')
exe winnum 'wincmd w'
endif
setlocal modifiable
else
if num_file == 1
if get(g:, 'gist_edit_with_buffers', 0)
let found = -1
for wnr in range(1, winnr('$'))
let bnr = winbufnr(wnr)
if bnr != -1 && !empty(getbufvar(bnr, 'gist'))
let found = wnr
break
endif
endfor
if found != -1
exe found 'wincmd w'
setlocal modifiable
else
if get(g:, 'gist_list_vsplit', 0)
exec 'silent noautocmd rightbelow vnew'
else
exec 'silent noautocmd rightbelow new'
endif
endif
else
silent only!
if get(g:, 'gist_list_vsplit', 0)
exec 'silent noautocmd rightbelow vnew'
else
exec 'silent noautocmd rightbelow new'
endif
endif
else
if get(g:, 'gist_list_vsplit', 0)
exec 'silent noautocmd rightbelow vnew'
else
exec 'silent noautocmd rightbelow new'
endif
endif
setlocal noswapfile
silent exec 'noautocmd file' s:bufprefix.a:gistid.'/'.fnameescape(filename)
endif
set undolevels=-1
filetype detect
silent %d _
let content = gist.files[filename].content
call setline(1, split(content, "\n"))
let b:gist = {
\ 'filename': filename,
\ 'id': gist.id,
\ 'description': gist.description,
\ 'private': gist.public =~# 'true',
\}
catch
let &undolevels = old_undolevels
bw!
redraw
echohl ErrorMsg | echomsg 'Gist contains binary' | echohl None
return
endtry
let &undolevels = old_undolevels
setlocal buftype=acwrite bufhidden=hide noswapfile
setlocal nomodified
doau StdinReadPost,BufRead,BufReadPost
let gist_detect_filetype = get(g:, 'gist_detect_filetype', 0)
if (&ft ==# '' && gist_detect_filetype == 1) || gist_detect_filetype == 2
call s:GistDetectFiletype(a:gistid)
endif
if a:clipboard
if exists('g:gist_clip_command')
exec 'silent w !'.g:gist_clip_command
elseif has('clipboard')
silent! %yank +
else
%yank
endif
endif
1
augroup GistWrite
au! BufWriteCmd <buffer> call s:GistWrite(expand("<amatch>"))
augroup END
endfor
else
bw!
redraw
echohl ErrorMsg | echomsg 'Gist not found' | echohl None
return
endif
endfunction
function! s:GistListAction(mode) abort
let line = getline('.')
let mx = '^gist:\s*\zs\(\w\+\)\ze.*'
if line =~# mx
let gistid = matchstr(line, mx)
if a:mode == 1
call s:open_browser('https://gist.github.com/' . gistid)
elseif a:mode == 0
call s:GistGet(gistid, 0)
wincmd w
bw
elseif a:mode == 2
call s:GistGet(gistid, 1)
" TODO close with buffe rname
bdelete
bdelete
elseif a:mode == 3
call s:GistGet(gistid, 1)
" TODO close with buffe rname
bdelete
bdelete
normal! "+p
endif
return
endif
if line =~# '^more\.\.\.$'
call s:GistList(b:gistls, b:page+1, g:gist_per_page_limit)
return
endif
endfunction
function! s:GistUpdate(content, gistid, gistnm, desc) abort
let gist = { 'id': a:gistid, 'files' : {}, 'description': '','public': function('webapi#json#true') }
if exists('b:gist')
if has_key(b:gist, 'filename') && len(a:gistnm) > 0
let gist.files[b:gist.filename] = { 'content': '', 'filename': b:gist.filename }
let b:gist.filename = a:gistnm
endif
if has_key(b:gist, 'private') && b:gist.private | let gist['public'] = function('webapi#json#false') | endif
if has_key(b:gist, 'description') | let gist['description'] = b:gist.description | endif
if has_key(b:gist, 'filename') | let filename = b:gist.filename | endif
else
let filename = a:gistnm
if len(filename) == 0 | let filename = s:GistGetFileName(a:gistid) | endif
if len(filename) == 0 | let filename = s:get_current_filename(1) | endif
endif
let auth = s:GistGetAuthHeader()
if len(auth) == 0
redraw
echohl ErrorMsg | echomsg v:errmsg | echohl None
return
endif
" Update description
" If no new description specified, keep the old description
if a:desc !=# ' '
let gist['description'] = a:desc
else
let res = webapi#http#get(g:gist_api_url.'gists/'.a:gistid, '', { 'Authorization': auth })
if res.status =~# '^2'
let old_gist = webapi#json#decode(res.content)
let gist['description'] = old_gist.description
endif
endif
let gist.files[filename] = { 'content': a:content, 'filename': filename }
redraw | echon 'Updating gist... '
let res = webapi#http#post(g:gist_api_url.'gists/' . a:gistid,
\ webapi#json#encode(gist), {
\ 'Authorization': auth,
\ 'Content-Type': 'application/json',
\})
if res.status =~# '^2'
let obj = webapi#json#decode(res.content)
let loc = obj['html_url']
let b:gist = {'id': a:gistid, 'filename': filename}
setlocal nomodified
redraw | echomsg 'Done: '.loc
else
let loc = ''
echohl ErrorMsg | echomsg 'Post failed: ' . res.message | echohl None
endif
return loc
endfunction
function! s:GistDelete(gistid) abort
let auth = s:GistGetAuthHeader()
if len(auth) == 0
redraw
echohl ErrorMsg | echomsg v:errmsg | echohl None
return
endif
redraw | echon 'Deleting gist... '
let res = webapi#http#post(g:gist_api_url.'gists/'.a:gistid, '', {
\ 'Authorization': auth,
\ 'Content-Type': 'application/json',
\}, 'DELETE')
if res.status =~# '^2'
if exists('b:gist')
unlet b:gist
endif
redraw | echomsg 'Done: '
else
echohl ErrorMsg | echomsg 'Delete failed: ' . res.message | echohl None
endif
endfunction
function! s:get_current_filename(no) abort
let filename = expand('%:t')
if len(filename) == 0 && &ft !=# ''
let pair = filter(items(s:extmap), 'v:val[1] == &ft')
if len(pair) > 0
let filename = printf('gistfile%d%s', a:no, pair[0][0])
endif
endif
if filename ==# ''
let filename = printf('gistfile%d.txt', a:no)
endif
return filename
endfunction
function! s:update_GistID(id) abort
let view = winsaveview()
normal! gg
let ret = 0
if search('\<GistID\>:\s*$')
let line = getline('.')
let line = substitute(line, '\s\+$', '', 'g')
call setline('.', line . ' ' . a:id)
let ret = 1
endif
call winrestview(view)
return ret
endfunction
" GistPost function:
" Post new gist to github
"
" if there is an embedded gist url or gist id in your file,
" it will just update it.
" -- by c9s
"
" embedded gist id format:
"
" GistID: 123123
"
function! s:GistPost(content, private, desc, anonymous) abort
let gist = { 'files' : {}, 'description': '','public': function('webapi#json#true') }
if a:desc !=# ' ' | let gist['description'] = a:desc | endif
if a:private | let gist['public'] = function('webapi#json#false') | endif
let filename = s:get_current_filename(1)
let gist.files[filename] = { 'content': a:content, 'filename': filename }
let header = {'Content-Type': 'application/json'}
if !a:anonymous
let auth = s:GistGetAuthHeader()
if len(auth) == 0
redraw
echohl ErrorMsg | echomsg v:errmsg | echohl None
return
endif
let header['Authorization'] = auth
endif
redraw | echon 'Posting it to gist... '
let res = webapi#http#post(g:gist_api_url.'gists', webapi#json#encode(gist), header)
if res.status =~# '^2'
let obj = webapi#json#decode(res.content)
let loc = obj['html_url']
let b:gist = {
\ 'filename': filename,
\ 'id': matchstr(loc, '[^/]\+$'),
\ 'description': gist['description'],
\ 'private': a:private,
\}
if s:update_GistID(b:gist['id'])
Gist -e
endif
redraw | echomsg 'Done: '.loc
else
let loc = ''
echohl ErrorMsg | echomsg 'Post failed: '. res.message | echohl None
endif
return loc
endfunction
function! s:GistPostBuffers(private, desc, anonymous) abort
let bufnrs = range(1, bufnr('$'))
let bn = bufnr('%')
let query = []
let gist = { 'files' : {}, 'description': '','public': function('webapi#json#true') }
if a:desc !=# ' ' | let gist['description'] = a:desc | endif
if a:private | let gist['public'] = function('webapi#json#false') | endif
let index = 1
for bufnr in bufnrs
if !bufexists(bufnr) || buflisted(bufnr) == 0
continue
endif
echo 'Creating gist content'.index.'... '
silent! exec 'buffer!' bufnr
let content = join(getline(1, line('$')), "\n")
let filename = s:get_current_filename(index)
let gist.files[filename] = { 'content': content, 'filename': filename }
let index = index + 1
endfor
silent! exec 'buffer!' bn
let header = {'Content-Type': 'application/json'}
if !a:anonymous
let auth = s:GistGetAuthHeader()
if len(auth) == 0
redraw
echohl ErrorMsg | echomsg v:errmsg | echohl None
return
endif
let header['Authorization'] = auth
endif
redraw | echon 'Posting it to gist... '
let res = webapi#http#post(g:gist_api_url.'gists', webapi#json#encode(gist), header)
if res.status =~# '^2'
let obj = webapi#json#decode(res.content)
let loc = obj['html_url']
let b:gist = {
\ 'filename': filename,
\ 'id': matchstr(loc, '[^/]\+$'),
\ 'description': gist['description'],
\ 'private': a:private,
\}
if s:update_GistID(b:gist['id'])
Gist -e
endif
redraw | echomsg 'Done: '.loc
else
let loc = ''
echohl ErrorMsg | echomsg 'Post failed: ' . res.message | echohl None
endif
return loc
endfunction
function! gist#Gist(count, bang, line1, line2, ...) abort
redraw
let bufname = bufname('%')
" find GistID: in content , then we should just update
let gistid = ''
let gistls = ''
let gistnm = ''
let gistdesc = ' '
let private = get(g:, 'gist_post_private', 0)
let multibuffer = 0
let clipboard = 0
let deletepost = 0
let editpost = 0
let anonymous = get(g:, 'gist_post_anonymous', 0)
let openbrowser = 0
let setpagelimit = 0
let pagelimit = g:gist_per_page_limit
let listmx = '^\%(-l\|--list\)\s*\([^\s]\+\)\?$'
let bufnamemx = '^' . s:bufprefix .'\(\zs[0-9a-f]\+\ze\|\zs[0-9a-f]\+\ze[/\\].*\)$'
if strlen(g:github_user) == 0 && anonymous == 0
echohl ErrorMsg | echomsg 'You have not configured a Github account. Read '':help gist-setup''.' | echohl None
return
endif
if a:bang == '!'
let gistidbuf = ''
elseif bufname =~# bufnamemx
let gistidbuf = matchstr(bufname, bufnamemx)
elseif exists('b:gist') && has_key(b:gist, 'id')
let gistidbuf = b:gist['id']
else
let gistidbuf = matchstr(join(getline(a:line1, a:line2), "\n"), 'GistID:\s*\zs\w\+')
endif
let args = (a:0 > 0) ? s:shellwords(a:1) : []
for arg in args
if arg =~# '^\(-h\|--help\)$\C'
help :Gist
return
elseif arg =~# '^\(-g\|--git\)$\C' && gistidbuf !=# '' && g:gist_api_url ==# 'https://api.github.com/' && has_key(b:, 'gist') && has_key(b:gist, 'id')
echo printf('git clone [email protected]:%s', b:gist['id'])
return
elseif arg =~# '^\(-G\|--gitclone\)$\C' && gistidbuf !=# '' && g:gist_api_url ==# 'https://api.github.com/' && has_key(b:, 'gist') && has_key(b:gist, 'id')
exe '!' printf('git clone [email protected]:%s', b:gist['id'])
return
elseif setpagelimit == 1
let setpagelimit = 0
let pagelimit = str2nr(arg)
if pagelimit < 1 || pagelimit > 100
echohl ErrorMsg | echomsg 'Page limit should be between 1 and 100: '.arg | echohl None
unlet args
return 0
endif
elseif arg =~# '^\(-la\|--listall\)$\C'
let gistls = '-all'
elseif arg =~# '^\(-ls\|--liststar\)$\C'
let gistls = 'starred'
elseif arg =~# '^\(-l\|--list\)$\C'
if get(g:, 'gist_show_privates')
let gistls = 'mine'
else
let gistls = g:github_user
endif
elseif arg =~# '^\(-m\|--multibuffer\)$\C'
let multibuffer = 1
elseif arg =~# '^\(-p\|--private\)$\C'
let private = 1
elseif arg =~# '^\(-P\|--public\)$\C'
let private = 0
elseif arg =~# '^\(-a\|--anonymous\)$\C'
let anonymous = 1
elseif arg =~# '^\(-s\|--description\)$\C'
let gistdesc = ''
elseif arg =~# '^\(-c\|--clipboard\)$\C'
let clipboard = 1
elseif arg =~# '^--rawurl$\C' && gistidbuf !=# '' && g:gist_api_url ==# 'https://api.github.com/'
let gistid = gistidbuf
echo 'https://gist.github.com/raw/'.gistid
return
elseif arg =~# '^\(-d\|--delete\)$\C' && gistidbuf !=# ''
let gistid = gistidbuf
let deletepost = 1
elseif arg =~# '^\(-e\|--edit\)$\C'
if gistidbuf !=# ''
let gistid = gistidbuf
endif
let editpost = 1
elseif arg =~# '^\(+1\|--star\)$\C' && gistidbuf !=# ''
let auth = s:GistGetAuthHeader()
if len(auth) == 0
echohl ErrorMsg | echomsg v:errmsg | echohl None
else
let gistid = gistidbuf
let res = webapi#http#post(g:gist_api_url.'gists/'.gistid.'/star', '', { 'Authorization': auth }, 'PUT')
if res.status =~# '^2'
echomsg 'Starred' gistid
else
echohl ErrorMsg | echomsg 'Star failed' | echohl None
endif
endif
return
elseif arg =~# '^\(-1\|--unstar\)$\C' && gistidbuf !=# ''
let auth = s:GistGetAuthHeader()
if len(auth) == 0
echohl ErrorMsg | echomsg v:errmsg | echohl None
else
let gistid = gistidbuf
let res = webapi#http#post(g:gist_api_url.'gists/'.gistid.'/star', '', { 'Authorization': auth }, 'DELETE')
if res.status =~# '^2'
echomsg 'Unstarred' gistid
else
echohl ErrorMsg | echomsg 'Unstar failed' | echohl None
endif
endif
return
elseif arg =~# '^\(-f\|--fork\)$\C' && gistidbuf !=# ''
let auth = s:GistGetAuthHeader()
if len(auth) == 0
echohl ErrorMsg | echomsg v:errmsg | echohl None
return
else
let gistid = gistidbuf
let res = webapi#http#post(g:gist_api_url.'gists/'.gistid.'/fork', '', { 'Authorization': auth })
if res.status =~# '^2'
let obj = webapi#json#decode(res.content)
let gistid = obj['id']
else
echohl ErrorMsg | echomsg 'Fork failed' | echohl None
return
endif
endif
elseif arg =~# '^\(-b\|--browser\)$\C'
let openbrowser = 1
elseif arg =~# '^\(-n\|--per-page\)$\C'
if len(gistls) > 0
let setpagelimit = 1
else
echohl ErrorMsg | echomsg 'Page limit can be set only for list commands'.arg | echohl None
unlet args
return 0
endif
elseif arg !~# '^-' && len(gistnm) == 0
if gistdesc !=# ' '
let gistdesc = matchstr(arg, '^\s*\zs.*\ze\s*$')
elseif editpost == 1 || deletepost == 1
let gistnm = arg
elseif len(gistls) > 0 && arg !=# '^\w\+$\C'
let gistls = arg
elseif arg =~# '^[0-9a-z]\+$\C'
let gistid = arg
else
echohl ErrorMsg | echomsg 'Invalid arguments: '.arg | echohl None
unlet args
return 0
endif
elseif len(arg) > 0
echohl ErrorMsg | echomsg 'Invalid arguments: '.arg | echohl None
unlet args
return 0
endif
endfor
unlet args
"echom "gistid=".gistid
"echom "gistls=".gistls
"echom "gistnm=".gistnm
"echom "gistdesc=".gistdesc
"echom "private=".private
"echom "clipboard=".clipboard
"echom "editpost=".editpost
"echom "deletepost=".deletepost
if gistidbuf !=# '' && gistid ==# '' && editpost == 0 && deletepost == 0 && anonymous == 0
let editpost = 1
let gistid = gistidbuf
endif
if len(gistls) > 0
call s:GistList(gistls, 1, pagelimit)
elseif len(gistid) > 0 && editpost == 0 && deletepost == 0
call s:GistGet(gistid, clipboard)
else
let url = ''
if multibuffer == 1
let url = s:GistPostBuffers(private, gistdesc, anonymous)
else
if a:count < 1
let content = join(getline(a:line1, a:line2), "\n")
else
let save_regcont = @"
let save_regtype = getregtype('"')
silent! normal! gvy
let content = @"
call setreg('"', save_regcont, save_regtype)
endif
if editpost == 1
let url = s:GistUpdate(content, gistid, gistnm, gistdesc)
elseif deletepost == 1
call s:GistDelete(gistid)
else
let url = s:GistPost(content, private, gistdesc, anonymous)
endif
if a:count >= 1 && get(g:, 'gist_keep_selection', 0) == 1
silent! normal! gv
endif
endif
if type(url) == 1 && len(url) > 0
if get(g:, 'gist_open_browser_after_post', 0) == 1 || openbrowser
call s:open_browser(url)
endif
let gist_put_url_to_clipboard_after_post = get(g:, 'gist_put_url_to_clipboard_after_post', 1)
if gist_put_url_to_clipboard_after_post > 0 || clipboard
if gist_put_url_to_clipboard_after_post == 2
let url = url . "\n"
endif
if exists('g:gist_clip_command')
call system(g:gist_clip_command, url)
elseif has('clipboard')
let @+ = url
else
let @" = url
endif
endif
endif
endif
return 1
endfunction
function! s:GistGetAuthHeader() abort
if get(g:, 'gist_use_password_in_gitconfig', 0) != 0
let password = substitute(system('git config --get github.password'), "\n", '', '')
if password =~# '^!' | let password = system(password[1:]) | endif
return printf('basic %s', webapi#base64#b64encode(g:github_user.':'.password))
endif
let auth = ''
if !empty(get(g:, 'gist_token', $GITHUB_TOKEN))
let auth = 'token ' . get(g:, 'gist_token', $GITHUB_TOKEN)
elseif filereadable(s:gist_token_file)
let str = join(readfile(s:gist_token_file), '')
if type(str) == 1
let auth = str
endif
endif
if len(auth) > 0
return auth
endif
redraw
echohl WarningMsg
echo 'Gist.vim requires authorization to use the GitHub API. These settings are stored in "~/.gist-vim". If you want to revoke, do "rm ~/.gist-vim".'
echohl None
let password = inputsecret('GitHub Password for '.g:github_user.':')