Skip to content

Commit 3fe6c2d

Browse files
Pablo Cobellimattn
authored andcommitted
First attempt.
Added functionalities to GistListing function, see README file for details. Added pasting into current buffer capability. Also enhanced the formatting of the gist listing. Correcting an error in the previous upload. Enhanced visualization of gist listing and added yank and insert capabilities. Updated README to account for changes. Corrected errors on README. Corrected errors on README. Now opening a gist buffer with Enter closes the listing afterwards. Added documentation to doc/gist-vim.txt, code is now clean for a PR.
1 parent 88c331e commit 3fe6c2d

File tree

3 files changed

+122
-10
lines changed

3 files changed

+122
-10
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
### My modifications
2+
3+
- New mappings on the gist-listing buffer:
4+
- Both `o` or `Enter` open the gist file in a new buffer, and close the
5+
gist-vim listing one.
6+
- `b` opens the gist file in a browser; this is necessary because
7+
`Shift-Enter` (as was originally) only works for GUI vim.
8+
- `y` copies the contents of the selected gist to the clipboard, and
9+
closes the gist-vim buffer.
10+
- `p` pastes the contents of the selected gist to the buffer from where
11+
gist-vim was called, and closes the gist-vim buffer.
12+
- Hitting `Escape` or `Tab` at the gist-vim buffer closes it.
13+
- Gist listing has fixed-length columns now, more amenable to eye inspection.
14+
Every line on the gist-listing buffer contains the gist id, name and
15+
description, in that order. Columns are now padded and truncated to offer a
16+
faster browsing, in the following way:
17+
- The gist id string is fixed at 32 characters.
18+
- The length (in characters) of the name of the gist is fixed and
19+
can be set by the user using, for example:
20+
21+
`let g:gistvim_namelength = 20`
22+
23+
The default value for `gistvim_namelength` is 30. If the gist (file)name
24+
exceeds that length, it is truncated to the specified length.
25+
- Finally, the gist description is truncated in length to fit the remaining
26+
of the line, avoiding wrapped lines that mess up the table layout.
27+
- Note that the gist listing buffer now does not show the field 'code'
28+
(not sure what that did in the first place).
29+
30+
- Now the listing is complete (no need to select 'more' to see the following
31+
gists on the list), in this way the user can later perform a search (using vim's `/`,
32+
for instance) for the sought gist by name, description, etc.
33+
- The first line on the gist-listing states the number of gists listed and the
34+
user whose gists are being listed.
35+
- The height of the gist-listing buffer is now determined in the following way.
36+
If the number of gists listed equals or exceeds 10, then the height is fixed at 10. If
37+
it is smaller than 10, then the height is adjusted so that there are no empty
38+
lines displayed on the buffer. Note: right now this value (10) is fixed; I'm
39+
going to change it to a global plugin variable that defaults to 10.
40+
141
### Gist.vim
242

