Skip to content

Commit b4f251b

Browse files
author
ryanss
committed
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
1 parent eb354aa commit b4f251b

File tree

5 files changed

+62
-71
lines changed

5 files changed

+62
-71
lines changed

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,8 @@ Basic Usage
1818

1919
* Open the Hacker News front page in Vim by executing the `:HackerNews` command
2020
* The HackerNews command takes an optional parameter to view items other
21-
than the top stories on the front page:
22-
* `:HackerNews ask`
23-
* `:HackerNews show`
24-
* `:HackerNews shownew`
25-
* `:HackerNews jobs`
26-
* `:HackerNews best`
27-
* `:HackerNews active`
28-
* `:HackerNews newest`
29-
* `:HackerNews noobstories`
21+
than the top stories on the front page: `ask`, `show`, `shownew`, `jobs`,
22+
`best`, `active`, `newest`, `noobstories`, or `item id`
3023
* Press lowercase `o` to open links in Vim
3124
* Press uppercase `O` to open links in default web browser
3225
* Numbered lines with story titles on the front page link to the story url

ftplugin/hackernews.py

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def hex(s):
6767

6868

6969
def main():
70-
stories = vim.eval("g:hackernews_stories") or "news"
71-
vim.command("edit %s.hackernews" % (stories if stories != "news" else ""))
70+
vim.command("edit .hackernews")
7271
vim.command("setlocal noswapfile")
7372
vim.command("setlocal buftype=nofile")
7473

@@ -81,15 +80,23 @@ def main():
8180
bwrite("")
8281

8382
try:
84-
if stories == "news":
83+
arg = vim.eval("g:hackernews_arg")
84+
if arg in ['newest', 'ask', 'show', 'shownew', 'jobs',
85+
'best', 'active', 'noobstories']:
86+
items = json.loads(urlopen(API_URL+"/"+arg, timeout=5)
87+
.read().decode('utf-8'))
88+
elif arg.isdigit():
89+
link(item_id=arg)
90+
return
91+
elif arg[:4] == 'http':
92+
link(url=arg)
93+
return
94+
else:
8595
news1 = json.loads(urlopen(API_URL+"/news", timeout=5)
8696
.read().decode('utf-8'))
8797
news2 = json.loads(urlopen(API_URL+"/news2", timeout=5)
8898
.read().decode('utf-8'))
8999
items = news1 + news2
90-
else:
91-
items = json.loads(urlopen(API_URL+"/"+stories, timeout=5)
92-
.read().decode('utf-8'))
93100
except HTTPError:
94101
print("HackerNews.vim Error: %s" % str(sys.exc_info()[1].reason))
95102
return
@@ -122,46 +129,44 @@ def main():
122129
vim.command("setlocal undolevels=100")
123130

124131

125-
def link(external=False):
132+
def link(item_id=None, url=None, external=False):
126133
line = vim.current.line
127134

128-
item_id = None
129-
url = None
130-
131-
# Search for Hacker News [item id]
132-
m = re.search(r"\[([0-9]{3,})\]$", line)
133-
if m:
134-
item_id = m.group(1)
135+
if not (item_id or url):
136+
# Search for Hacker News [item id]
137+
m = re.search(r"\[([0-9]{3,})\]$", line)
138+
if m:
139+
item_id = m.group(1)
135140

136-
else:
137-
# Search for [http] link
138-
b = vim.current.buffer
139-
y, x = vim.current.window.cursor
140-
y -= 1
141-
while b[y].find("[http") < 0 and y >= 0:
142-
# The line we were on had no part of a link in it
143-
if b[y-1].find("]") > 0 \
144-
and b[y-1].find("]") > b[y-1].find("[http"):
145-
return
141+
else:
142+
# Search for [http] link
143+
b = vim.current.buffer
144+
y, x = vim.current.window.cursor
146145
y -= 1
147-
start = y
148-
loc = max(b[y].find("[http", x, b[y].find("]", x)),
149-
b[y].rfind("[http", 0, x))
150-
if loc >= 0:
151-
if b[y].find("]", loc) >= 0:
152-
a = loc + 1
153-
e = b[y].find("]", loc)
154-
url = b[y][a:e]
155-
else:
156-
url = b[y][loc:]
157-
y += 1
158-
while b[y].find("]") < 0:
159-
if y != start:
160-
url += b[y]
146+
while b[y].find("[http") < 0 and y >= 0:
147+
# The line we were on had no part of a link in it
148+
if b[y-1].find("]") > 0 \
149+
and b[y-1].find("]") > b[y-1].find("[http"):
150+
return
151+
y -= 1
152+
start = y
153+
loc = max(b[y].find("[http", x, b[y].find("]", x)),
154+
b[y].rfind("[http", 0, x))
155+
if loc >= 0:
156+
if b[y].find("]", loc) >= 0:
157+
a = loc + 1
158+
e = b[y].find("]", loc)
159+
url = b[y][a:e]
160+
else:
161+
url = b[y][loc:]
161162
y += 1
162-
if y != start:
163-
url += b[y][:b[y].find("]")]
164-
url = url.replace(" ", "").replace("\n", "")
163+
while b[y].find("]") < 0:
164+
if y != start:
165+
url += b[y]
166+
y += 1
167+
if y != start:
168+
url += b[y][:b[y].find("]")]
169+
url = url.replace(" ", "").replace("\n", "")
165170

