| 1 | scriptencoding utf-8 |
| 2 | if &cp || exists('g:loaded_cmdline_increment') |
| 3 | finish |
| 4 | endif |
| 5 | let g:loaded_cmdline_increment = 1 |
| 6 | |
| 7 | " escape user configuration |
| 8 | let s:save_cpo = &cpo |
| 9 | set cpo&vim |
| 10 | |
| 11 | " mapping |
| 12 | if !hasmapto('<Plug>IncrementCommandLineNumber', 'c') |
| 13 | cmap <c-a> <Plug>IncrementCommandLineNumber |
| 14 | endif |
| 15 | cnoremap <Plug>IncrementCommandLineNumber <c-b>"<cr>:call g:IncrementCommandLineNumbering(1)<cr>:<c-r>=g:IncrementedCommandLine()<cr> |
| 16 | if !hasmapto('<Plug>DecrementCommandLineNumber', 'c') |
| 17 | cmap <c-x> <Plug>DecrementCommandLineNumber |
| 18 | endif |
| 19 | cnoremap <Plug>DecrementCommandLineNumber <c-b>"<cr>:call g:IncrementCommandLineNumbering(-1)<cr>:<c-r>=g:IncrementedCommandLine()<cr> |
| 20 | |
| 21 | " script sharing variables. |
| 22 | " updated command line will be stored in g:IncrementCommandLineNumbering(). |
| 23 | let s:updatedcommandline = '' |
| 24 | |
| 25 | " increment, or decrement last appearing number. |
| 26 | function! g:IncrementCommandLineNumbering(plus) |
| 27 | " when continuous increment is done, because @: is not updated, |
| 28 | " plugin can not increment correctly. |
| 29 | " so add one entry to command history, and use there flag. |
| 30 | " last command is start with '"' is first try, |
| 31 | " last command is not start with '"' is second try. |
| 32 | let l:lastcommand = histget(':', -1) |
| 33 | let l:firstcommandchar = strpart(l:lastcommand, 0, 1) |
| 34 | |
| 35 | " l:command is '"' starting text. |
| 36 | if l:firstcommandchar ==# '"' |
| 37 | let l:command = l:lastcommand |
| 38 | else |
| 39 | let l:command = '"' . l:lastcommand |
| 40 | endif |
| 41 | |
| 42 | " check input |
| 43 | let l:matchtest = match(l:command, '^".\{-\}-\?\d\+\D*$') |
| 44 | " command do not contain number |
| 45 | if l:matchtest < 0 |
| 46 | " remove first char '"' |
| 47 | let s:updatedcommandline = substitute(l:command, '^"\(.*\)$', '\1', '') |
| 48 | return |
| 49 | endif |
| 50 | |
| 51 | " update numbering |
| 52 | let l:numpattern = substitute(l:command, '^".\{-\}\(\d\+\)\D*$', '\1', '') |
| 53 | let l:updatednumberpattern = s:IncrementedText(l:numpattern, a:plus) |
| 54 | |
| 55 | " create new command line strings |
| 56 | let l:p1 = substitute(l:command, '^"\(.\{-\}\)\d\+\D*$', '\1', '') |
| 57 | let l:p2 = l:updatednumberpattern |
| 58 | let l:p3 = substitute(l:command, '^".\{-\}\d\+\(\D*\)$', '\1', '') |
| 59 | " set l register |
| 60 | let s:updatedcommandline = l:p1 . l:p2 . l:p3 |
| 61 | |
| 62 | " delete '"' command (dummy command) history |
| 63 | call histdel(':', -1) |
| 64 | " wrong command history is added. limitation. |
| 65 | call histadd(':', s:updatedcommandline) |
| 66 | endfunction |
| 67 | |
| 68 | " return incremented command line text. |
| 69 | function! g:IncrementedCommandLine() |
| 70 | return s:updatedcommandline |
| 71 | endfunction |
| 72 | |
| 73 | " return incremented pattern text. |
| 74 | " |
| 75 | " number + - |
| 76 | " 0 -> 1, 0 |
| 77 | " 0000 -> 0001, 0000 |
| 78 | " 127 -> 128, 126 |
| 79 | " 0127 -> 0128, 0126 |
| 80 | " 00127 -> 00128, 00126 |
| 81 | function! s:IncrementedText(pattern, plus) |
| 82 | " 0 |
| 83 | if match(a:pattern, '^0$') >= 0 |
| 84 | if a:plus > 0 |
| 85 | return a:pattern + a:plus |
| 86 | else |
| 87 | " not supported |
| 88 | return a:pattern |
| 89 | endif |
| 90 | endif |
| 91 | |
| 92 | " 123 |
| 93 | if match(a:pattern, '^[^0]\d*$') >= 0 |
| 94 | return a:pattern + a:plus |
| 95 | endif |
| 96 | |
| 97 | " 00000 |
| 98 | if match(a:pattern, '^0\+$') >= 0 |
| 99 | if a:plus > 0 |
| 100 | let l:numlength = strlen(a:pattern) |
| 101 | return printf('%0' .l:numlength. 'd', a:plus) |
| 102 | else |
| 103 | " not supported |
| 104 | return a:pattern |
| 105 | endif |
| 106 | endif |
| 107 | |
| 108 | " 00123 |
| 109 | if match(a:pattern, '^0\d*$') >= 0 |
| 110 | echo a:pattern + a:plus |
| 111 | let l:numlength = strlen(a:pattern) |
| 112 | let l:number = substitute(a:pattern, '^0\+\(\d\+\)$', '\1', '') |
| 113 | return printf('%0' .l:numlength. 'd', l:number + a:plus) |
| 114 | endif |
| 115 | |
| 116 | throw 'unknow numbering pattern is found.' |
| 117 | endfunction |
| 118 | |
| 119 | " recover user configuration |
| 120 | let &cpo = s:save_cpo |
| 121 | finish |
| 122 | |
| 123 | ============================================================================== |
| 124 | cmdline-increment.vim : コマンドライン行で数値インクリメント、デクリメント |
| 125 | ------------------------------------------------------------------------------ |
| 126 | $VIMRUNTIMEPATH/plugin/cmdline-increment.vim |
| 127 | ============================================================================== |
| 128 | author : 小見 拓 |
| 129 | url : http://nanasi.jp/ |
| 130 | email : mail@nanasi.jp |
| 131 | version : 2009/09/19 03:00:00 |
| 132 | ============================================================================== |
| 133 | コマンドラインモードで、限定的にですが、インクリメント、デクリメントを |
| 134 | 実行するプラグインです。 |
| 135 | |
| 136 | <C-a> インクリメント |
| 137 | <C-x> デクリメント |
| 138 | |
| 139 | このプラグインには、最後に現れた数値のみをインクリメント、デクリメントする、 |
| 140 | という制限があります。 |
| 141 | |
| 142 | |
| 143 | ------------------------------------------------------------------------------ |
| 144 | [使い方] |
| 145 | |
| 146 | 1. まず、コマンドラインモードになります。 |
| 147 | 2. 次のコマンドを入力してください。 |
| 148 | |
| 149 | :edit workfile_1.txt |
| 150 | |
| 151 | 3. そのまま、コマンドラインモードのままで、 |
| 152 | Ctrl-a、Ctrl-x を押下してみてください。 |
| 153 | |
| 154 | |
| 155 | ------------------------------------------------------------------------------ |
| 156 | [マッピングの変更] |
| 157 | Ctrl-a、Ctrl-xはあまりに利用されやすいマッピングなので、 |
| 158 | 設定でマッピングを変更できるようにしてあります。 |
| 159 | |
| 160 | デフォルトのマッピングを、他のマッピングに変更したい場合は、 |
| 161 | 次のような設定をVimエディタの設定ファイルに追加してください。 |
| 162 | |
| 163 | " Shift-↑ でインクリメント |
| 164 | cmap <S-Up> <Plug>IncrementCommandLineNumber |
| 165 | " Shift-↓ でデクリメント |
| 166 | cmap <S-Down> <Plug>DecrementCommandLineNumber |
| 167 | |
| 168 | |
| 169 | ------------------------------------------------------------------------------ |
| 170 | [制限事項] |
| 171 | - 最後に現れた数値のみがインクリメント、デクリメントできる。 |
| 172 | カーソル位置ではない。 |
| 173 | - コマンド実行履歴にゴミが残る。 |
| 174 | |
| 175 | |
| 176 | ------------------------------------------------------------------------------ |
| 177 | [history] |
| 178 | 2009/09/17 |
| 179 | - initial version. |
| 180 | |
| 181 | 2009/09/18 |
| 182 | - plugin is now not using 'l' register. |
| 183 | - below 0 number decrement is newly not supported. |
| 184 | minial value is 0. |
| 185 | - default mapping is switched to <c-a>, <c-x>. |
| 186 | - custom mapping is supported. |
| 187 | |
| 188 | |
| 189 | ============================================================================== |
| 190 | " vim: set ff=unix et ft=vim nowrap : |
| 191 | |