343
This is a vimscript for creating gists (http://gist.github.com).

autoload/gist.vim

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,23 @@ function! s:format_gist(gist) abort
132132
else
133133
let code = ''
134134
endif
135-
let desc = type(a:gist.description)==0 || a:gist.description ==# '' ? '' : '('.a:gist.description.')'
135+
let desc = type(a:gist.description)==0 || a:gist.description ==# '' ? '' : a:gist.description
136136
let name = substitute(name, '[\r\n\t]', ' ', 'g')
137137
let name = substitute(name, ' ', ' ', 'g')
138138
let desc = substitute(desc, '[\r\n\t]', ' ', 'g')
139139
let desc = substitute(desc, ' ', ' ', 'g')
140-
return printf('gist: %s %s %s%s', a:gist.id, name, desc, code)
140+
" return printf('gist: %-32s %s %s%s', a:gist.id, name, desc, code)
141+
" Display a nice formatted (and truncated if needed) table of gists on screen
142+
" Calculate field lengths for gist-listing formatting on screen
143+
redir =>a |exe "sil sign place buffer=".bufnr('')|redir end
144+
let signlist = split(a, '\n')
145+
let width = winwidth(0) - ((&number||&relativenumber) ? &numberwidth : 0) - &foldcolumn - (len(signlist) > 2 ? 2 : 0)
146+
let s:gistlen = 33
147+
if !exists("g:gist_namelength")
148+
let g:gist_namelength=30
149+
endif
150+
let s:desclen = width-(s:gistlen+g:gist_namelength+10)
151+
return printf('gist: %-*s %-*s %-*s', s:gistlen, a:gist.id, g:gist_namelength+1, strpart(name, 0, g:gist_namelength), s:desclen+1, strpart(desc, 0, s:desclen))
141152
endfunction
142153

143154
" Note: A colon in the file name has side effects on Windows due to NTFS Alternate Data Streams; avoid it.
@@ -206,17 +217,38 @@ function! s:GistList(gistls, page) abort
206217
let lines = map(filter(content, '!empty(v:val.files)'), 's:format_gist(v:val)')
207218
call setline(1, split(join(lines, "\n"), "\n"))
208219

209-
$put='more...'
220+
let numlines = line('$')
221+
if numlines > 1
222+
let plural='s'
223+
else
224+
let plural=''
225+
endif
226+
" $put='more...'.line('$').' gist'.plural.' shown.'
227+
call append(0, ' '.a:gistls.': '.numlines.' gist'.plural)
228+
229+
if numlines+1 > 11
230+
let listheight = 11
231+
else
232+
let listheight = numlines+1
233+
endif
234+
execute 'resize ' . listheight
210235

211236
let b:gistls = a:gistls
212237
let b:page = a:page
213238
setlocal buftype=nofile bufhidden=hide noswapfile
239+
setlocal cursorline
214240
setlocal nomodified
215241
setlocal nomodifiable
216-
syntax match SpecialKey /^gist:/he=e-1
242+
syntax match SpecialKey /^gist:/he=e
217243
syntax match Title /^gist: \S\+/hs=s+5 contains=ALL
218244
nnoremap <silent> <buffer> <cr> :call <SID>GistListAction(0)<cr>
219-
nnoremap <silent> <buffer> <s-cr> :call <SID>GistListAction(1)<cr>
245+
nnoremap <silent> <buffer> o :call <SID>GistListAction(0)<cr>
246+
nnoremap <silent> <buffer> b :call <SID>GistListAction(1)<cr>
247+
nnoremap <silent> <buffer> y :call <SID>GistListAction(2)<cr>
248+
nnoremap <silent> <buffer> p :call <SID>GistListAction(3)<cr>
249+
nnoremap <silent> <buffer> <esc> :bw<cr>
250+
" Next line (in original code) only works on GUI VIM
251+
nnoremap <silent> <buffer> <s-cr> :call <SID>GistListAction(1)<cr>
220252
221253
cal cursor(1+len(oldlines),1)
222254
nohlsearch
@@ -401,6 +433,7 @@ function! s:GistGet(gistid, clipboard) abort
401433
endif
402434
else
403435
silent only!
436+
" exec 'silent noautocmd edit'
404437
if get(g:, 'gist_list_vsplit', 0)
405438
exec 'silent noautocmd rightbelow vnew'
406439
else
@@ -437,7 +470,8 @@ function! s:GistGet(gistid, clipboard) abort
437470
return
438471
endtry
439472
let &undolevels = old_undolevels
440-
setlocal buftype=acwrite bufhidden=delete noswapfile
473+
" setlocal buftype=acwrite bufhidden=delete noswapfile
474+
setlocal buftype=acwrite bufhidden=hide noswapfile
441475
setlocal nomodified
442476
doau StdinReadPost,BufRead,BufReadPost
443477
let gist_detect_filetype = get(g:, 'gist_detect_filetype', 0)
@@ -471,10 +505,22 @@ function! s:GistListAction(shift) abort
471505
let mx = '^gist:\s*\zs\(\w\+\)\ze.*'
472506
if line =~# mx
473507
let gistid = matchstr(line, mx)
474-
if a:shift
508+
if a:shift == 1
475509
call s:open_browser('https://gist.github.com/' . gistid)
476-
else
510+
elseif a:shift == 0
477511
call s:GistGet(gistid, 0)
512+
wincmd w
513+
" bdelete
514+
bw
515+
elseif a:shift == 2
516+
call s:GistGet(gistid, 1)
517+
bdelete
518+
bdelete
519+
elseif a:shift == 3
520+
call s:GistGet(gistid, 1)
521+
bdelete
522+
bdelete
523+
normal! "+p
478524
endif
479525
return
480526
endif

doc/gist-vim.txt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,21 @@ USAGE *:Gist* *gist-vim-usage*
116116
>
117117
:Gist -ls
118118
:Gist --liststar
119-
<
119+
120+
- While the gist list is visible, the following mappings apply:
121+
122+
- 'o' or 'Enter' will open the selected gist file in a new buffer
123+
and close the gist-vim listing split.
124+
- 'b' will open the selected gist file in a browser. If you are in
125+
GUI vim you can also achieve this by pressing 'Shift-Enter'.
126+
- 'y' will copy the contents of the selected gist to the clipboard,
127+
and close the gist-vim listing split.
128+
- 'p' will (copy and) paste the contents of the selected gist to the
129+
buffer from which gist-vim was called, and close the gist-vim
130+
listing split.
131+
- 'Esc' will close the gist-vim listing split without performing any
132+
further action.
133+
120134
- Open the gist on browser after you post or update it.
121135
>
122136
:Gist -b
@@ -201,7 +215,19 @@ If you want to update a gist, embed >
201215
in your local file, then call >
202216
203217
:Gist
204-
>
218+
219+
The gist-vim listing split lists gists ids, names (filenames) as well as
220+
their description. This is done following a table layout, with fixed space
221+
for each column. For offering quick browsing, gist-vim will truncate all
222+
output exceeding the available horizontal space, assuring that every gist
223+
listed only takes one line on the table. Although the gist id field width is
224+
fixed internally, the user can define the length of the (file)name field on
225+
the gist-vim listing. This can be done by the following declaration:
226+
227+
let g:gist_namelength = 20
228+
229+
Note that the default value for gist_namelength is 30. Again, if the gist
230+
(file)name exceeds the specified number of characters, it will be truncated.
205231

206232
If you want to update a gist when only |:w!|: >
207233

0 commit comments

Comments
 (0)