diff options
| author | ryanss | 2015-09-04 23:59:00 -0400 |
|---|---|---|
| committer | ryanss | 2015-09-04 23:59:00 -0400 |
| commit | b4f251bf4ba95d6d9c1b03117274a3470588e5f8 (patch) | |
| tree | 07ee78d9d3ba9dc2599c28aff94fcc69da3315f4 | |
| parent | eb354aa1716d0fad88b2e59565599020a4af9706 (diff) | |
| download | vim-hn-b4f251bf4ba95d6d9c1b03117274a3470588e5f8.tar.gz | |
Improve handling of optional arg
An item_id can now be passed as the optional argument to the `:Hackernews`
command which will load that specific item into the .hackernews buffer,
whether it be a story, poll, comment thread or individual comment.
Ex. `:Hackernews 9015621`
Close #35
| -rw-r--r-- | README.md | 11 | ||||
| -rw-r--r-- | ftplugin/hackernews.py | 93 | ||||
| -rw-r--r-- | ftplugin/hackernews.vim | 4 | ||||
| -rw-r--r-- | plugin/hackernews.vim | 21 | ||||
| -rw-r--r-- | tests.vader | 4 |
5 files changed, 62 insertions, 71 deletions
@@ -18,15 +18,8 @@ Basic Usage * Open the Hacker News front page in Vim by executing the `:HackerNews` command * The HackerNews command takes an optional parameter to view items other - than the top stories on the front page: - * `:HackerNews ask` - * `:HackerNews show` - * `:HackerNews shownew` - * `:HackerNews jobs` - * `:HackerNews best` - * `:HackerNews active` - * `:HackerNews newest` - * `:HackerNews noobstories` + than the top stories on the front page: `ask`, `show`, `shownew`, `jobs`, + `best`, `active`, `newest`, `noobstories`, or `item id` * Press lowercase `o` to open links in Vim * Press uppercase `O` to open links in default web browser * Numbered lines with story titles on the front page link to the story url diff --git a/ftplugin/hackernews.py b/ftplugin/hackernews.py index 94f1a90..836f227 100644 --- a/ftplugin/hackernews.py +++ b/ftplugin/hackernews.py @@ -67,8 +67,7 @@ def hex(s): def main(): - stories = vim.eval("g:hackernews_stories") or "news" - vim.command("edit %s.hackernews" % (stories if stories != "news" else "")) + vim.command("edit .hackernews") vim.command("setlocal noswapfile") vim.command("setlocal buftype=nofile") @@ -81,15 +80,23 @@ def main(): bwrite("") try: - if stories == "news": + arg = vim.eval("g:hackernews_arg") + if arg in ['newest', 'ask', 'show', 'shownew', 'jobs', + 'best', 'active', 'noobstories']: + items = json.loads(urlopen(API_URL+"/"+arg, timeout=5) + .read().decode('utf-8')) + elif arg.isdigit(): + link(item_id=arg) + return + elif arg[:4] == 'http': + link(url=arg) + return + else: news1 = json.loads(urlopen(API_URL+"/news", timeout=5) .read().decode('utf-8')) news2 = json.loads(urlopen(API_URL+"/news2", timeout=5) .read().decode('utf-8')) items = news1 + news2 - else: - items = json.loads(urlopen(API_URL+"/"+stories, timeout=5) - .read().decode('utf-8')) except HTTPError: print("HackerNews.vim Error: %s" % str(sys.exc_info()[1].reason)) return @@ -122,46 +129,44 @@ def main(): vim.command("setlocal undolevels=100") -def link(external=False): +def link(item_id=None, url=None, external=False): line = vim.current.line - item_id = None - url = None - - # Search for Hacker News [item id] - m = re.search(r"\[([0-9]{3,})\]$", line) - if m: - item_id = m.group(1) + if not (item_id or url): + # Search for Hacker News [item id] + m = re.search(r"\[([0-9]{3,})\]$", line) + if m: + item_id = m.group(1) - else: - # Search for [http] link - b = vim.current.buffer - y, x = vim.current.window.cursor - y -= 1 - while b[y].find("[http") < 0 and y >= 0: - # The line we were on had no part of a link in it - if b[y-1].find("]") > 0 \ - and b[y-1].find("]") > b[y-1].find("[http"): - return + else: + # Search for [http] link + b = vim.current.buffer + y, x = vim.current.window.cursor y -= 1 - start = y - loc = max(b[y].find("[http", x, b[y].find("]", x)), - b[y].rfind("[http", 0, x)) - if loc >= 0: - if b[y].find("]", loc) >= 0: - a = loc + 1 - e = b[y].find("]", loc) - url = b[y][a:e] - else: - url = b[y][loc:] - y += 1 - while b[y].find("]") < 0: - if y != start: - url += b[y] + while b[y].find("[http") < 0 and y >= 0: + # The line we were on had no part of a link in it + if b[y-1].find("]") > 0 \ + and b[y-1].find("]") > b[y-1].find("[http"): + return + y -= 1 + start = y + loc = max(b[y].find("[http", x, b[y].find("]", x)), + b[y].rfind("[http", 0, x)) + if loc >= 0: + if b[y].find("]", loc) >= 0: + a = loc + 1 + e = b[y].find("]", loc) + url = b[y][a:e] + else: + url = b[y][loc:] y += 1 - if y != start: - url += b[y][:b[y].find("]")] - url = url.replace(" ", "").replace("\n", "") + while b[y].find("]") < 0: + if y != start: + url += b[y] + y += 1 + if y != start: + url += b[y][:b[y].find("]")] + url = url.replace(" ", "").replace("\n", "") if url and url.find("news.ycombinator.com/item?id=") > 0: item_id = url[url.find("item?id=")+8:] @@ -252,14 +257,18 @@ def link(external=False): def save_pos(): marks = vim.eval("g:hackernews_marks") m = hex(vim.current.buffer[0]) + if not m: + return marks[m] = list(vim.current.window.cursor) marks[m].append(vim.eval("&syntax")) vim.command("let g:hackernews_marks = %s" % str(marks)) -def recall_pos(): +def recall_pos(cmd): marks = vim.eval("g:hackernews_marks") m = hex(vim.current.buffer[0]) + if not m: + vim.command(cmd) if m in marks: mark = marks[m] vim.current.window.cursor = (int(mark[0]), int(mark[1])) diff --git a/ftplugin/hackernews.vim b/ftplugin/hackernews.vim index ec07e94..501d443 100644 --- a/ftplugin/hackernews.vim +++ b/ftplugin/hackernews.vim @@ -41,10 +41,10 @@ noremap <buffer> O :Python hackernews.link(external=True)<cr> noremap <buffer> gx :Python hackernews.link(external=True)<cr> noremap <buffer> u :Python hackernews.save_pos()<cr> \u - \:Python hackernews.recall_pos()<cr> + \:Python hackernews.recall_pos("undo")<cr> noremap <buffer> <C-R> :Python hackernews.save_pos()<cr> \<C-R> - \:Python hackernews.recall_pos()<cr> + \:Python hackernews.recall_pos("redo")<cr> " Helper motions to browse front page, comments and articles easier diff --git a/plugin/hackernews.vim b/plugin/hackernews.vim index bbe8200..cb0bff1 100644 --- a/plugin/hackernews.vim +++ b/plugin/hackernews.vim @@ -16,8 +16,8 @@ au! BufRead,BufNewFile *.hackernews set filetype=hackernews " Set required defaults -if !exists("g:hackernews_stories") - let g:hackernews_stories = 'news' +if !exists("g:hackernews_arg") + let g:hackernews_arg = 'news' endif if !exists("g:hackernews_marks") @@ -27,20 +27,9 @@ endif function! HackerNews(...) if a:0 > 0 - let available_lists = ['news', 'newest', 'ask', 'show', 'shownew', - \'jobs', 'best', 'active', 'noobstories'] - if index(available_lists, a:1) >= 0 - let g:hackernews_stories = a:1 - let stories = a:1 - else - let g:hackernews_stories = 'news' - let stories = '' - end - else - let g:hackernews_stories = 'news' - let stories = '' - end - execute "edit " . stories . ".hackernews" + let g:hackernews_arg = a:1 + endif + execute "edit .hackernews" normal! gg endfunction diff --git a/tests.vader b/tests.vader index c9dd9f8..e45df0e 100644 --- a/tests.vader +++ b/tests.vader @@ -73,8 +73,8 @@ Expect (Keys Mapped): o *@:Python hackernews.link()<CR> O *@:Python hackernews.link(external=True)<CR> gx *@:Python hackernews.link(external=True)<CR> - u *@:Python hackernews.save_pos()<CR>u:Python hackernews.recall_pos()<CR> - <C-R> *@:Python hackernews.save_pos()<CR><C-R>:Python hackernews.recall_pos()<CR> + u *@:Python hackernews.save_pos()<CR>u:Python hackernews.recall_pos("undo")<CR> + <C-R> *@:Python hackernews.save_pos()<CR><C-R>:Python hackernews.recall_pos("redo")<CR> Do (Test opening link item w/ url): :HackerNews\<cr> |
