├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── plugin └── vimdiscord.vim └── python ├── plugin.py └── rpc.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Handle default line endings 2 | * text=auto eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.py[cod] 3 | *$py.class 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Valentin B. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vimdiscord 2 | 3 | This plugin adds Discord RPC to Vim so you can show the world on what cool projects you're working on! 4 | 5 | ## Installation 6 | 7 | Make sure to have a Vim installation with Python 3 support. 8 | 9 | ### [vim-plug](https://github.com/junegunn/vim-plug) 10 | 11 | Add `Plug 'vbe0201/vimdiscord'` to your `.vimrc` file and run `:PlugInstall`. 12 | 13 | ### [Vundle](https://github.com/VundleVim/Vundle.vim) 14 | 15 | Add `Plugin 'vbe0201/vimdiscord'` to your `.vimrc` file and run `:BundleInstall`. 16 | 17 | ### [NeoBundle](https://github.com/Shougo/neobundle.vim) 18 | 19 | Add `NeoBundle 'vbe0201/vimdiscord'` to your `.vimrc` file and run `:NeoUpdate`. 20 | 21 | ### [pathogen.vim](https://github.com/tpope/vim-pathogen) 22 | 23 | ```sh 24 | git clone https://github.com/vbe0201/vimdiscord.git ~/.vim/bundle/vimdiscord 25 | ``` 26 | -------------------------------------------------------------------------------- /plugin/vimdiscord.vim: -------------------------------------------------------------------------------- 1 | " Discord RPC implementation for Vim 2 | 3 | if !has('python3') 4 | echo 'Compile Vim with +python3 in order to run vimdiscord' 5 | finish 6 | endif 7 | 8 | if exists('g:vimdiscord') 9 | finish 10 | endif 11 | let g:vimdiscord = 1 12 | 13 | let s:plugin_root = fnamemodify(resolve(expand(':p')), ':h') 14 | 15 | function! vimdiscord#version() 16 | return '1.0.0' 17 | endfunction 18 | 19 | function! vimdiscord#run() 20 | python3 << EOF 21 | import os 22 | import sys 23 | import threading 24 | import vim 25 | 26 | plugin_root = vim.eval('s:plugin_root') 27 | python_root = os.path.normpath(os.path.join(plugin_root, '..', 'python')) 28 | sys.path.insert(0, python_root) 29 | 30 | import plugin 31 | 32 | connection = plugin.rpc.connect() 33 | if connection is not None: 34 | plugin.rpc.perform_handshake(connection, plugin.CLIENT_ID) 35 | plugin.logger.info('Connection successfully established.') 36 | 37 | activity = plugin.BASE_ACTIVITY 38 | plugin.rpc.set_activity(connection, activity) 39 | EOF 40 | endfunction 41 | 42 | function! vimdiscord#update() 43 | python3 << EOF 44 | if connection is not None: 45 | import plugin 46 | plugin.update_presence(connection) 47 | EOF 48 | endfunction 49 | 50 | function! vimdiscord#stop() 51 | python3 << EOF 52 | plugin.rpc.connection_closed = True 53 | EOF 54 | endfunction 55 | 56 | call vimdiscord#run() 57 | 58 | command! VimdiscordVersion call vimdiscord#version() 59 | command! -nargs=0 UpdatePresence call vimdiscord#update() 60 | 61 | augroup VimdiscordAutoStart 62 | autocmd! 63 | autocmd BufNewFile,BufRead,BufEnter * :call vimdiscord#update() 64 | augroup END 65 | -------------------------------------------------------------------------------- /python/plugin.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import time 3 | import vim 4 | 5 | import rpc 6 | 7 | logger = logging.getLogger(__name__) 8 | logger.setLevel(20) 9 | 10 | #START_TIME = int(time.time()) 11 | 12 | BASE_ACTIVITY = { 13 | 'details': 'Idle', 14 | #'timestamps': { 15 | # 'start': START_TIME 16 | # }, 17 | 'assets': { 18 | 'large_text': 'Vim', 19 | 'large_image': 'vim_logo', 20 | 'small_text': 'The one true editor', 21 | 'small_image': 'vim_logo' 22 | } 23 | } 24 | 25 | CLIENT_ID = '589111267986243624' 26 | 27 | thumbnails = { 28 | 'html': 'HTML', 29 | 'css': 'CSS', 30 | 'js': 'JavaScript', 31 | 'php': 'PHP', 32 | 'scss': 'Sass', 33 | 'py': 'Python', 34 | 'rs': 'Rust', 35 | 'c': 'C', 36 | 'h': 'C Header', 37 | 'cpp': 'C++', 38 | 'hpp': 'C++ Header', 39 | 'cxx': 'C++', 40 | 'cc': 'C++', 41 | 'cs': 'C#', 42 | 'capnp': "Cap'n' Proto", 43 | 'java': 'Java', 44 | 'ex': 'Elixir', 45 | 'md': 'Markdown', 46 | 'ts': 'TypeScript', 47 | 'go': 'Go', 48 | 'kt': 'Kotlin', 49 | 'kts': 'Kotlin', 50 | 'cr': 'Crystal', 51 | 'rb': 'Ruby', 52 | 'clj': 'Clojure', 53 | 'hs': 'Haskell', 54 | 'json': 'JSON', 55 | 'vue': 'Vue', 56 | 'groovy': 'Groovy', 57 | 'swift': 'Swift', 58 | 'lua': 'Lua', 59 | 'jl': 'Julia', 60 | 'dart': 'Dart', 61 | 'pp': 'Pascal', 62 | } 63 | 64 | 65 | def get_filename(): 66 | return vim.eval('expand("%:t")') 67 | 68 | 69 | def get_extension(): 70 | return vim.eval('expand("%:e")') 71 | 72 | 73 | def get_cwd(): 74 | return vim.eval('getcwd()') 75 | 76 | 77 | def update_presence(connection): 78 | if rpc.connection_closed: 79 | rpc.close(connection) 80 | logger.error('Connection to Discord closed.') 81 | return 82 | 83 | activity = BASE_ACTIVITY 84 | filename = get_filename() 85 | cwd = get_cwd() 86 | if not filename or not cwd: 87 | return 88 | 89 | activity['details'] = 'Editing a .' + get_extension() +' file' 90 | activity['assets']['small_text'] = 'Working on project ' + cwd 91 | 92 | extension = get_extension() 93 | if extension and extension in thumbnails.keys(): 94 | activity['assets']['large_image'] = extension 95 | activity['assets']['large_text'] = 'Editing a {} file'.format(thumbnails[extension]) 96 | activity['details'] = 'Editing a {} file'.format(thumbnails[extension]) 97 | else: 98 | activity['details'] = 'Editing a .{} file'.format(extension) 99 | activity['assets']['large_image'] = 'unknown' 100 | 101 | try: 102 | rpc.set_activity(connection, activity) 103 | except NameError: 104 | logger.error('Discord is not running!') 105 | except BrokenPipeError: 106 | logger.error('Connection to Discord lost!') 107 | -------------------------------------------------------------------------------- /python/rpc.py: -------------------------------------------------------------------------------- 1 | import enum 2 | import json 3 | import logging 4 | import os 5 | import socket 6 | import sys 7 | import struct 8 | import uuid 9 | 10 | import plugin 11 | logger = logging.getLogger(__name__) 12 | logger.setLevel(20) 13 | 14 | connection_closed = True 15 | 16 | ON_WINDOWS = sys.platform == 'win32' 17 | PIPE_PATH = R'\\?\pipe\discord-ipc-{}' if ON_WINDOWS else R'discord-ipc-{}' 18 | 19 | class Opcodes(enum.Enum): 20 | HANDSHAKE = 0 21 | FRAME = 1 22 | CLOSE = 2 23 | PING = 3 24 | PONG = 4 25 | 26 | 27 | def _write(connection, data): 28 | if connection is not None: 29 | if ON_WINDOWS: 30 | connection.write(data) 31 | connection.flush() 32 | else: 33 | connection.sendall(data) 34 | 35 | 36 | def _receive(connection, size): 37 | if connection is not None: 38 | if ON_WINDOWS: 39 | return connection.read(size) 40 | else: 41 | return connection.recv(size) 42 | 43 | 44 | def _receive_exactly(connection, size): 45 | if connection is not None: 46 | buffer = b'' 47 | while size: 48 | chunk = _receive(connection, size) 49 | buffer += chunk 50 | size -= len(chunk) 51 | 52 | return buffer 53 | 54 | 55 | def _receive_header(connection): 56 | if connection is not None: 57 | return struct.unpack('