├── LICENSE ├── README.md ├── autoload └── CL.vim ├── doc └── compare-lines.txt └── plugin └── compare-lines.vim /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Adrien Fabre 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-Compare-lines 2 | 3 | Quickly compare two lines in a vim buffer. 4 | 5 | Compare-lines is a plugin which allows you to select two lines and put the 6 | differencies between these lines in the search register. 7 | 8 | The differences are then highlighted as searches and you can navigate through 9 | them with the `n` and `N` commands. 10 | 11 | # See it in action 12 | 13 | ![Example of usage](http://i.stack.imgur.com/HaB5Y.gif) 14 | 15 | # How to use it 16 | 17 | Compare-lines enable the `:CL` command which start the diff between two lines. 18 | The command can be used with three types of invocation: 19 | 20 | :CL 21 | Will start the diff between current line (where the cursor is currently 22 | positionned) and the line under it. 23 | 24 | :CL 42 25 | Will start the diff between the current line and the line 42. 26 | 27 | :CL 42 66 28 | Will start the diff between the line 42 and the line 66. 29 | -------------------------------------------------------------------------------- /autoload/CL.vim: -------------------------------------------------------------------------------- 1 | " Get two different lines and put the differences in the search register 2 | function! CL#CompareLines(l1, l2) 3 | let l1 = a:l1 4 | let l2 = a:l2 5 | 6 | " Get the content of the lines 7 | let line1 = getline(l1) 8 | let line2 = getline(l2) 9 | 10 | let pattern = "" 11 | 12 | " Compare lines and create pattern of diff 13 | for i in range(strlen(line1)) 14 | if strpart(line1, i, 1) !=# strpart(line2, i, 1) 15 | if pattern != "" 16 | let pattern = pattern . "\\|" 17 | endif 18 | let pattern = pattern . "\\%" . l1 . "l" . "\\%" . ( i+1 ) . "c" 19 | let pattern = pattern . "\\|" . "\\%" . l2 . "l" . "\\%" . ( i+1 ) . "c" 20 | endif 21 | endfor 22 | 23 | " Search and highlight the diff 24 | if strlen(pattern) > 0 25 | execute "let @/='" . pattern . "'" 26 | set hlsearch 27 | normal! n 28 | endif 29 | endfunction 30 | 31 | " Creates foldings to focus on two lines 32 | function! CL#FocusLines(l1, l2) 33 | let l1 = a:l1 34 | let l2 = a:l2 35 | 36 | if (l1 > 1) 37 | execute "1, " . ( l1 - 1 ) . "fold" 38 | endif 39 | 40 | if ( l2-l1 > 2 ) 41 | execute (l1 + 1) . "," . (l2 - 1) . "fold" 42 | endif 43 | 44 | if (l2 < line('$')) 45 | execute (l2 + 1) . ",$fold" 46 | endif 47 | endfunction 48 | -------------------------------------------------------------------------------- /doc/compare-lines.txt: -------------------------------------------------------------------------------- 1 | *compare-lines.txt* Diff two lines in a buffer and navigate through the changes 2 | 3 | 4 | Compare-lines *compare-lines* 5 | 6 | 1. Introduction |compare-lines-intro| 7 | 2. Compare-lines Commands |compare-lines-commands| 8 | 2.1 Compare two lines |:CompareLines| 9 | 2.2 Focus on two lines |:FocusLines| 10 | 2.3 Focus and compare two lines |:FocusCompareLines| 11 | 3. About |compare-lines-about| 12 | 13 | ============================================================================== 14 | 1. Introduction *compare-lines-intro* 15 | 16 | Compare-lines is a plugin which allows you to select two lines and put the 17 | differencies between these lines in the search register. 18 | 19 | The differences are then highlighted as searches and you can navigate through 20 | them with the |n| and |N| commands. 21 | 22 | ============================================================================== 23 | 2. Compare-lines Commands *compare-lines-commands* 24 | 25 | 2.1 Compare two lines *:CL* *:CompareLines* 26 | 27 | Select two lines and highlight the differences between them. 28 | The differences are put in the search register, this way the use can navigate 29 | through the differences with the |n| and |N| normal mode commands. 30 | 31 | The command can be used with three types of invocation: 32 | 33 | > 34 | :CL 35 | < 36 | Will start the diff between current line (where the cursor is currently 37 | positionned) and the line under it. 38 | 39 | > 40 | :CL 42 41 | < 42 | Will start the diff between the current line and the line 42. 43 | 44 | > 45 | :CL 42 66 46 | < 47 | Will start the diff between the line 42 and the line 66. 48 | 49 | 2.2 Focus on two lines *:FL* *:FocusLines* 50 | 51 | Select two lines and focus on them. The focus is achieved by folding the lines 52 | which are not select. This way only the selected lines are shown in the 53 | buffer. 54 | 55 | This command can be invoqued in the same way as |:CompareLines|. I.e: 56 | 57 | > 58 | :FL 59 | < 60 | > 61 | :FL 42 62 | < 63 | > 64 | :FL 42 66 65 | < 66 | 2.3 Focus and compare two lines *:FCL* *:FocusCompareLines* 67 | 68 | Combines the commands |:FocusLines| and |:CompareLines|. The lines selected 69 | are the only one shown in the buffer and their differences are highlighted 70 | thanks to the search feature. 71 | 72 | This command can be invoqued in the same way as |:CompareLines|. I.e: 73 | 74 | > 75 | :FCL 76 | < 77 | > 78 | :FCL 42 79 | < 80 | > 81 | :FCL 42 66 82 | 83 | 84 | ============================================================================== 85 | 3. Compare-lines About *compare-lines-about* 86 | 87 | This plugin was inspired by this question on the site vi.stackexchange.com: 88 | http://vi.stackexchange.com/q/7348/1841 89 | 90 | The sources of the plugin are available on github: 91 | http://github.com/statox/vim-compare-lines 92 | 93 | The plugin is distributed under the MIT license. See the LICENSE file which 94 | comes with the plugin, or here: 95 | https://opensource.org/licenses/MIT 96 | 97 | 98 | vim:tw=78:ts=8:ft=help:norl: 99 | -------------------------------------------------------------------------------- /plugin/compare-lines.vim: -------------------------------------------------------------------------------- 1 | " Vim plugin to diff two lines of a buffer and navigate through the changes 2 | " File: compare-lines.vim 3 | " Author: statox 4 | " License: This file is distributed under the MIT License 5 | 6 | " Create the commands 7 | command! -nargs=* CL call PreTreatmentFunction("Compare", ) 8 | command! -nargs=* CompareLines call PreTreatmentFunction("Compare", ) 9 | command! -nargs=* FL call PreTreatmentFunction("Focus", ) 10 | command! -nargs=* FocusLines call PreTreatmentFunction("Focus", ) 11 | command! -nargs=* FCL call PreTreatmentFunction("CompareFocus", ) 12 | command! -nargs=* FocusCompareLines call PreTreatmentFunction("CompareFocus", ) 13 | 14 | command! XL call RestoreAfterCompare() 15 | 16 | " This function is called to 17 | " - get the line numbers 18 | " - check their existence in the buffer 19 | " - save the foldmethod 20 | " - create the mappings of the plugin 21 | function! s:PreTreatmentFunction(function, ...) 22 | " Depending on the number of arguments define which lines to treat 23 | if len(a:000) == 0 24 | let l1=line(".") 25 | let l2=line(".")+1 26 | elseif len(a:000) == 1 27 | let l1 =line(".") 28 | let l2 =str2nr(a:1) 29 | elseif len(a:000) == 2 30 | let l1 = str2nr(a:1) 31 | let l2 = str2nr(a:2) 32 | else 33 | echom "Bad number of arguments" 34 | return 35 | endif 36 | 37 | " Sort the lines 38 | if ( l1 > l2 ) 39 | let temp = l2 40 | let l2 = l1 41 | let l1 = temp 42 | endif 43 | 44 | " Check that the lines are in the buffer 45 | if (l1 < 1 || l1 > line("$") || l2 < 1 || l2 > line("$")) 46 | echom ("A selected line is not in the buffer") 47 | return 48 | endif 49 | 50 | " Save user configurations 51 | " Handle foldmethod configuration 52 | let s:foldmethod_save=&foldmethod 53 | let s:hlsearch_save=&hlsearch 54 | execute "mkview! " . &viewdir . "compare-lines" 55 | 56 | " Change foldmethod to do ours foldings 57 | set foldmethod=manual 58 | 59 | " Create a mapping to quit the compare mode 60 | if !empty(maparg('', 'n')) 61 | let s:mapping_save = maparg('', 'n', 0, 1) 62 | endif 63 | nnoremap :XL 64 | 65 | " Depending on the command used call the corresponding function 66 | if a:function == "Compare" 67 | call CL#CompareLines(l1, l2) 68 | elseif a:function == "Focus" 69 | call CL#FocusLines(l1, l2) 70 | elseif a:function == "CompareFocus" 71 | call CL#CompareLines(l1, l2) 72 | call CL#FocusLines(l1, l2) 73 | else 74 | echoe "Unkown function call" 75 | return 76 | endif 77 | endfunction 78 | 79 | function! s:RestoreAfterCompare() 80 | " Remove search highlight 81 | nohlsearch 82 | 83 | " Remove foldings created by the plugin 84 | normal! zE 85 | 86 | " Restore user configuration 87 | execute "loadview " . &viewdir ."compare-lines" 88 | let &foldmethod=s:foldmethod_save 89 | let &hlsearch=s:hlsearch_save 90 | 91 | " Restore the mapping to its previous value 92 | unmap 93 | if exists("s:mapping_save") 94 | execute (s:mapping_save.noremap ? 'nnoremap ' : 'nmap ') . 95 | \ (s:mapping_save.buffer ? ' ' : '') . 96 | \ (s:mapping_save.expr ? ' ' : '') . 97 | \ (s:mapping_save.nowait ? ' ' : '') . 98 | \ (s:mapping_save.silent ? ' ' : '') . 99 | \ s:mapping_save.lhs . " " 100 | \ s:mapping_save.rhs 101 | endif 102 | endfunction 103 | --------------------------------------------------------------------------------