From 739fc710fb4738ec54605322f4d50ccb01536a67 Mon Sep 17 00:00:00 2001 From: ryanss Date: Tue, 3 Mar 2015 12:07:54 -0500 Subject: Add commment thread folding --- README.md | 1 + doc/hackernews.txt | 1 + ftplugin/hackernews.vim | 31 ++++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9055e19..900c317 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Basic Usage * Press uppercase `O` to open links in default web browser * Numbered lines with story titles on the front page link to the story url * Comment lines on the front page link to the comments url +* Press uppercase `F` to fold current comment thread * Press lowercase `u` to go back * Press `Ctrl+r` to go forward * Execute the `:bd` command to close and remove the Hacker News buffer diff --git a/doc/hackernews.txt b/doc/hackernews.txt index 2031efd..72e4f85 100644 --- a/doc/hackernews.txt +++ b/doc/hackernews.txt @@ -13,6 +13,7 @@ BASIC USAGE *hackernews-usage* * Press uppercase `O` to open links in default web browser * Numbered lines with story titles on the front page link to the story url * Comment lines on the front page link to the comments url +* Press uppercase `F` to fold current comment thread * Press lowercase `u` to go back * Press `Ctrl+r` to go forward * Execute the `:bd` command to close and remove the Hacker News buffer diff --git a/ftplugin/hackernews.vim b/ftplugin/hackernews.vim index c64b5d2..5f42f7c 100644 --- a/ftplugin/hackernews.vim +++ b/ftplugin/hackernews.vim @@ -62,11 +62,15 @@ function! s:Move(backwards) " Move to next/previous title line let pattern = '^\s*\d\+\.\s.' endif - execute 'silent normal! ' . dir . pattern . dir . 'e\r ' + execute 'silent normal! ' . dir . pattern . dir . '\r ' elseif match(getline(2), '^\d\+\s.\+ago') == 0 " Comment Page let pattern = '^\s*Comment by' execute 'silent normal! ' . dir . pattern . dir . '\r zt' + " Do not stop on folded lines + if foldclosed(line('.')) != -1 + execute 'silent normal! ' . dir . pattern . dir . '\r zt' + endif else " Article if a:backwards @@ -79,3 +83,28 @@ endfunction noremap J :call Move(0) noremap K :call Move(1) + + +" Fold comment threads +function! s:FoldComments() + if match(getline(2), '^\d\+\s.\+ago') != 0 + " Do not continue if this is not a comments page + return + endif + set nowrapscan + try + execute 'silent normal! ' . 'jj?^\s*Comment by.*:?\r j' + catch + " Nothing to fold + return + endtry + let level = matchstr(getline('.'), '^\s\+') + try + execute 'silent normal! ' . 'zf/\n^\s\{0,' . len(level). '}Comment/\r ' + catch + execute 'silent! normal! ' . 'zf/\n\%$/e\r ' + endtry + set wrapscan +endfunction + +noremap F :call FoldComments() -- cgit v1.2.3