@@ -67,8 +67,7 @@ def hex(s):
67
67
68
68
69
69
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" )
72
71
vim .command ("setlocal noswapfile" )
73
72
vim .command ("setlocal buftype=nofile" )
74
73
@@ -81,15 +80,23 @@ def main():
81
80
bwrite ("" )
82
81
83
82
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 :
85
95
news1 = json .loads (urlopen (API_URL + "/news" , timeout = 5 )
86
96
.read ().decode ('utf-8' ))
87
97
news2 = json .loads (urlopen (API_URL + "/news2" , timeout = 5 )
88
98
.read ().decode ('utf-8' ))
89
99
items = news1 + news2
90
- else :
91
- items = json .loads (urlopen (API_URL + "/" + stories , timeout = 5 )
92
- .read ().decode ('utf-8' ))
93
100
except HTTPError :
94
101
print ("HackerNews.vim Error: %s" % str (sys .exc_info ()[1 ].reason ))
95
102
return
@@ -122,46 +129,44 @@ def main():
122
129
vim .command ("setlocal undolevels=100" )
123
130
124
131
125
- def link (external = False ):
132
+ def link (item_id = None , url = None , external = False ):
126
133
line = vim .current .line
127
134
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 )
135
140
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
146
145
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 :]
161
162
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 " , "" )
165
170
166
171
if url and url .find ("news.ycombinator.com/item?id=" ) > 0 :
167
172
item_id = url [url .find ("item?id=" )+ 8 :]
@@ -252,14 +257,18 @@ def link(external=False):
252
257
def save_pos ():
253
258
marks = vim .eval ("g:hackernews_marks" )
254
259
m = hex (vim .current .buffer [0 ])
260
+ if not m :
261
+ return
255
262
marks [m ] = list (vim .current .window .cursor )
256
263
marks [m ].append (vim .eval ("&syntax" ))
257
264
vim .command ("let g:hackernews_marks = %s" % str (marks ))
258
265
259
266
260
- def recall_pos ():
267
+ def recall_pos (cmd ):
261
268
marks = vim .eval ("g:hackernews_marks" )
262
269
m = hex (vim .current .buffer [0 ])
270
+ if not m :
271
+ vim .command (cmd )
263
272
if m in marks :
264
273
mark = marks [m ]
265
274
vim .current .window .cursor = (int (mark [0 ]), int (mark [1 ]))
0 commit comments