diff options
| author | ryanss | 2015-02-10 10:14:09 -0500 |
|---|---|---|
| committer | ryanss | 2015-02-10 10:14:09 -0500 |
| commit | 353a05e29dacb8c7f8c2d9ce31336d96dd65918a (patch) | |
| tree | d1833e28da2f780c5380a24c8ce6d48e04350714 | |
| parent | bf2266ce059684c5b35424a030d37eee97399e3a (diff) | |
| download | vim-hn-353a05e29dacb8c7f8c2d9ce31336d96dd65918a.tar.gz | |
Wait for :HackerNews command to load python code Fixes #22
Once you import a python module into an instance of vim it does not get
reloaded when a plugin manager updates the plugin. This can cause errors
when, for example, a new function is added to the python module and
updated vim code is trying to call it. This commit does two things:
1) Does not load the python code until the :HackerNews command is
called. There is no need to load the python code during vim startup if
the plugin will not be used.
2) Each time the :HackerNews command is issued and a .hackernews buffer
is opened the python code is reloaded to avoid the errors previously
mentioned.
| -rw-r--r-- | ftplugin/hackernews.py (renamed from plugin/hackernews.py) | 12 | ||||
| -rw-r--r-- | ftplugin/hackernews.vim | 30 | ||||
| -rw-r--r-- | plugin/hackernews.vim | 22 |
3 files changed, 36 insertions, 28 deletions
diff --git a/plugin/hackernews.py b/ftplugin/hackernews.py index 0810b9f..78fcff0 100644 --- a/plugin/hackernews.py +++ b/ftplugin/hackernews.py @@ -45,13 +45,13 @@ def bwrite(s): b.append(s) -def hacker_news(): +def main(): vim.command("edit .hackernews") vim.command("setlocal noswapfile") vim.command("setlocal buftype=nofile") bwrite("┌───┐") - bwrite("│ Y │ Hacker News (news.ycombinator.com)") + bwrite("│ Y │ Hacker New (news.ycombinator.com)") bwrite("└───┘") bwrite("") @@ -89,7 +89,7 @@ def hacker_news(): bwrite("") -def hacker_news_link(external=False): +def link(external=False): line = vim.current.line # Search for Hacker News [item id] @@ -111,7 +111,7 @@ def hacker_news_link(external=False): return save_pos() - vim.command("edit .hackernews") + del vim.current.buffer[:] if 'domain' in item: bwrite("%s (%s)" % (item['title'], item['domain'])) else: @@ -169,7 +169,7 @@ def hacker_news_link(external=False): print "HackerNews.vim Error: HTTP Request Timeout" return save_pos() - vim.command("edit .hackernews") + del vim.current.buffer[:] if 'domain' in item: bwrite("%s (%s)" % (item['title'], item['domain'])) else: @@ -203,7 +203,7 @@ def hacker_news_link(external=False): print "HackerNews.vim Error: HTTP Request Timeout" return save_pos() - vim.command("edit .hackernews") + del vim.current.buffer[:] for i, line in enumerate(content.split('\n')): if not line: bwrite("") diff --git a/ftplugin/hackernews.vim b/ftplugin/hackernews.vim index b5d9372..023ac99 100644 --- a/ftplugin/hackernews.vim +++ b/ftplugin/hackernews.vim @@ -8,12 +8,34 @@ " Version: 0.1.1 +if !has('python') + echo "HackerNews.vim Error: Requires Vim compiled with +python" + finish +endif + if !exists("g:hackernews_marks") let g:hackernews_marks = {} endif -noremap <buffer> o :python hacker_news_link()<cr> -noremap <buffer> O :python hacker_news_link(external=True)<cr> -noremap <buffer> u u:python recall_pos()<cr> -noremap <buffer> <C-r> <C-r>:python recall_pos()<cr> +" Import Python code +execute "python import sys" +execute "python sys.path.append(r'" . expand("<sfile>:p:h") . "')" + +python << EOF +if 'hackernews' not in sys.modules: + import hackernews +else: + # Reload python module to avoid errors when updating plugin + hackernews = reload(hackernews) +EOF + + +" Load front page +execute "python hackernews.main()" + + +noremap <buffer> o :python hackernews.link()<cr> +noremap <buffer> O :python hackernews.link(external=True)<cr> +noremap <buffer> u u:python hackernews.recall_pos()<cr> +noremap <buffer> <C-r> <C-r>:python hackernews.recall_pos()<cr> diff --git a/plugin/hackernews.vim b/plugin/hackernews.vim index d3a6dd7..7c8fab0 100644 --- a/plugin/hackernews.vim +++ b/plugin/hackernews.vim @@ -8,24 +8,10 @@ " Version: 0.1.1 -if !has('python') - echo "HackerNews.vim Error: Requires Vim compiled with +python" - finish -endif - - -" Filetype plugins and syntax highlighting should be enabled +" Filetype plugins need to be enabled filetype plugin on -syntax on - - -" Import Python code -execute "python import sys" -execute "python sys.path.append(r'" . expand("<sfile>:p:h") . "')" -execute "python from hackernews import hacker_news, hacker_news_link, recall_pos" - - -command! HackerNews python hacker_news() - +" Load ftplugin when opening .hackernews buffer au! BufRead,BufNewFile *.hackernews set filetype=hackernews + +command! HackerNews edit .hackernews |
