[haschool] Partial program from today.

Jason Dusek jason.dusek at gmail.com
Wed Jun 17 06:34:43 UTC 2009


  My `.vimrc` has some useful Haskell formatting things in it. I
  have excerpted the relevant parts below.

--
Jason Dusek




function! haskell:module()
  let s                      =  expand('%:.:r:gs?/?.?')
  call append(".", "module " . s . " where")
endfunction

function! haskell:line_fill(beg, mid, end)
  if strlen(a:beg) <= 27 && strlen(a:mid) <= 2 && strlen(a:end) <= 46
    let pad_beg              =  a:beg . repeat(" ", 27 - strlen(a:beg))
    let pad_mid              =  repeat(" ", 3 - strlen(a:mid)) . a:mid . "  "
    call setline(".", pad_beg . pad_mid . a:end)
  endif
endfunction

function! haskell:align()
  let leading                =  '^( *[^ ]+( +[^ ]+)*) +'
  let trailing               =  ' +([^ ]+( +[^ ]+)*) *$'
  let defines_pat            =  '\v' . leading . '(\=|\<-)' . trailing
  let signature_pat          =  '\v' . leading .   '(::)'   . trailing
  let case_pat               =  '\v' . leading .  '(-\>)'   . trailing
  let text                   =  getline(".")
  let defines                =  matchlist(text, defines_pat)
  if text !~ '\v^ *--'
    if ! empty(defines)
      call haskell:line_fill(defines[1], defines[3], defines[4])
    else
      let signature          =  matchlist(text, signature_pat)
      if ! empty(signature)
        call haskell:line_fill(signature[1], signature[3], signature[4])
      else
        let case             =  matchlist(text, case_pat)
        if ! empty(case)
          " Ignore '->' when in data definitions and multi-line type signatures.
          if text !~ '\v^ +(-\>|::|\=\>|\=|\|)'
            call haskell:line_fill(case[1], case[3], case[4])
          endif
        endif
      endif
    endif
  endif
endfunction

function! haskell:align_all()
  let a                      =  line(".")
  let b                      =  col(".")
  % call haskell:align()
  call cursor(a, b)
endfunction

function! haskell:comment_horizontal_rule(which)
  let text                   =  system("yes ' -' | sed '38 q' | tr -d '\n'")
  if a:which == "begin"
    call append(".", "{-" . text)
  else
    call append(".", text . " -}")
  endif
endfunction

function! haskell:comment_haddock()
  call append(".", " -}")
  call append(".", "{-| ")
  normal 1j
  normal 5|
endfunction

function! haskell:comment_block()
  call haskell:comment_horizontal_rule("end")
  call append(".", "  ")
  call haskell:comment_horizontal_rule("begin")
  normal 2j
  normal 3|
endfunction

command! -nargs=+ -complete=custom,haskell:complete HS
\ :silent! call haskell:dispatch("<args>")<CR>

function! haskell:complete(A,C,P)
  return join(keys(g:HS), "\n")
endfunction

function! haskell:dispatch(which)
  call g:HS[a:which]()
endfunction

let HS =
\ { "align all" : function("haskell:align_all")
\ , "haddock comment block" : function("haskell:comment_haddock")
\ , "module header" : function("haskell:module")
\ , "block comment" : function("haskell:comment_block")
\ }




function! cabal:align()
  let matches                =  matchlist(getline("."), '\v(^.+):( +)(.+)$')
  let diff                   =  18 - strlen(matches[1])
  if diff > 0
    call setline(".", matches[1] . repeat(" ", diff) . ": " . matches[3])
  endif
endfunction

function! cabal:align_all()
  let a                      =  line(".")
  let b                      =  col(".")
  % call cabal:align()
  call cursor(a, b)
endfunction

command! -nargs=+ -complete=custom,cabal:complete CABAL
\ :silent! call cabal:dispatch("<args>")<CR>

function! cabal:complete(A,C,P)
  return join(keys(g:CABAL), "\n")
endfunction

function! cabal:dispatch(which)
  call g:CABAL[a:which]()
endfunction

let CABAL =
\ { "align all" : function("cabal:align_all")
\ }



More information about the haschool mailing list