download

Sign in or create your account | Project List | Help

download Git Source Tree

Root/cmdline-increment.vim

1scriptencoding utf-8
2if &cp || exists('g:loaded_cmdline_increment')
3    finish
4endif
5let g:loaded_cmdline_increment = 1
6
7" escape user configuration
8let s:save_cpo = &cpo
9set cpo&vim
10
11" mapping
12if !hasmapto('<Plug>IncrementCommandLineNumber', 'c')
13    cmap <c-a> <Plug>IncrementCommandLineNumber
14endif
15cnoremap <Plug>IncrementCommandLineNumber <c-b>"<cr>:call g:IncrementCommandLineNumbering(1)<cr>:<c-r>=g:IncrementedCommandLine()<cr>
16if !hasmapto('<Plug>DecrementCommandLineNumber', 'c')
17    cmap <c-x> <Plug>DecrementCommandLineNumber
18endif
19cnoremap <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().
23let s:updatedcommandline = ''
24
25" increment, or decrement last appearing number.
26function! 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)
66endfunction
67
68" return incremented command line text.
69function! g:IncrementedCommandLine()
70    return s:updatedcommandline
71endfunction
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
81function! 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.'
117endfunction
118
119" recover user configuration
120let &cpo = s:save_cpo
121finish
122
123==============================================================================
124cmdline-increment.vim : コマンドライン行で数値インクリメント、デクリメント
125------------------------------------------------------------------------------
126$VIMRUNTIMEPATH/plugin/cmdline-increment.vim
127==============================================================================
128author : 小見 拓
129url : http://nanasi.jp/
130email : mail@nanasi.jp
131version : 2009/09/19 03:00:00
132==============================================================================
133コマンドラインモードで、限定的にですが、インクリメント、デクリメントを
134実行するプラグインです。
135
136    <C-a> インクリメント
137    <C-x> デクリメント
138
139このプラグインには、最後に現れた数値のみをインクリメント、デクリメントする、
140という制限があります。
141
142
143------------------------------------------------------------------------------
144[使い方]
145
1461. まず、コマンドラインモードになります。
1472. 次のコマンドを入力してください。
148
149        :edit workfile_1.txt
150
1513. そのまま、コマンドラインモードのままで、
152    Ctrl-a、Ctrl-x を押下してみてください。
153
154
155------------------------------------------------------------------------------
156[マッピングの変更]
157Ctrl-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]
1782009/09/17
179    - initial version.
180
1812009/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

Archive Download this file

Branches:
master