Skip to content

Commit

Permalink
Manage per-page limit while listing
Browse files Browse the repository at this point in the history
Control how many gists we're going to get during list commands. Two ways
introduced:

1. Global variable `g:gist_per_page_limit` which is 30 by default. Set
   it to the value that will be used as a default number of gists per
   page in list commands.
2. Specify the number of gists for any list command explicitly `-n` or
   `--per-page` option:

```
:Gist -l -n 100
:Gist --list --per-page 100
```

Note that the page limit could not exceed 100 and should be a positive
number.
  • Loading branch information
dantonyuk authored and allex committed May 18, 2022
1 parent 04dbb9a commit f1a0858
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ For the latest version please see https://github.com/mattn/vim-gist.

:Gist -l mattn

- Specify the number of gists listed:

:Gist -l -n 100

- List everyone's gists.

:Gist -la
Expand Down Expand Up @@ -188,6 +192,10 @@ You need to either set global git config:

$ git config --global github.user Username

If you want to list more than 30 gists per page (maximum is 100):

let g:gist_per_page_limit = 100

## License:

Copyright 2010 by Yasuhiro Matsumoto
Expand Down
28 changes: 24 additions & 4 deletions autoload/gist.vim
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ 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) abort
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'
Expand All @@ -196,9 +196,11 @@ function! s:GistList(gistls, page) abort
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
let url = url . '&page=' . a:page
endif

setlocal modifiable
Expand Down Expand Up @@ -527,7 +529,7 @@ function! s:GistListAction(mode) abort
return
endif
if line =~# '^more\.\.\.$'
call s:GistList(b:gistls, b:page+1)
call s:GistList(b:gistls, b:page+1, g:gist_per_page_limit)
return
endif
endfunction
Expand Down Expand Up @@ -767,6 +769,8 @@ function! gist#Gist(count, bang, line1, line2, ...) abort
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
Expand Down Expand Up @@ -794,6 +798,14 @@ function! gist#Gist(count, bang, line1, line2, ...) abort
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'
Expand Down Expand Up @@ -874,6 +886,14 @@ function! gist#Gist(count, bang, line1, line2, ...) abort
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*$')
Expand Down Expand Up @@ -910,7 +930,7 @@ function! gist#Gist(count, bang, line1, line2, ...) abort
endif

if len(gistls) > 0
call s:GistList(gistls, 1)
call s:GistList(gistls, 1, pagelimit)
elseif len(gistid) > 0 && editpost == 0 && deletepost == 0
call s:GistGet(gistid, clipboard)
else
Expand Down
6 changes: 6 additions & 0 deletions doc/gist-vim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ USAGE *:Gist* *vim-gist-usage*
:Gist -l
:Gist --list
:Gist -l -n 100
:Gist --list --per-page 100
<
- List gists from user "mattn". >
Expand Down Expand Up @@ -201,6 +203,10 @@ If you want to open gist list/buffer as vertical split: >
let g:gist_list_vsplit = 1
If you want to list more than 30 gists per page (maximum is 100):

let g:gist_per_page_limit = 100

If you want to modify filetype for the file on github, or add mapping of
filetype/file-extension: >
Expand Down
5 changes: 3 additions & 2 deletions plugin/gist.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ endif
let g:loaded_gist_vim = 1

function! s:CompleteArgs(arg_lead,cmdline,cursor_pos)
return filter(copy(["-p", "-P", "-a", "-m", "-e", "-s", "-d", "+1", "-1", "-f", "-c", "-l", "-la", "-ls", "-b",
return filter(copy(["-p", "-P", "-a", "-m", "-e", "-s", "-d", "+1", "-1", "-f", "-c", "-l", "-la", "-ls", "-b", "-n",
\ "--listall", "--liststar", "--list", "--multibuffer", "--private", "--public", "--anonymous", "--description", "--clipboard",
\ "--rawurl", "--delete", "--edit", "--star", "--unstar", "--fork", "--browser"
\ "--rawurl", "--delete", "--edit", "--star", "--unstar", "--fork", "--browser", "--per-page"
\ ]), 'stridx(v:val, a:arg_lead)==0')
endfunction

let g:gist_per_page_limit = get(g:, 'gist_per_page_limit', 30)
command! -nargs=? -range=% -bang -complete=customlist,s:CompleteArgs Gist :call gist#Gist(<count>, "<bang>", <line1>, <line2>, <f-args>)

" vim:set et:

0 comments on commit f1a0858

Please sign in to comment.