├── .gitignore ├── README.md ├── doc └── toggle-bool.txt └── plugin ├── tests ├── __init__.py └── toggle_bool_tests.py ├── toggle_bool.py └── toggle_bool.vim /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | doc/tags 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # toggle-bool 2 | 3 | Vim plugin to toggle boolean values. 4 | 5 | 6 | ## List of boolean values supported 7 | 8 | - true <-> false 9 | - yes <-> no 10 | - on <-> off 11 | - 0 <-> 1 12 | - enable(d) <-> disable(d) 13 | - first <-> last 14 | - before <-> after 15 | 16 | 17 | ## Installation 18 | 19 | Use your plugin manager of choice. 20 | 21 | - [Pathogen](https://github.com/tpope/vim-pathogen) 22 | - `git clone https://github.com/sagarrakshe/toggle-bool ~/.vim/bundle/toggle-bool` 23 | - [Vundle](https://github.com/gmarik/vundle) 24 | - Add `Bundle 'https://github.com/sagarrakshe/toggle-bool'` to .vimrc 25 | - Run `:BundleInstall` 26 | - [NeoBundle](https://github.com/Shougo/neobundle.vim) 27 | - Add `NeoBundle 'https://github.com/sagarrakshe/toggle-bool'` to .vimrc 28 | - Run `:NeoBundleInstall` 29 | - [vim-plug](https://github.com/junegunn/vim-plug) 30 | - Add `Plug 'https://github.com/sagarrakshe/toggle-bool'` to .vimrc 31 | - Run `:PlugInstall` 32 | 33 | 34 | ## Usage 35 | 36 | To invoke you need to call `:ToggleBool`. You can map accordingly. 37 | For example, to map to `r` you need to add following to your *.vimrc* 38 | 39 | noremap r :ToggleBool 40 | 41 | ## Todo 42 | 43 | 1. When word is replaced with the toggled value, it eats leading and trailing 44 | spaces. 45 | 2. Support more for more values and logical operators. 46 | -------------------------------------------------------------------------------- /doc/toggle-bool.txt: -------------------------------------------------------------------------------- 1 | *toggle-bool.txt* A short multi line description of your plugin 2 | 3 | =============================================================================== 4 | CONTENTS *toggle-bool* 5 | 6 | 1. Intro .............................................. |toggle-bool-intro| 7 | 2. Requirements ................................ |toggle-bool-requirements| 8 | 3. Usage .............................................. |toggle-bool-usage| 9 | 4. Licence .......................................... |toggle-bool-licence| 10 | =============================================================================== 11 | 1. Intro *toggle-bool-intro* 12 | 13 | Overview of plugin 14 | 15 | 2. Requirements *toggle-bool-requirements* 16 | 17 | What additional programs are required to run the plugin 18 | 19 | 3. Usage *toggle-bool-usage* 20 | 21 | How to use the plugin. What functions does it give the user? 22 | 23 | 4. Licence *toggle-bool-licence* 24 | 25 | This is open source make sure to include a licence 26 | -------------------------------------------------------------------------------- /plugin/tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sagarrakshe/toggle-bool/7bc8358d2de311805e6802f9df7ca876b76308a3/plugin/tests/__init__.py -------------------------------------------------------------------------------- /plugin/tests/toggle_bool_tests.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import toggle_bool as sut 3 | 4 | 5 | @unittest.skip("Don't forget to test!") 6 | class ToggleBoolTests(unittest.TestCase): 7 | 8 | def test_example_fail(self): 9 | result = sut.toggle_bool_example() 10 | self.assertEqual("Happy Hacking", result) 11 | -------------------------------------------------------------------------------- /plugin/toggle_bool.py: -------------------------------------------------------------------------------- 1 | values = { 2 | 'true': 'false', 3 | 'on': 'off', 4 | 'yes': 'no', 5 | '1': '0', 6 | 'enable': 'disable', 7 | 'enabled': 'disabled', 8 | 'before': 'after', 9 | 'first': 'last', 10 | } 11 | 12 | 13 | def format_value(word, toggleWord): 14 | if word.isupper(): 15 | toggleWord = toggleWord.upper() 16 | elif word.istitle(): 17 | toggleWord = toggleWord.title() 18 | return toggleWord 19 | 20 | 21 | def toggle_bool_value(word): 22 | toggleWord = word 23 | for key in values.keys(): 24 | if key == word.lower(): 25 | toggleWord = values[key] 26 | toggleWord = format_value(word, toggleWord) 27 | break 28 | elif values[key] == word.lower(): 29 | toggleWord = key 30 | toggleWord = format_value(word, toggleWord) 31 | break 32 | return toggleWord 33 | -------------------------------------------------------------------------------- /plugin/toggle_bool.vim: -------------------------------------------------------------------------------- 1 | " -------------------------------- 2 | " Add our plugin to the path 3 | " -------------------------------- 4 | if has("python") 5 | python import sys 6 | python import vim 7 | python sys.path.append(vim.eval('expand(":h")')) 8 | elseif has("python3") 9 | python3 import sys 10 | python3 import vim 11 | python3 sys.path.append(vim.eval('expand(":h")')) 12 | endif 13 | 14 | 15 | " -------------------------------- 16 | " Function(s) 17 | " -------------------------------- 18 | function! TemplateExample() 19 | if has("python") 20 | python << endOfPython 21 | 22 | from toggle_bool import toggle_bool_value 23 | 24 | # get word under cursor 25 | wordUnderCursor = vim.eval('expand("")') 26 | 27 | if len(wordUnderCursor): 28 | # get the toggle value of word under cursor 29 | toggleValue = toggle_bool_value(wordUnderCursor) 30 | 31 | # replace the current word with new toggled value 32 | vim.command('normal viwc%s' % toggleValue) 33 | vim.command('normal b') 34 | 35 | endOfPython 36 | elseif has("python3") 37 | python3 << endOfPython 38 | 39 | from toggle_bool import toggle_bool_value 40 | 41 | # get word under cursor 42 | wordUnderCursor = vim.eval('expand("")') 43 | 44 | if len(wordUnderCursor): 45 | # get the toggle value of word under cursor 46 | toggleValue = toggle_bool_value(wordUnderCursor) 47 | 48 | # replace the current word with new toggled value 49 | vim.command('normal viwc%s' % toggleValue) 50 | vim.command('normal b') 51 | 52 | endOfPython 53 | endif 54 | endfunction 55 | 56 | " -------------------------------- 57 | " Expose our commands to the user 58 | " -------------------------------- 59 | command! ToggleBool call TemplateExample() 60 | 61 | " key mapping 62 | " noremap r :ToggleBool 63 | --------------------------------------------------------------------------------