├── LICENSE ├── README.md ├── binder ├── apt.txt ├── postBuild └── requirements.txt ├── machine-learning-with-vim-brain.ipynb ├── pyproject.toml ├── screenshot.png ├── setup.py └── vim_kernel ├── __init__.py ├── __main__.py ├── autoload └── vimkernel.vim ├── install.py ├── kernel.json ├── kernel.py ├── kernel.vim ├── logo-32x32.png └── logo-64x64.png /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Yasuhiro Matsumoto 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 | # vim_kernel 2 | 3 | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/mattn/vim_kernel/master) 4 | 5 | 6 | A Jupyter kernel for Vim script 7 | 8 | ![vim_kernel](https://raw.githubusercontent.com/mattn/vim_kernel/master/screenshot.png) 9 | 10 | ## Try vim_kernel without install 11 | 12 | Click [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/mattn/vim_kernel/master) 13 | 14 | ## Installation 15 | 16 | This requires IPython 3. 17 | 18 | ``` 19 | python setup.py install 20 | python -m vim_kernel.install 21 | ``` 22 | 23 | To use it, run one of: 24 | 25 | ``` 26 | jupyter notebook 27 | jupyter qtconsole --kernel vim_kernel 28 | jupyter console --kernel vim_kernel 29 | ``` 30 | 31 | ## License 32 | 33 | MIT 34 | 35 | ## Author 36 | 37 | Yasuhiro Matsumoto (a.k.a. mattn) 38 | -------------------------------------------------------------------------------- /binder/apt.txt: -------------------------------------------------------------------------------- 1 | vim-nox 2 | -------------------------------------------------------------------------------- /binder/postBuild: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | python -m vim_kernel.install 4 | -------------------------------------------------------------------------------- /binder/requirements.txt: -------------------------------------------------------------------------------- 1 | vim_kernel 2 | -------------------------------------------------------------------------------- /machine-learning-with-vim-brain.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Machine Learning with vim-brain" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "A feedforward neural network is an artificial neural network where connections between the units do not form a cycle. This is different from recurrent neural networks.\n", 15 | "\n", 16 | "The feedforward neural network was the first and simplest type of artificial neural network devised. In this network, the information moves in only one direction, forward, from the input nodes, through the hidden nodes (if any) and to the output nodes. There are no cycles or loops in the network." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## Initialize Feed-Forward Neural Network" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "On my Windows PC, plugins are managed on directory `~/vimfiles/plugged`. Add runtimepath to vim-brain plugin" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | ":!rm -rf vim-brain\r\n", 43 | "\n", 44 | ":!git clone https://github.com/mattn/vim-brain\r\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "!rm -rf vim-brain\n", 50 | "!git clone https://github.com/mattn/vim-brain" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "set rtp+=./vim-brain" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "Set the random seed to 0" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "call brain#srand(0)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "Create the XOR representation patter to train the network" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 4, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "let patterns = [\n", 92 | "\\ [[0.0, 0.0], [0.0]],\n", 93 | "\\ [[0.0, 1.0], [1.0]],\n", 94 | "\\ [[1.0, 0.0], [1.0]],\n", 95 | "\\ [[1.0, 1.0], [0.0]],\n", 96 | "\\]" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "Instantiate the Feed Forward" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 5, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "let ff = brain#new_feed()" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Initialize the Neural Network. The networks structure will contain:\n", 120 | "* 2 inputs\n", 121 | "* 2 hidden nodes\n", 122 | "* 1 output" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 6, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "call ff.Init(2, 2, 1)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "Train the network using the XOR patterns. The training will run for 1000 epochs. The learning rate is set to 0.6 and the momentum factor to 0.4. Use true in the last parameter to receive reports about the learning error" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 7, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [ 147 | "call ff.Train(patterns, 1000, 0.6, 0.4, v:false)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "Then run the tests with input patterns." 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "[0.0, 0.0] -> [0.045977] : [0.0]\n", 167 | "[0.0, 1.0] -> [0.9461] : [1.0]\n", 168 | "[1.0, 0.0] -> [0.944088] : [1.0]\n", 169 | "[1.0, 1.0] -> [0.073895] : [0.0]" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "call ff.Test(patterns)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "Where the first values are the inputs, the values after the arrow -> are the output values from the network and the values after : are the expected outputs." 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [] 190 | } 191 | ], 192 | "metadata": { 193 | "kernelspec": { 194 | "display_name": "Vim", 195 | "language": "", 196 | "name": "vim_kernel" 197 | }, 198 | "language_info": { 199 | "file_extension": ".vim", 200 | "mimetype": "text/plain", 201 | "name": "vim" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 2 206 | } 207 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "vim_kernel" 3 | version = "0.0.3" 4 | description = "A Jupyter kernel for Vim script" 5 | authors = ["mattn "] 6 | 7 | [tool.poetry.dependencies] 8 | python = "^3.6" 9 | notebook = "^6.0.3" 10 | 11 | [tool.poetry.dev-dependencies] 12 | pytest = "^5.2" 13 | 14 | [build-system] 15 | requires = ["poetry>=0.12"] 16 | build-backend = "poetry.masonry.api" 17 | 18 | [tool.flit.metadata] 19 | module = "vim_kernel" 20 | author = "mattn" 21 | author-email = "mattn.jp@gmail.com" 22 | home-page = "https://github.com/mattn/vim_kernel" 23 | description-file = "README.md" 24 | classifiers = [ 25 | 'Framework :: IPython', 26 | 'License :: MIT', 27 | 'Programming Language :: Vim', 28 | ] 29 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattn/vim_kernel/1051cd9c6bcb2648b1cc6dc01ad12191cb420d9d/screenshot.png -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name="vim_kernel", 5 | version = "0.2", 6 | packages=find_packages(), 7 | description="SQLite3 Jupyter Kernel", 8 | url = "https://github.com/mattn/vim_kernel", 9 | classifiers = [ 10 | 'Framework :: IPython', 11 | 'License :: MIT', 12 | 'Programming Language :: Vim', 13 | ] 14 | ) 15 | -------------------------------------------------------------------------------- /vim_kernel/__init__.py: -------------------------------------------------------------------------------- 1 | """A bash kernel for Jupyter""" 2 | 3 | from .kernel import __version__ 4 | -------------------------------------------------------------------------------- /vim_kernel/__main__.py: -------------------------------------------------------------------------------- 1 | from ipykernel.kernelapp import IPKernelApp 2 | from .kernel import VimKernel 3 | IPKernelApp.launch_instance(kernel_class=VimKernel) 4 | -------------------------------------------------------------------------------- /vim_kernel/autoload/vimkernel.vim: -------------------------------------------------------------------------------- 1 | function! s:encode(str) abort 2 | let str = a:str 3 | let str = substitute(str, '&', '\&', 'g') 4 | let str = substitute(str, '>', '\>', 'g') 5 | let str = substitute(str, '<', '\<', 'g') 6 | let str = substitute(str, "\n", '\ ', 'g') 7 | let str = substitute(str, '"', '\"', 'g') 8 | let str = substitute(str, "'", '\'', 'g') 9 | let str = substitute(str, ' ', '\ ', 'g') 10 | return str 11 | endfunction 12 | 13 | function! vimkernel#display_table(data) 14 | let l:table = '' 15 | for l:row in a:data 16 | let l:table .= '' 17 | for l:col in l:row 18 | let l:str = type(l:col) == v:t_string ? l:col : string(l:col) 19 | let l:table .= '' 20 | endfor 21 | let l:table .= '' 22 | endfor 23 | let l:table .= '
' . s:encode(l:str) . '
' 24 | let g:vimkernel_display_data = { 25 | \ 'source': 'kernel', 26 | \ 'data': { 27 | \ 'text/html': l:table, 28 | \ 'text/plain': string(a:data), 29 | \ }, 30 | \ 'metadata': {}, 31 | \} 32 | endfunction 33 | -------------------------------------------------------------------------------- /vim_kernel/install.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | import sys 4 | import argparse 5 | from subprocess import check_output 6 | 7 | from jupyter_client.kernelspec import KernelSpecManager 8 | from IPython.utils.tempdir import TemporaryDirectory 9 | 10 | def install_vim_kernel_spec(user=True, prefix=None): 11 | curdir = os.path.dirname(os.path.abspath(__file__)) 12 | kernel_json = {"argv":[sys.executable,"-m","vim_kernel", "-f", "{connection_file}"], "display_name":"Vim"} 13 | with open(os.path.join(curdir, 'kernel.json'), 'w') as f: 14 | json.dump(kernel_json, f, sort_keys=True) 15 | print('Installing IPython kernel spec') 16 | KernelSpecManager().install_kernel_spec(curdir, 'vim_kernel', user=user, prefix=prefix) 17 | 18 | def _is_root(): 19 | try: 20 | return os.geteuid() == 0 21 | except AttributeError: 22 | return False # assume not an admin on non-Unix platforms 23 | 24 | def main(argv=None): 25 | parser = argparse.ArgumentParser(description='Install KernelSpec for Vim Kernel') 26 | prefix_locations = parser.add_mutually_exclusive_group() 27 | prefix_locations.add_argument('--user', help='Install KernelSpec in user home directory', action='store_true') 28 | prefix_locations.add_argument('--sys-prefix', help='Install KernelSpec in sys.prefix. Useful in conda / virtualenv', action='store_true', dest='sys_prefix') 29 | prefix_locations.add_argument('--prefix', help='Install KernelSpec in this prefix', default=None) 30 | args = parser.parse_args(argv) 31 | user = False 32 | prefix = None 33 | if args.sys_prefix: 34 | prefix = sys.prefix 35 | elif args.prefix: 36 | prefix = args.prefix 37 | elif args.user or not _is_root(): 38 | user = True 39 | install_vim_kernel_spec(user=user, prefix=prefix) 40 | 41 | if __name__ == '__main__': 42 | main() 43 | -------------------------------------------------------------------------------- /vim_kernel/kernel.json: -------------------------------------------------------------------------------- 1 | {"argv": ["/home/mattn/miniconda3/envs/jupyterlab/bin/python", "-m", "vim_kernel", "-f", "{connection_file}"], "display_name": "Vim"} -------------------------------------------------------------------------------- /vim_kernel/kernel.py: -------------------------------------------------------------------------------- 1 | from ipykernel.kernelbase import Kernel 2 | from subprocess import Popen, STDOUT, PIPE, check_output 3 | from os import path, remove, environ 4 | from time import sleep 5 | import json 6 | from jupyter_client.kernelspec import get_kernel_spec 7 | 8 | 9 | __version__ = '0.0.1' 10 | 11 | class VimKernel(Kernel): 12 | implementation = 'Vim' 13 | implementation_version = __version__ 14 | 15 | @property 16 | def language_version(self): 17 | return __version__ 18 | 19 | _banner = None 20 | 21 | @property 22 | def banner(self): 23 | if self._banner is None: 24 | self._banner = check_output(['vim', '--version']).decode('utf-8') 25 | return self._banner 26 | 27 | language_info = {'name': 'vim', 28 | 'mimetype': 'text/plain', 29 | 'file_extension': '.vim'} 30 | 31 | def __init__(self, **kwargs): 32 | Kernel.__init__(self, **kwargs) 33 | self.dir = get_kernel_spec('vim_kernel').resource_dir 34 | self.vim = Popen([ 35 | 'vim', 36 | '-X', 37 | '-N', 38 | '-u', 39 | 'NONE', 40 | '-i', 41 | 'NONE', 42 | '-e', 43 | '-s', 44 | '-S', 45 | path.join(self.dir, 'kernel.vim') 46 | ], stdout=PIPE, stderr=STDOUT, shell=False, env=environ.copy()) 47 | 48 | def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False): 49 | stdout = path.join(self.dir, '%d.output' % self.vim.pid) 50 | stdin = path.join(self.dir, '%d.input' % self.vim.pid) 51 | if path.exists(stdout): 52 | remove(stdout) 53 | with open(stdin, 'w', encoding='utf-8') as f: 54 | f.write("\n".join(code.splitlines())) 55 | 56 | while not path.exists(stdout): 57 | sleep(1) 58 | 59 | with open(stdout, 'r', encoding='utf-8') as f: 60 | output = json.loads(f.read()) 61 | 62 | remove(stdout) 63 | if not silent: 64 | if 'stdout' in output and len(output['stdout']) > 0: 65 | self.send_response(self.iopub_socket, 'stream', {'name': 'stdout', 'text': output['stdout']}) 66 | if 'stderr' in output and len(output['stderr']) > 0: 67 | self.send_response(self.iopub_socket, 'stream', {'name': 'stderr', 'text': output['stderr']}) 68 | if 'data' in output: 69 | self.send_response(self.iopub_socket, 'display_data', output['data']) 70 | return { 71 | 'status': 'ok', 72 | 'execution_count': self.execution_count, 73 | 'payload': [], 74 | 'user_expressions': {}, 75 | } 76 | 77 | def do_shutdown(self, restart): 78 | self.vim.kill() 79 | -------------------------------------------------------------------------------- /vim_kernel/kernel.vim: -------------------------------------------------------------------------------- 1 | let s:base = expand(':p:h') 2 | 3 | function! s:watch_stdin(...) abort 4 | let l:input = s:base . '/' . getpid() . '.input' 5 | while 1 6 | sleep 500m 7 | if !filereadable(l:input) 8 | continue 9 | endif 10 | try 11 | let g:vimkernel_display_data = v:none 12 | let l:stderr = '' 13 | redir => l:stdout 14 | exe 'source' l:input 15 | catch 16 | let l:stderr = v:exception 17 | finally 18 | redir END 19 | if &filetype == 'help' 20 | let l:l1 = line('.') 21 | silent normal! j 22 | silent let l:l2 = search('\*[^*]\+\*$', 'W') 23 | if l:l2 == 0 24 | let l:l2 = getline('$') 25 | else 26 | let l:l2 -= 1 27 | endif 28 | let l:lines = getline(l:l1, l:l2) 29 | let l:stdout = join(l:lines, "\n") 30 | close 31 | endif 32 | if !empty(l:stdout) && l:stdout[0] == "\n" 33 | let l:stdout = l:stdout[1:] 34 | endif 35 | call delete(l:input) 36 | let l:temp = s:base . '/' . getpid() . '.temp' 37 | let l:res = { 38 | \ 'stdout': l:stdout, 39 | \ 'stderr': l:stderr 40 | \} 41 | if !empty(g:vimkernel_display_data) 42 | let l:res['data'] = g:vimkernel_display_data 43 | endif 44 | call writefile([json_encode(l:res)], l:temp) 45 | let l:output = s:base . '/' . getpid() . '.output' 46 | call rename(l:temp, l:output) 47 | endtry 48 | endwhile 49 | endfunction 50 | 51 | set encoding=utf-8 52 | exe 'set rtp+=' . s:base 53 | "call timer_start(500, function('s:watch_stdin'), {'repeat': -1}) 54 | call s:watch_stdin() 55 | -------------------------------------------------------------------------------- /vim_kernel/logo-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattn/vim_kernel/1051cd9c6bcb2648b1cc6dc01ad12191cb420d9d/vim_kernel/logo-32x32.png -------------------------------------------------------------------------------- /vim_kernel/logo-64x64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattn/vim_kernel/1051cd9c6bcb2648b1cc6dc01ad12191cb420d9d/vim_kernel/logo-64x64.png --------------------------------------------------------------------------------