0

I would like to get foldexpr to work on my .emacs file like outshine.el does. In other words, I would like to get the fold levels as in this example:

        0
;;;     0
        1
        1
;;;;    1
        2
        2
;;;;;   2
        3
;;;;    1
        2
;;;     0
        1
        1

(Notation like that found at Advanced Folding _ Learn Vimscript the Hard Way.)

In summary, I think the foldexpr will have to check current lines and previous lines.

If the current line has at least 3 ;, then fold level will be 3 less. Otherwise, if the previous line has at least 3, the current will have fold level 2 less, else fold level should be equal to previous line.

That is the effect I tried to achieve with

function ELispLevel() 
    let n = len(matchstr(getline(v:lnum), '^;\+'))
    let m = len(matchstr(getline(v:lnum-1), '^;\+'))
    if n >= 2
        return ">" . n-3
    else 
        if m >= 2
            return ">" . m-2
        else 
            return "=" 
    endif 
endfunction

(Credit for most of the code goes to the discussion of markdown folding at Vim Markdown Folding? - Stack Overflow, especially a comment by Omar AntolĂ­n-Camarena.)

I am guessing this does not work. Some of my less trivial modifications include the n-3 and m-2, so maybe I just screwed up the syntax there. But I'm afraid my error could be anywhere, and hope someone can find it.

I also have the following to try to activate it for my init.el file,

aug filetype_lisp
    au!
    " au FileType lisp setl fdm=marker fmr=;;;,;;+
    au FileType lisp setlocal foldmethod=expr foldexpr=ELispLevel()
aug END

(BTW, a third link that I learned some of this from is vim - Explaining foldexpr syntax - Stack Overflow)

Community
  • 1
  • 1
Brady Trainor
  • 2,026
  • 20
  • 18

1 Answers1

0

First I had to delete a useless v:lnum in an autocmd line, and use lets for the subtractions. Once folding worked at all, I've reassessed and found my desired level assignment was incorrect. I think it should be

        0
;;;     1
        1
        1
;;;;    2
        2
        2
;;;;;   3
        3
;;;;    2
        2
;;;     1
        1
        1

I did not anticipate how Vim actually does it's folding. This seems actually easier to implement.

It works!

function! ELispLevel() 
    let n = len(matchstr(getline(v:lnum), '^;\+'))
    let m = n - 2
    if n >= 3
        return ">" . m
    else 
        return "=" 
    endif 
endfunction
Brady Trainor
  • 2,026
  • 20
  • 18