├── .gitignore ├── LICENSE ├── README.md ├── autoload └── convert.vim ├── doc └── convert.txt ├── plugin └── convert.vim └── vim-convert.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | /doc/tags 2 | *.swp 3 | *.swo 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright 2018 Christopher Peterson [https://cspeterson.net] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | convert.vim 2 | ============ 3 | 4 | This Vim plugin is a tool for converting units to other units. Time, distance, 5 | data, power, etc. 6 | 7 | 8 | 9 | # Usage 10 | 11 | ```viml 12 | :Convert [[value]|[start unit]] {target unit} 13 | ``` 14 | 15 | To convert in-place the currently visually selected text which includes a unit, e.g. `500g` 16 | 17 | ```viml 18 | :Convert {target unit} 19 | ``` 20 | 21 | To convert in-place the currently visually selected text which does *not* include a unit, e.g. `500`, you'll need to specify one: 22 | 23 | ```viml 24 | :Convert {start unit} {target unit} 25 | ``` 26 | 27 | To insert a unit conversion as a new value after the cursor, give both fields: 28 | 29 | ```viml 30 | :Convert 31 | ``` 32 | 33 | This plugin will try to be mindful of whether the unit itself is desired when replacing text. That is, if the selected text lacks a unit, the result will be placed without one as well. 34 | 35 | Examples of input and results and how they get there: 36 | 37 | --------------------------------------------------------------------------- 38 | | Selected text | Command | What it's doing | Result | 39 | |-----------------|------------------|-----------------------|------------| 40 | | `192in` | `:Convert ft` | `units 192in ft` | `4.166ft` | 41 | | `16` | `:Convert ft` in | `units 16ft in` | `192` | 42 | | *none* | `:Convert 16ft` in | `units 16ft in` | `192in` | 43 | | *any text* | `:Convert 16ft` in | `units 16ft in` | `192in` | 44 | | `12parsecs` | `:Convert hrs` | `units 12parsecs hrs` | *error*\* | 45 | --------------------------------------------------------------------------- 46 | 47 | \* parsecs are a unit of distance 48 | 49 | # Installation 50 | 51 | Several ways to install: 52 | 53 | ### Pathogen Vim plugin manager 54 | 55 | [Pathogen](https://github.com/tpope/vim-pathogen) 56 | 57 | ```sh 58 | cd ~/.vim/bundle 59 | git clone git://github.com/cspeterson/vim-convert.git 60 | ``` 61 | 62 | ### Vundle Vim plugin manager 63 | 64 | [Vundle](https://github.com/VundleVim/Vundle.vim) 65 | 66 | Add this to the Vundle plugin list in your `.vimrc` 67 | 68 | ```viml 69 | Plugin 'cspeterson/vim-convert' 70 | ``` 71 | 72 | And install with: 73 | 74 | ```sh 75 | vim +PluginInstall 76 | ``` 77 | 78 | Once help tags have been generated, you can view the manual with `:help convert`. 79 | 80 | # Customization 81 | 82 | ## Customizing the plugin 83 | 84 | To set the path to the `units` command if not in your PATH: 85 | 86 | ```viml 87 | set g:convert_command='/path/to/units' 88 | ``` 89 | 90 | ## Customing the Units utility 91 | 92 | This plugin uses Units, a GNU unit conversion and calculation program, and so supports any conversions that it supports. As a rule of thumb any unit or shortened unit you think of will probably work. 93 | 94 | To list supported units (as defined in Units' `definitions.units` file) in a split window: 95 | 96 | ```viml 97 | :Units 98 | ``` 99 | 100 | And while the full lowdown on customizing GNU Units may be beyond the scope of this text, here is an example of the sort of statements you could put into `$HOME/.units` in order to create new definitions or override existing ones: 101 | 102 | ``` 103 | # re-define `pt` from `pint` to `point` (the typographical unit) 104 | pt computerpoint 105 | # re-define `pc` from `parsec` to `pica` (the typographical unit) 106 | pc computerpica 107 | ``` 108 | 109 | # Contributing 110 | 111 | Taking pull requests at `https://github.com/cspeterson/vim-convert` 112 | 113 | Please code defensively (`normal!` etc) and try to maintain style, such as there is. 114 | 115 | # Credits 116 | 117 | By [Christopher Peterson](https://cspeterson.net) ([@cspeterson_](https://www.twitter.com/cspeterson_)) 118 | 119 | This plugin relies on the really actually very awesome [Units utility from GNU](https://www.gnu.org/software/units/) 120 | 121 | Images used in the logo art: 122 | 123 | 124 | [Vim logo from D0ktorz](https://commons.wikimedia.org/wiki/File:Vimlogo.svg) | [SI Units from Dono](https://commons.wikimedia.org/wiki/File:SI_base_unit.svg) 125 | 126 | License 127 | ------- 128 | 129 | Copyright (c) Christopher Peterson. Distributed under [The MIT License](https://opensource.org/licenses/MIT). 130 | -------------------------------------------------------------------------------- /autoload/convert.vim: -------------------------------------------------------------------------------- 1 | " vim-convert/autoload/convert.vim 2 | " Author: Christopher Peterson 3 | 4 | """ Functions {{{1 5 | function! convert#Display_units_defs() 6 | """ Display the unit definitions file in a split 7 | let output = system(g:convert_command . ' -V') 8 | let defs_file = matchstr(output, '\(Units data file is ''\)\@<=[^'']\+\(''\)\@=') 9 | 10 | execute 'split|view ' . fnameescape(defs_file) 11 | endfunction 12 | 13 | function! convert#Convert_units(...) range 14 | """ Convert units and insert the result, considering formatting and whether 15 | """ to overwrite a selection or insert anew 16 | 17 | " Get current visual selection in reg n, but back it up the orig value first 18 | let nbak = @n 19 | 20 | try 21 | if a:0 >=# 3 22 | echoerr 'Too many arugments for conversion' 23 | return 24 | elseif a:0 ==# 2 25 | " Look at the first arg. Is it a value *and* unit, or just a unit? 26 | if a:1 =~# '[0-9]\+[a-zAaZ]\+[a-zA-Z0-9]*' 27 | " If given a value-unit combo a la "15kg" 28 | let @n = a:1 29 | let new_unit = a:2 30 | let nakedval = 0 31 | let replace_selection = 0 32 | else 33 | " Else we got just a unit specified, which means we're operating on the 34 | " last selection 35 | " Get the last selection into reg n 36 | silent! normal! gv"ny 37 | 38 | let @n = @n . a:1 39 | let new_unit = a:2 40 | let nakedval = 1 41 | let replace_selection = 1 42 | endif 43 | else 44 | " Only one arg: assume it is the target unit 45 | " Get the last selection into reg n 46 | silent! normal! gv"ny 47 | 48 | let new_unit = a:1 49 | let nakedval = 0 50 | let replace_selection = 1 51 | endif 52 | 53 | " Actually convert things between units 54 | let unitsargs = "'" . @n . "' '" . new_unit . "'" 55 | let @n = system(g:convert_command . ' ' . unitsargs) 56 | if v:shell_error !=# 0 57 | """ Bail 58 | echon @n 59 | let @n = nbak 60 | return 61 | endif 62 | let @n = split(@n, '\n')[0] " Get first line 63 | " Pare down to just the numerical result 64 | let @n = substitute(@n, '^\s\+\*\s\+', '', '') 65 | " Limit significant figures if configured to do so 66 | if !nakedval 67 | " If the source unit was given as an arg and not part of the selection, 68 | " put the answer back in place in the same manner: no units 69 | let @n = join([@n, new_unit], '') 70 | endif 71 | 72 | if replace_selection 73 | " If the function was apparently called unrelated to any prev text selection 74 | " Delete original selection 75 | normal! gvd 76 | normal! "nP 77 | " restore the visual selection to the whole new value 78 | let selectrange = repeat('l', len(@n) - 1) 79 | execute 'normal! ` <@cspeterson_> 4 | License: MIT 5 | 6 | ============================================================================== 7 | CONTENTS *convert-contents* 8 | 9 | * Usage 10 | * Mappings 11 | * License 12 | * Bugs 13 | * Contributing 14 | * Changelog 15 | * Credits 16 | 17 | ============================================================================== 18 | INTRODUCTION *convert* 19 | 20 | This Vim plugin is a tool for converting units to other units. Time, distance, 21 | data, power, etc. 22 | 23 | ============================================================================== 24 | USAGE *convert-usage* 25 | 26 | > 27 | :Convert [[value]|[start unit]] {target unit} 28 | < 29 | 30 | To convert in-place the currently visually-selected text which includes a 31 | unit, e.g. `500g` 32 | > 33 | :Convert {target unit} 34 | < 35 | 36 | To convert in-place the currently visually-selected text which does *not* 37 | include a unit, e.g. `500`: 38 | > 39 | :Convert {start unit} {target unit} 40 | < 41 | 42 | To insert a unit conversion as a new value after the cursor, give both fields: 43 | > 44 | :Convert 45 | < 46 | 47 | This plugin will try to be mindful of whether the unit itself is desired when 48 | replacing text. That is, if the selected text lacks a unit, the result will be 49 | placed without one as well. 50 | 51 | Examples of input and results and how they get there: 52 | --------------------------------------------------------------------------- 53 | | Selected text | Command | What it's doing | Result | 54 | |-----------------|------------------|-----------------------|------------| 55 | | 192in | :Convert ft | `units 192in ft` | 4.166ft | 56 | | 16 | :Convert ft in | `units 16ft in` | 192 | 57 | | | :Convert 16ft in | `units 16ft in` | 192in | 58 | | | :Convert 16ft in | `units 16ft in` | 192in | 59 | | 12parsecs | :Convert hrs | `units 12parsecs hrs` | * | 60 | --------------------------------------------------------------------------- 61 | *parsecs are a unit of distance 62 | 63 | ============================================================================== 64 | CUSTOMIZING THE PLUGIN *convert-customizing* 65 | 66 | To set the path to the `units` command if not in your PATH: 67 | > 68 | set g:convert_command='/path/to/units' 69 | < 70 | 71 | ============================================================================== 72 | CUSTOMIZING THE UNITS UTILITY *convert-customizing-units* 73 | 74 | This plugin uses Units, a GNU unit conversion and calculation program, and so 75 | supports any conversions that it supports. As a rule of thumb any unit or 76 | shortened unit you think of will probably work. 77 | 78 | To list supported units (as defined in Units' `definitions.units` file) in a 79 | split window: 80 | > 81 | :Units 82 | < 83 | 84 | And while the full lowdown on customizing GNU Units may be beyond the scope of 85 | this text, here is an example of the sort of statements you could put into 86 | `$HOME/.units` in order to create new definitions or override existing ones: 87 | > 88 | # re-define `pt` from `pint` to `point` (the typographical unit) 89 | pt computerpoint 90 | # re-define `pc` from `parsec` to `pica` (the typographical unit) 91 | pc computerpica 92 | < 93 | 94 | 95 | ============================================================================== 96 | MAPPINGS *convert-mappings* 97 | 98 | None atm. 99 | 100 | ============================================================================== 101 | BUGS *convert-bugs* 102 | 103 | Let me know! :) 104 | 105 | ============================================================================== 106 | CONTRIBUTING *convert-contributing* 107 | 108 | Taking pull requests at `https://github.com/cspeterson/vim-convert` 109 | 110 | Please code defensively (`normal!` etc) and try to maintain style, such as 111 | there is. 112 | 113 | ============================================================================== 114 | CHANGELOG *convert-changelog* 115 | 116 | Init commit 117 | 118 | ============================================================================== 119 | CREDITS *convert-credits* 120 | 121 | By Christopher Peterson <@cspeterson_> 122 | 123 | This plugin relies on the really actually very awesome Units utility from GNU 124 | https://www.gnu.org/software/units/ 125 | 126 | -------------------------------------------------------------------------------- /plugin/convert.vim: -------------------------------------------------------------------------------- 1 | " convert.vim - Convert units to other units quickly and easily 2 | " Author: Christopher Peterson 3 | 4 | if exists('g:loaded_convert') 5 | finish 6 | endif 7 | let g:loaded_convert = 1 8 | 9 | """ Config {{{1 10 | if !exists('g:convert_command') 11 | let g:convert_command = 'units' 12 | endif 13 | """ }}} 14 | 15 | """ Commands and functions {{{1 16 | command! -range -nargs=+ Convert 17 | \ call convert#Convert_units() 18 | 19 | command! Units 20 | \ call convert#Display_units_defs() 21 | 22 | 23 | """ }}} 24 | -------------------------------------------------------------------------------- /vim-convert.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cspeterson/vim-convert/be618498182c222554f9471408fb32098b06193d/vim-convert.jpg --------------------------------------------------------------------------------