166171
if url and url.find("news.ycombinator.com/item?id=") > 0:
167172
item_id = url[url.find("item?id=")+8:]
@@ -252,14 +257,18 @@ def link(external=False):
252257
def save_pos():
253258
marks = vim.eval("g:hackernews_marks")
254259
m = hex(vim.current.buffer[0])
260+
if not m:
261+
return
255262
marks[m] = list(vim.current.window.cursor)
256263
marks[m].append(vim.eval("&syntax"))
257264
vim.command("let g:hackernews_marks = %s" % str(marks))
258265

259266

260-
def recall_pos():
267+
def recall_pos(cmd):
261268
marks = vim.eval("g:hackernews_marks")
262269
m = hex(vim.current.buffer[0])
270+
if not m:
271+
vim.command(cmd)
263272
if m in marks:
264273
mark = marks[m]
265274
vim.current.window.cursor = (int(mark[0]), int(mark[1]))

ftplugin/hackernews.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ noremap <buffer> O :Python hackernews.link(external=True)<cr>
4141
noremap <buffer> gx :Python hackernews.link(external=True)<cr>
4242
noremap <buffer> u :Python hackernews.save_pos()<cr>
4343
\u
44-
\:Python hackernews.recall_pos()<cr>
44+
\:Python hackernews.recall_pos("undo")<cr>
4545
noremap <buffer> <C-R> :Python hackernews.save_pos()<cr>
4646
\<C-R>
47-
\:Python hackernews.recall_pos()<cr>
47+
\:Python hackernews.recall_pos("redo")<cr>
4848
4949

5050
" Helper motions to browse front page, comments and articles easier

plugin/hackernews.vim

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ au! BufRead,BufNewFile *.hackernews set filetype=hackernews
1616

1717

1818
" Set required defaults
19-
if !exists("g:hackernews_stories")
20-
let g:hackernews_stories = 'news'
19+
if !exists("g:hackernews_arg")
20+
let g:hackernews_arg = 'news'
2121
endif
2222

2323
if !exists("g:hackernews_marks")
@@ -27,20 +27,9 @@ endif
2727

2828
function! HackerNews(...)
2929
if a:0 > 0
30-
let available_lists = ['news', 'newest', 'ask', 'show', 'shownew',
31-
\'jobs', 'best', 'active', 'noobstories']
32-
if index(available_lists, a:1) >= 0
33-
let g:hackernews_stories = a:1
34-
let stories = a:1
35-
else
36-
let g:hackernews_stories = 'news'
37-
let stories = ''
38-
end
39-
else
40-
let g:hackernews_stories = 'news'
41-
let stories = ''
42-
end
43-
execute "edit " . stories . ".hackernews"
30+
let g:hackernews_arg = a:1
31+
endif
32+
execute "edit .hackernews"
4433
normal! gg
4534
endfunction
4635

tests.vader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ Expect (Keys Mapped):
7373
o *@:Python hackernews.link()<CR>
7474
O *@:Python hackernews.link(external=True)<CR>
7575
gx *@:Python hackernews.link(external=True)<CR>
76-
u *@:Python hackernews.save_pos()<CR>u:Python hackernews.recall_pos()<CR>
77-
<C-R> *@:Python hackernews.save_pos()<CR><C-R>:Python hackernews.recall_pos()<CR>
76+
u *@:Python hackernews.save_pos()<CR>u:Python hackernews.recall_pos("undo")<CR>
77+
<C-R> *@:Python hackernews.save_pos()<CR><C-R>:Python hackernews.recall_pos("redo")<CR>
7878

7979
Do (Test opening link item w/ url):
8080
:HackerNews\<cr>

0 commit comments

Comments
 (0)