lhaskell.vim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. " Vim syntax file
  2. " Language: Haskell with literate comments, Bird style,
  3. " TeX style and plain text surrounding
  4. " \begin{code} \end{code} blocks
  5. " Maintainer: Haskell Cafe mailinglist <haskell-cafe@haskell.org>
  6. " Original Author: Arthur van Leeuwen <arthurvl@cs.uu.nl>
  7. " Last Change: 2010 Apr 11
  8. " Version: 1.04
  9. "
  10. " Thanks to Ian Lynagh for thoughtful comments on initial versions and
  11. " for the inspiration for writing this in the first place.
  12. "
  13. " This style guesses as to the type of markup used in a literate haskell
  14. " file and will highlight (La)TeX markup if it finds any
  15. " This behaviour can be overridden, both glabally and locally using
  16. " the lhs_markup variable or b:lhs_markup variable respectively.
  17. "
  18. " lhs_markup must be set to either tex or none to indicate that
  19. " you always want (La)TeX highlighting or no highlighting
  20. " must not be set to let the highlighting be guessed
  21. " b:lhs_markup must be set to eiterh tex or none to indicate that
  22. " you want (La)TeX highlighting or no highlighting for
  23. " this particular buffer
  24. " must not be set to let the highlighting be guessed
  25. "
  26. "
  27. " 2004 February 18: New version, based on Ian Lynagh's TeX guessing
  28. " lhaskell.vim, cweb.vim, tex.vim, sh.vim and fortran.vim
  29. " 2004 February 20: Cleaned up the guessing and overriding a bit
  30. " 2004 February 23: Cleaned up syntax highlighting for \begin{code} and
  31. " \end{code}, added some clarification to the attributions
  32. " 2008 July 1: Removed % from guess list, as it totally breaks plain
  33. " text markup guessing
  34. " 2009 April 29: Fixed highlighting breakage in TeX mode,
  35. " thanks to Kalman Noel
  36. "
  37. " quit when a syntax file was already loaded
  38. if exists("b:current_syntax")
  39. finish
  40. endif
  41. " First off, see if we can inherit a user preference for lhs_markup
  42. if !exists("b:lhs_markup")
  43. if exists("lhs_markup")
  44. if lhs_markup =~ '\<\%(tex\|none\)\>'
  45. let b:lhs_markup = matchstr(lhs_markup,'\<\%(tex\|none\)\>')
  46. else
  47. echohl WarningMsg | echo "Unknown value of lhs_markup" | echohl None
  48. let b:lhs_markup = "unknown"
  49. endif
  50. else
  51. let b:lhs_markup = "unknown"
  52. endif
  53. else
  54. if b:lhs_markup !~ '\<\%(tex\|none\)\>'
  55. let b:lhs_markup = "unknown"
  56. endif
  57. endif
  58. " Remember where the cursor is, and go to upperleft
  59. let s:oldline=line(".")
  60. let s:oldcolumn=col(".")
  61. call cursor(1,1)
  62. " If no user preference, scan buffer for our guess of the markup to
  63. " highlight. We only differentiate between TeX and plain markup, where
  64. " plain is not highlighted. The heuristic for finding TeX markup is if
  65. " one of the following occurs anywhere in the file:
  66. " - \documentclass
  67. " - \begin{env} (for env != code)
  68. " - \part, \chapter, \section, \subsection, \subsubsection, etc
  69. if b:lhs_markup == "unknown"
  70. if search('\\documentclass\|\\begin{\(code}\)\@!\|\\\(sub\)*section\|\\chapter|\\part','W') != 0
  71. let b:lhs_markup = "tex"
  72. else
  73. let b:lhs_markup = "plain"
  74. endif
  75. endif
  76. " If user wants us to highlight TeX syntax or guess thinks it's TeX, read it.
  77. if b:lhs_markup == "tex"
  78. runtime! syntax/tex.vim
  79. unlet b:current_syntax
  80. " Tex.vim removes "_" from 'iskeyword', but we need it for Haskell.
  81. setlocal isk+=_
  82. syntax cluster lhsTeXContainer contains=tex.*Zone,texAbstract
  83. else
  84. syntax cluster lhsTeXContainer contains=.*
  85. endif
  86. " Literate Haskell is Haskell in between text, so at least read Haskell
  87. " highlighting
  88. syntax include @haskellTop syntax/haskell.vim
  89. syntax region lhsHaskellBirdTrack start="^>" end="\%(^[^>]\)\@=" contains=@haskellTop,lhsBirdTrack containedin=@lhsTeXContainer
  90. syntax region lhsHaskellBeginEndBlock start="^\\begin{code}\s*$" matchgroup=NONE end="\%(^\\end{code}.*$\)\@=" contains=@haskellTop,beginCodeBegin containedin=@lhsTeXContainer
  91. syntax match lhsBirdTrack "^>" contained
  92. syntax match beginCodeBegin "^\\begin" nextgroup=beginCodeCode contained
  93. syntax region beginCodeCode matchgroup=texDelimiter start="{" end="}"
  94. " Define the default highlighting.
  95. " Only when an item doesn't have highlighting yet
  96. hi def link lhsBirdTrack Comment
  97. hi def link beginCodeBegin texCmdName
  98. hi def link beginCodeCode texSection
  99. " Restore cursor to original position, as it may have been disturbed
  100. " by the searches in our guessing code
  101. call cursor (s:oldline, s:oldcolumn)
  102. unlet s:oldline
  103. unlet s:oldcolumn
  104. let b:current_syntax = "lhaskell"
  105. " vim: ts=8