Beware exceptions when processing input
[MacVim.git] / runtime / syntax / lhaskell.vim
blob7ff09e7b3b523826e85d1f6201bbc104d3a44359
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:          2008 Jul 01
8 " Version:              1.02
10 " Thanks to Ian Lynagh for thoughtful comments on initial versions and
11 " for the inspiration for writing this in the first place.
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.
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
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
37 " For version 5.x: Clear all syntax items
38 " For version 6.x: Quit when a syntax file was already loaded
39 if version < 600
40   syntax clear
41 elseif exists("b:current_syntax")
42   finish
43 endif
45 " First off, see if we can inherit a user preference for lhs_markup
46 if !exists("b:lhs_markup")
47     if exists("lhs_markup")
48         if lhs_markup =~ '\<\%(tex\|none\)\>'
49             let b:lhs_markup = matchstr(lhs_markup,'\<\%(tex\|none\)\>')
50         else
51             echohl WarningMsg | echo "Unknown value of lhs_markup" | echohl None
52             let b:lhs_markup = "unknown"
53         endif
54     else
55         let b:lhs_markup = "unknown"
56     endif
57 else
58     if b:lhs_markup !~ '\<\%(tex\|none\)\>'
59         let b:lhs_markup = "unknown"
60     endif
61 endif
63 " Remember where the cursor is, and go to upperleft
64 let s:oldline=line(".")
65 let s:oldcolumn=col(".")
66 call cursor(1,1)
68 " If no user preference, scan buffer for our guess of the markup to
69 " highlight. We only differentiate between TeX and plain markup, where
70 " plain is not highlighted. The heuristic for finding TeX markup is if
71 " one of the following occurs anywhere in the file:
72 "   - \documentclass
73 "   - \begin{env}       (for env != code)
74 "   - \part, \chapter, \section, \subsection, \subsubsection, etc
75 if b:lhs_markup == "unknown"
76     if search('\\documentclass\|\\begin{\(code}\)\@!\|\\\(sub \)*section\|\\chapter|\\part','W') != 0
77         let b:lhs_markup = "tex"
78     else
79         let b:lhs_markup = "plain"
80     endif
81 endif
83 " If user wants us to highlight TeX syntax or guess thinks it's TeX,  read it.
84 if b:lhs_markup == "tex"
85     if version < 600
86         source <sfile>:p:h/tex.vim
87         set isk+=_
88     else
89         runtime! syntax/tex.vim
90         unlet b:current_syntax
91         " Tex.vim removes "_" from 'iskeyword', but we need it for Haskell.
92         setlocal isk+=_
93     endif
94 endif
96 " Literate Haskell is Haskell in between text, so at least read Haskell
97 " highlighting
98 if version < 600
99     syntax include @haskellTop <sfile>:p:h/haskell.vim
100 else
101     syntax include @haskellTop syntax/haskell.vim
102 endif
104 syntax region lhsHaskellBirdTrack start="^>" end="\%(^[^>]\)\@=" contains=@haskellTop,lhsBirdTrack
105 syntax region lhsHaskellBeginEndBlock start="^\\begin{code}\s*$" matchgroup=NONE end="\%(^\\end{code}.*$\)\@=" contains=@haskellTop,@beginCode
107 syntax match lhsBirdTrack "^>" contained
109 syntax match beginCodeBegin "^\\begin" nextgroup=beginCodeCode contained
110 syntax region beginCodeCode  matchgroup=texDelimiter start="{" end="}"
111 syntax cluster beginCode    contains=beginCodeBegin,beginCodeCode
113 " Define the default highlighting.
114 " For version 5.7 and earlier: only when not done already
115 " For version 5.8 and later: only when an item doesn't have highlighting yet
116 if version >= 508 || !exists("did_tex_syntax_inits")
117   if version < 508
118     let did_tex_syntax_inits = 1
119     command -nargs=+ HiLink hi link <args>
120   else
121     command -nargs=+ HiLink hi def link <args>
122   endif
124   HiLink lhsBirdTrack Comment
126   HiLink beginCodeBegin       texCmdName
127   HiLink beginCodeCode        texSection
129   delcommand HiLink
130 endif
132 " Restore cursor to original position, as it may have been disturbed
133 " by the searches in our guessing code
134 call cursor (s:oldline, s:oldcolumn)
136 unlet s:oldline
137 unlet s:oldcolumn
139 let b:current_syntax = "lhaskell"
141 " vim: ts=8