Edit Page
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 24: | Line 24: | ||
then substituting 'getline(".")', use 'getline(line(".") - 1)' should work. |
then substituting 'getline(".")', use 'getline(line(".") - 1)' should work. |
||
− | Put the following in your |
+ | Put the following in your .vimrc: |
<pre> |
<pre> |
||
− | au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap } |
+ | au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap } <ESC>:call CurlyBracket()<CR>a |
function CurlyBracket() |
function CurlyBracket() |
||
Line 32: | Line 32: | ||
iunmap } |
iunmap } |
||
sil exe "normal i}" |
sil exe "normal i}" |
||
− | imap } |
+ | imap } <ESC>:call CurlyBracket()<CR> |
let l:result1 = searchpair('{', '', '}', 'bW') |
let l:result1 = searchpair('{', '', '}', 'bW') |
||
− | if (result1 |
+ | if (result1 > 0) |
let l:my_string = substitute(getline("."), '^\s*\(.*\){', '\1', "") |
let l:my_string = substitute(getline("."), '^\s*\(.*\){', '\1', "") |
||
sil exe ":" . l:my_linenum |
sil exe ":" . l:my_linenum |
||
Line 57: | Line 57: | ||
[4] I've added the line |
[4] I've added the line |
||
− | au BufNewFile,BufRead *.java,*.c,*.cc,*.C,*.h map |
+ | au BufNewFile,BufRead *.java,*.c,*.cc,*.C,*.h map <A-F1> :call CurlyBracket()<CR> |
to my .vimrc for those times when you started with a small if statement, and ended up with a mammoth one. Simply calls CurleyBracket to comment it. |
to my .vimrc for those times when you started with a small if statement, and ended up with a mammoth one. Simply calls CurleyBracket to comment it. |
||
Line 65: | Line 65: | ||
iunmap } |
iunmap } |
||
sil exe "normal i}" |
sil exe "normal i}" |
||
− | imap } |
+ | imap } <ESC>:sil call CurlyBracket()<CR>a |
let result1=searchpair('{', '', '}', 'bW') |
let result1=searchpair('{', '', '}', 'bW') |
||
let goline="" |
let goline="" |
||
let appendstr="" |
let appendstr="" |
||
− | if (result1 |
+ | if (result1 > 0) |
let l:my_startline=line(".") |
let l:my_startline=line(".") |
||
− | if (l:my_linenum - l:my_startline |
+ | if (l:my_linenum - l:my_startline > 10) |
let l:my_string=substitute(getline(line(".")-1),"^ *","","g") |
let l:my_string=substitute(getline(line(".")-1),"^ *","","g") |
||
let l:shortstring=strpart(l:my_string,0,30)."..." |
let l:shortstring=strpart(l:my_string,0,30)."..." |
||
Line 86: | Line 86: | ||
Okay. I have changed this script. It takes into account the last note and some other things I noticed. |
Okay. I have changed this script. It takes into account the last note and some other things I noticed. |
||
− | # This was annoying when I had a short if(){}else{} statement, so I changed the mapping to } |
+ | # This was annoying when I had a short if(){}else{} statement, so I changed the mapping to }<CR>. |
# If there is an else statement, the comment inserted is the if statement. followed by an else { |
# If there is an else statement, the comment inserted is the if statement. followed by an else { |
||
− | # |
+ | # >10 lines check put in. |
− | # |
+ | # < 30 char check put in |
Notes: |
Notes: |
||
Line 96: | Line 96: | ||
<pre> |
<pre> |
||
− | au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap } |
+ | au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap }<CR> <ESC>:call CurlyBracket()<CR>a |
function CurlyBracket() |
function CurlyBracket() |
||
let l:startline = line(".") |
let l:startline = line(".") |
||
let l:result1 = searchpair('{', '', '}', 'bW', '') |
let l:result1 = searchpair('{', '', '}', 'bW', '') |
||
− | if (result1 |
+ | if (result1 > 0) |
let l:linenum = line(".") |
let l:linenum = line(".") |
||
let l:string1 = getline(l:linenum) |
let l:string1 = getline(l:linenum) |
||
Line 107: | Line 107: | ||
sil exe "normal 0" |
sil exe "normal 0" |
||
let l:result2 = searchpair('{', '', '}', 'bW', '') |
let l:result2 = searchpair('{', '', '}', 'bW', '') |
||
− | if (l:result2 |
+ | if (l:result2 > 0) |
let l:string1 = getline(".") . l:string1 |
let l:string1 = getline(".") . l:string1 |
||
endif |
endif |
||
Line 115: | Line 115: | ||
sil exe ":" . l:startline |
sil exe ":" . l:startline |
||
sil exe "normal i}" |
sil exe "normal i}" |
||
− | if ((l:startline - l:linenum) |
+ | if ((l:startline - l:linenum) > 10) |
sil exe "normal a //" . l:my_string |
sil exe "normal a //" . l:my_string |
||
endif |
endif |
||
Line 151: | Line 151: | ||
[2] The number of lines is taken form the start of the "if" (in case of if-else) |
[2] The number of lines is taken form the start of the "if" (in case of if-else) |
||
− | [3] The "..." is added only if the length of the stringis |
+ | [3] The "..." is added only if the length of the stringis > 30. |
======= |
======= |
||
Line 159: | Line 159: | ||
" Automatically comments {} in C/C++/Java. |
" Automatically comments {} in C/C++/Java. |
||
− | au BufNewFile,BufRead *.c,*.cc,*.h imap } |
+ | au BufNewFile,BufRead *.c,*.cc,*.h imap }<CR> <ESC>:call CurlyBracket()<CR>a |
− | au BufNewFile,BufRead *.cpp,*.C imap } |
+ | au BufNewFile,BufRead *.cpp,*.C imap }<CR> <ESC>:call CurlyBracket()<CR>a |
− | au BufNewFile,BufRead *.java,*.idl imap } |
+ | au BufNewFile,BufRead *.java,*.idl imap }<CR> <ESC>:call CurlyBracket()<CR>a |
function CurlyBracket() |
function CurlyBracket() |
||
let l:startline = line(".") |
let l:startline = line(".") |
||
let l:result1 = searchpair('{', '', '}', 'bW') |
let l:result1 = searchpair('{', '', '}', 'bW') |
||
− | if (result1 |
+ | if (result1 > 0) |
let l:linenum = line(".") |
let l:linenum = line(".") |
||
let l:string1 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "") |
let l:string1 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "") |
||
Line 178: | Line 178: | ||
sil exe "normal 0" |
sil exe "normal 0" |
||
let l:result2 = searchpair('{', '', '}', 'bW') |
let l:result2 = searchpair('{', '', '}', 'bW') |
||
− | if (l:result2 |
+ | if (l:result2 > 0) |
let l:linenum = line(".") |
let l:linenum = line(".") |
||
let l:string2 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "") |
let l:string2 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "") |
||
Line 191: | Line 191: | ||
let l:my_string = substitute(l:string1, '\s*{[^{]*$', '', "") |
let l:my_string = substitute(l:string1, '\s*{[^{]*$', '', "") |
||
let l:my_strlen = strlen(l:my_string) |
let l:my_strlen = strlen(l:my_string) |
||
− | if (l:my_strlen |
+ | if (l:my_strlen > 30) |
let l:my_string = strpart(l:my_string,0,30)."..." |
let l:my_string = strpart(l:my_string,0,30)."..." |
||
endif |
endif |
||
Line 197: | Line 197: | ||
sil exe ":" . l:startline |
sil exe ":" . l:startline |
||
sil exe "normal i}" |
sil exe "normal i}" |
||
− | if ((l:startline - l:linenum) |
+ | if ((l:startline - l:linenum) > 10) |
sil exe "normal a /* " . l:my_string . " */" |
sil exe "normal a /* " . l:my_string . " */" |
||
endif |
endif |
||
Line 218: | Line 218: | ||
---- |
---- |
||
− | People facing Problems with running this script can try removing the |
+ | People facing Problems with running this script can try removing the <space> after "iunmap }" statement. |
---- |
---- |