looking for an advice how to programatically detect, if current Vim's buffer contains at least one fold defined ? Regardless if a fold is open or closed.
Attempting to call mkview only if there is a fold defined in current buffer:
autocmd BufWrite ?* if fold_defined() | mkview | endif
function fold_defined()
???
endfunction
function! HasFold()
let view = winsaveview()
let fold = 0
for move in ['zj', 'zk']
exe 'keepj norm!' move
if foldlevel('.') > 0
let fold = 1
break
endif
endfor
call winrestview(view)
return fold
endfunction
Based on perreal's advice, I did wrote one of possible solutions to my question:
" Detect presence of fold definition in the current buffer
function FoldDefined()
let result = 0
let save_cursor = getpos('.')
call cursor(1,1)
let scanline = line('.')
let lastline = line('$')
while scanline <= lastline
if foldlevel(scanline) > 0
let result = 1
break
endif
let scanline = scanline + 1
endwhile
call setpos('.', save_cursor)
return result
endfunction
function! FoldDefined()
return len(filter(range(1, line('$')), 'foldlevel(v:val)>1'))>0
endfunction
Related
Does it support following code?
More precisely, does it support defining function? invoking function system? and so on.
Following code is used to auto close my Chinese input method when quit from insert normal.
let g:input_toggle = 1
function! Fcitx2en()
let s:input_status = system("fcitx-remote")
if s:input_status == 2
let g:input_toggle = 1
let l:a = system("fcitx-remote -c")
endif
endfunction
function! Fcitx2zh()
let s:input_status = system("fcitx-remote")
if s:input_status != 2 && g:input_toggle == 1
let l:a = system("fcitx-remote -o")
let g:input_toggle = 0
endif
endfunction
set timeoutlen=150
autocmd InsertLeave * call Fcitx2en()
"autocmd InsertEnter * call Fcitx2zh()
No, IdeaVim understands only several configuration options in ~/.ideavimrc, the rest is ignored. See this feature request for details.
I am using an adaptation of the VIM folding settings published here and here. Now, notice the program below:
When folded on my VIM, it produces the following result:
I don't really like that look, I'd prefer the following:
Which I got by rewriting the code:
But leaving the code like that is horrible. Is it possible to update the VimScript so I get the folding I want, without having to leave the JavaScript code like that?
Here is my exact setting:
setlocal foldmethod=expr
setlocal foldexpr=GetPotionFold(v:lnum)
setlocal foldminlines=0
function! s:NextNonBlankLine(lnum)
let numlines = line('$')
let current = a:lnum + 1
while current <= numlines
if getline(current) =~? '\v\S'
return current
endif
let current += 1
endwhile
return -2
endfunction
function! s:IndentLevel(lnum)
return indent(a:lnum) / &shiftwidth
endfunction
function! GetPotionFold(lnum)
if getline(a:lnum) =~? '\v^\s*$'
return '-1'
endif
let this_indent = <SID>IndentLevel(a:lnum)
let next_indent = <SID>IndentLevel(<SID>NextNonBlankLine(a:lnum))
if next_indent == this_indent
return this_indent
elseif next_indent < this_indent
return this_indent
elseif next_indent > this_indent
return '>' . next_indent
endif
endfunction
function! NeatFoldText()
let line = getline(v:foldstart)
let lines_count = v:foldend - v:foldstart + 1
let lines_count_text = '| ' . printf("%10s", lines_count . ' lines') . ' |'
let foldchar = ' '
let foldtextstart = strpart(line, 0, (winwidth(0)*2)/3)
let foldtextend = lines_count_text . repeat(foldchar, 6)
let foldtextlength = strlen(substitute(foldtextstart . foldtextend, '.', 'x', 'g')) + &foldcolumn
return foldtextstart . repeat(foldchar, winwidth(0)-foldtextlength) . foldtextend
endfunction
set foldtext=NeatFoldText()
hi Folded ctermbg=255 ctermfg=21
hi FoldColumn ctermbg=white ctermfg=darkred
And here is the sample code:
function foo(x){
var y = x*x;
var z = y+y;
return z;
};
function bar(x){
var y = x*x;
var z = y+y;
return z;
};
function foobar(x){
var y = x*x;
var z = y+y;
return z;
};
function barfoo(x){
var y = x*x;
var z = y+y;
return z;
};
set foldmethod=marker
set foldmarker={,}
Should give you what you want.
You are using a fold expression that is (as far as I can see from a glance) designed for indent folding (used for HAML, Python, Haskell and other indent-syntax languages).
For C, you should just be using :set foldmethod=syntax.
Here is what I do
:syntax match conceal Test +[A-Z0-9]\{6}+
:set conceallevel=2
:set concealcursor=nvi
So when I write 123456 in vim I expect it to be nothing there. But what actually happens when I move over that area is that I have to move 6 times in the direction I want to move for the cursor to pass that area.
Is there a way to work around this? I want vim to see it as if there is nothing there and that when I move over that area it's like there is nothing there. But I still want to be able to search for it and delete it.
Currently there is no built-in way to do this. You can use synconcealed() to determine whether there is a concealed character under the cursor and what it is concealed to and remap all moving keys to respect it: like this:
function! ForwardSkipConceal(count)
let cnt=a:count
let mvcnt=0
let c=col('.')
let l=line('.')
let lc=col('$')
let line=getline('.')
while cnt
if c>=lc
let mvcnt+=cnt
break
endif
if stridx(&concealcursor, 'n')==-1
let isconcealed=0
else
let [isconcealed, cchar, group]=synconcealed(l, c)
endif
if isconcealed
let cnt-=strchars(cchar)
let oldc=c
let c+=1
while c<lc && synconcealed(l, c)[2]==group | let c+=1 | endwhile
let mvcnt+=strchars(line[oldc-1:c-2])
else
let cnt-=1
let mvcnt+=1
let c+=len(matchstr(line[c-1:], '.'))
endif
endwhile
return ":\<C-u>\e".mvcnt.'l'
endfunction
nnoremap <expr> l ForwardSkipConceal(v:count1)
. Note: this does the thing for one single motion (l) and in normal mode, just to show the way it may be done.
ZyX's solution from above did not work for me: apparently the ID of a concealed text region can change while traversing it, causing the motion to stop prematurely.
I have been using an alternate version pasted below (also with the missing BackwardSkipConceal function). It's not pretty but works well when substituting mathematical characters in LaTeX documents or C++ code.
function! ForwardSkipConceal(count)
let cnt=a:count
let mvcnt=0
let c=col('.')
let l=line('.')
let lc=col('$')
let line=getline('.')
while cnt
if c>=lc
let mvcnt+=cnt
break
endif
if stridx(&concealcursor, 'n')==-1
let isconcealed=0
else
let [isconcealed, cchar, group] = synconcealed(l, c)
endif
if isconcealed
let cnt-=strchars(cchar)
let oldc=c
let c+=1
while c < lc
let [isconcealed2, cchar2, group2] = synconcealed(l, c)
if !isconcealed2 || cchar2 != cchar
break
endif
let c+= 1
endwhile
let mvcnt+=strchars(line[oldc-1:c-2])
else
let cnt-=1
let mvcnt+=1
let c+=len(matchstr(line[c-1:], '.'))
endif
endwhile
return ":\<C-u>\e".mvcnt.'l'
endfunction
function! BackwardSkipConceal(count)
let cnt=a:count
let mvcnt=0
let c=col('.')
let l=line('.')
let lc=0
let line=getline('.')
while cnt
if c<=1
let mvcnt+=cnt
break
endif
if stridx(&concealcursor, 'n')==-1 || c == 0
let isconcealed=0
else
let [isconcealed, cchar, group]=synconcealed(l, c-1)
endif
if isconcealed
let cnt-=strchars(cchar)
let oldc=c
let c-=1
while c>1
let [isconcealed2, cchar2, group2] = synconcealed(l, c-1)
if !isconcealed2 || cchar2 != cchar
break
endif
let c-=1
endwhile
let c = max([c, 1])
let mvcnt+=strchars(line[c-1:oldc-2])
else
let cnt-=1
let mvcnt+=1
let c-=len(matchstr(line[:c-2], '.$'))
endif
endwhile
return ":\<C-u>\e".mvcnt.'h'
endfunction
nnoremap <expr> l ForwardSkipConceal(v:count1)
nnoremap <expr> h BackwardSkipConceal(v:count1)
I use the supertab plugin in vim.
These are my default settings (in _vimrc)
let g:SuperTabDefaultCompletionType = '<c-x><c-k>' -->(dictionary)
let g:SuperTabRetainCompletionDuration = "completion"
let g:SuperTabLongestEnhanced = 1
let g:SuperTabLongestHighlight = 1
I created this script to select omnicomplete with supertab on the fly:
function! SuperTabFunction()
if !exists("WhatSuperTab")
let WhatSuperTab = "SuperTab function?"
endif
if !exists("MenuSuperTab_choices")
let MenuSuperTab_choices = "&Current page\n&Spellchecker\nSentence\nCode"
endif
let n = confirm(WhatSuperTab, MenuSuperTab_choices, "Question")
if n == 1
let g:SuperTabDefaultCompletionType = '<c-x><c-m>'
so $VIM/vimfiles/plugin/supertab.vim
elseif n == 2
let g:SuperTabDefaultCompletionType = '<c-x><c-k>'
so $VIM/vimfiles/plugin/supertab.vim
elseif n == 3
let g:SuperTabDefaultCompletionType = '<c-x><c-l>'
so $VIM/vimfiles/plugin/supertab.vim
elseif n == 4
let g:SuperTabDefaultCompletionType = '<c-x><c-o>'
so $VIM/vimfiles/plugin/supertab.vim
else
return ''
endif
endfun
nmap <silent> <C-S-tab> :call SuperTabFunction()<CR>
imap <silent> <C-S-tab> <esc>:call SuperTabFunction()<CR>a
When I invoke above function and choose p.e. "Sentence"
I can use super tab to complete sentences
When I invoke above function again and choose p.e. "Spellchecker"
The correct value is assigned to g:SuperTabDefaultCompletionType
but it still replaces sentences.
What did I wrong in this function?
Have you tried calling the provided function instead of altering the global variable directly?
" SuperTabSetDefaultCompletionType(type) {{{
" Globally available function that users can use to set the default
" completion type for the current buffer, like in an ftplugin.
function! SuperTabSetDefaultCompletionType(type)
It would be very nice to have an option that would show all the column numbers of the current line or maybe of all the buffer, so I could know where exactly to navigate. Is there such an option or do i have to program it myself (nooo XD)?
:h 'statusline'
It is as easy as defining exactly what you what to see printed. e.g.
" RulerStr() comes from http://www.vanhemert.co.uk/vim/vimacros/ruler2.vim
function! RulerStr()
let columns = &columns
let inc = 0
let str = ""
while (inc < columns)
let inc10 = inc / 10 + 1
let buffer = "."
if (inc10 > 9)
let buffer = ""
endif
let str .= "....+..." . buffer . inc10
let inc += 10
endwhile
let str = strpart(str, 0, columns)
return str
endfunction
let s:saved_stl = {}
function! s:ToggleRuler()
let buf = bufnr('%')
if has_key(s:saved_stl, buf)
let &l:stl = s:saved_stl[buf]
unlet s:saved_stl[buf]
else
let s:saved_stl[buf] = &l:stl
setlocal stl=%{RulerStr()}
endif
endfunction
nnoremap <silent> ยต :call <sid>ToggleRuler()<cr>
You can use "set ruler". It will show the line number and column position at the bottom.