├── .gitignore ├── LICENSE ├── README.md ├── ftplugin └── reddit.vim ├── plugin ├── reddit.vim └── vimreddit.py ├── syntax └── reddit.vim └── vim-reddit-home.png /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | doc/tags 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-reddit 2 | 3 | Browse [reddit](http://www.reddit.com) inside Vim! 4 | 5 | Heavily inspired (and forked) from [vim-hackernews](https://github.com/ryanss/vim-hackernews). 6 | ![subreddit home](https://raw.githubusercontent.com/mnpk/vim-reddit/master/vim-reddit-home.png) 7 | 8 | ## usage 9 | 10 | * Open the front page of a subreddit with the `:Reddit [subreddit]` 11 | * Press lowercase `o` to open links in Vim 12 | * Press uppercase `O` to open links in default web browser 13 | * Press lowercase `u` to go back (or whatever you've remapped `undo` to) 14 | * Press `Ctrl+r` to go forward (or whatever you're remapped `redo` to) 15 | * Execute the `:bd` command to close and remove the reddit buffer 16 | 17 | ## installation 18 | 19 | ##### Pathogen 20 | ```bash 21 | git clone https://github.com/joshhartigan/vim-reddit ~/.vim/bundle/vim-reddit 22 | ``` 23 | 24 | ##### Vundle, vim-plug and friends 25 | ``` 26 | Plugin 'joshhartigan/vim-reddit' 27 | ``` 28 | -------------------------------------------------------------------------------- /ftplugin/reddit.vim: -------------------------------------------------------------------------------- 1 | noremap o :python vim_reddit_link() 2 | noremap O :python vim_reddit_link(in_browser=True) 3 | -------------------------------------------------------------------------------- /plugin/reddit.vim: -------------------------------------------------------------------------------- 1 | if !has('python') 2 | echo 'vim-reddit requires Vim compiled with +python!' 3 | finish 4 | endif 5 | 6 | execute 'python import sys' 7 | execute "python sys.path.append(r'" . expand(":p:h") . "')" 8 | execute "python from vimreddit import vim_reddit, vim_reddit_link" 9 | 10 | command! -nargs=1 Reddit python vim_reddit() 11 | 12 | au! BufRead,BufNewFile *.reddit set filetype=reddit 13 | -------------------------------------------------------------------------------- /plugin/vimreddit.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import textwrap 4 | import json 5 | import vim 6 | import webbrowser 7 | import urllib2 8 | import re 9 | 10 | def redditurl(subreddit): 11 | return 'http://www.reddit.com/r/' + subreddit + '/hot.json' 12 | 13 | MARKDOWN_URL = 'http://fuckyeahmarkdown.com/go/?read=1&u=' 14 | 15 | def bufwrite(string): 16 | b = vim.current.buffer 17 | 18 | # Never write more than two blank lines in a row 19 | if not string.strip() and not b[-1].strip() and not b[-2].strip(): 20 | return 21 | 22 | # Vim must be given UTF-8 rather than unicode 23 | if isinstance(string, unicode): 24 | string = string.encode('utf-8', errors='replace') 25 | 26 | # Code block markers for syntax highlighting 27 | if string and string[-1] == unichr(160).encode('utf-8'): 28 | b[-1] = string 29 | return 30 | 31 | if not b[0]: 32 | b[0] = string 33 | return 34 | 35 | if not b[0]: 36 | b[0] = string 37 | else: 38 | b.append(string) 39 | 40 | def read_url(url): 41 | opener = urllib2.build_opener() 42 | opener.addheaders = [('User-Agent', 'Python/vim-reddit')] 43 | return opener.open(url.encode("UTF-8")).read() 44 | 45 | urls = [None] * 1000 # urls[index]: url of link at index 46 | 47 | def vim_reddit(sub): 48 | vim.command('edit .reddit') 49 | vim.command('setlocal noswapfile') 50 | vim.command('setlocal buftype=nofile') 51 | 52 | bufwrite(' ┌─o') 53 | bufwrite(' ((• •)) r e d d i t') 54 | bufwrite(' http://www.reddit.com/r/' + sub) 55 | bufwrite('') 56 | 57 | items = json.loads(read_url(redditurl(sub))) 58 | for i, item in enumerate(items['data']['children']): 59 | item = item['data'] 60 | try: 61 | # surround shorter numbers (e.g. 9) with padding 62 | # to align with longer numbers 63 | index = (2 - len(str(i + 1))) * ' ' + str(i + 1) + '. ' 64 | 65 | line_1 = index + item['title'] + \ 66 | ' (' + item['domain'] + ')' 67 | line_2 = ' ' + str(item['score']) + ' points, by ' + \ 68 | item['author'] + ' | ' + str(item['num_comments']) + \ 69 | ' comments' 70 | bufwrite(line_1) 71 | bufwrite(line_2) 72 | bufwrite('') 73 | 74 | urls[i + 1] = item['url'] 75 | except KeyError: 76 | pass 77 | 78 | def vim_reddit_link(in_browser = False): 79 | line = vim.current.line 80 | print urls[int(line.split()[0].replace('.', ''))] 81 | 82 | regexp = re.compile(r'\d+\.') 83 | if regexp.search(line) is not None: 84 | id = line.split()[0].replace('.', '') 85 | if in_browser: 86 | browser = webbrowser.get() 87 | browser.open(urls[int(id)]) 88 | return 89 | vim.command('edit .reddit') 90 | content = read_url(MARKDOWN_URL + urls[int(id)]) 91 | for i, line in enumerate(content.split('\n')): 92 | if not line: 93 | bufwrite('') 94 | continue 95 | line = textwrap.wrap(line, width=80) 96 | for wrap in line: 97 | bufwrite(wrap) 98 | return 99 | print 'vim-reddit error: could not parse item' 100 | 101 | -------------------------------------------------------------------------------- /syntax/reddit.vim: -------------------------------------------------------------------------------- 1 | if exists('b:current_syntax') 2 | finish 3 | endif 4 | 5 | syn match AlienEye /•/ 6 | highlight AlienEye ctermfg=202 guifg=#ff4500 7 | syn match Header /http:\/\/www\.reddit\.com\/r\/.*$/ 8 | highlight Header ctermfg=153 guifg=#cee3f8 9 | 10 | " highlight link info as a comment 11 | syn match Comment /\d\+ points,.*$/ 12 | 13 | let b:current_syntax = 'reddit' 14 | -------------------------------------------------------------------------------- /vim-reddit-home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joshhartigan/vim-reddit/2cbfa51dff5d4acc22af73d1dde958382200f74f/vim-reddit-home.png --------------------------------------------------------------------------------