2012-01-06 8 views

Respuesta

1

Lo más probable es que se puede crear una función para comprobar si el archivo tiene pliegues, como:

function HasFoldedLine() 
    let lnum=1 
    while lnum <= line("$") 
     if (foldclosed(lnum) > -1) 
      return 1 
     endif 
     let lnum+=1 
    endwhile 
    return 0 
endfu 

Ahora se puede utilizar con un poco de autocommand, por ejemplo:

au CursorHold * if HasFoldedLine() == 1 | set fdc=1 | else |set fdc=0 | endif 

HTH

+0

Esto es bastante lento. –

+0

Tiene usted razón, ahora muéstrenos una mejor solución. –

+0

Tengo la intención de, más tarde :) –

4

Mi método es más rápido que @Zsolt Botykai cuando los archivos crecen lo suficiente. Para archivos pequeños, me imagino que la diferencia de tiempo es insignificante. En lugar de verificar cada línea para un pliegue, la función simplemente intenta moverse entre pliegues. Si el cursor nunca se mueve, no hay pliegues.

function HasFolds() 
    "Attempt to move between folds, checking line numbers to see if it worked. 
    "If it did, there are folds. 

    function! HasFoldsInner() 
     let origline=line('.') 
     :norm zk 
     if origline==line('.') 
      :norm zj 
      if origline==line('.') 
       return 0 
      else 
       return 1 
      endif 
     else 
      return 1 
     endif 
     return 0 
    endfunction 

    let l:winview=winsaveview() "save window and cursor position 
    let foldsexist=HasFoldsInner() 
    if foldsexist 
     set foldcolumn=1 
    else 
     "Move to the end of the current fold and check again in case the 
     "cursor was on the sole fold in the file when we checked 
     if line('.')!=1 
      :norm [z 
      :norm k 
     else 
      :norm ]z 
      :norm j 
     endif 
     let foldsexist=HasFoldsInner() 
     if foldsexist 
      set foldcolumn=1 
     else 
      set foldcolumn=0 
     endif 
    end 
    call winrestview(l:winview) "restore window/cursor position 
endfunction 

au CursorHold,BufWinEnter ?* call HasFolds() 
0

(auto Shameless plug-in )

he creado un plugin para hacer esto llama Auto Origami, modelado en @ de answer SnoringFrog.

dejar caer el siguiente ejemplo en su vimrc después de instalarlo para ver la magia suceda (y leer :help auto-origami para encontrar la manera de poner a punto): I

augroup autofoldcolumn 
    au! 

    " Or whatever autocmd-events you want 
    au CursorHold,BufWinEnter * let &foldcolumn = auto_origami#Foldcolumn() 
augroup END 
Cuestiones relacionadas