blob: cf1f126a3c66be45957c4d0da3eb2cacdd45eed9 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
2" Language: Haskell with literate comments, Bird style,
Bram Moolenaar9faec4e2021-02-27 16:38:07 +01003" Markdown style, TeX style and plain text surrounding
Bram Moolenaar071d4272004-06-13 20:20:40 +00004" \begin{code} \end{code} blocks
5" Maintainer: Haskell Cafe mailinglist <haskell-cafe@haskell.org>
6" Original Author: Arthur van Leeuwen <arthurvl@cs.uu.nl>
Bram Moolenaar9faec4e2021-02-27 16:38:07 +01007" Last Change: 2020 Feb 25
8" Version: 1.05
Bram Moolenaar071d4272004-06-13 20:20:40 +00009"
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
Bram Moolenaar5c736222010-01-06 20:54:52 +010032" 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000036"
37
38
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020039" quit when a syntax file was already loaded
40if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 finish
42endif
43
44" First off, see if we can inherit a user preference for lhs_markup
45if !exists("b:lhs_markup")
46 if exists("lhs_markup")
Bram Moolenaar9faec4e2021-02-27 16:38:07 +010047 if lhs_markup =~ '\<\%(tex\|md\|none\)\>'
48 let b:lhs_markup = matchstr(lhs_markup,'\<\%(tex\|md\|none\)\>')
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 else
50 echohl WarningMsg | echo "Unknown value of lhs_markup" | echohl None
51 let b:lhs_markup = "unknown"
52 endif
53 else
54 let b:lhs_markup = "unknown"
55 endif
56else
Bram Moolenaar9faec4e2021-02-27 16:38:07 +010057 if b:lhs_markup !~ '\<\%(tex\|md\|none\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 let b:lhs_markup = "unknown"
59 endif
60endif
61
62" Remember where the cursor is, and go to upperleft
63let s:oldline=line(".")
64let s:oldcolumn=col(".")
65call cursor(1,1)
66
67" If no user preference, scan buffer for our guess of the markup to
68" highlight. We only differentiate between TeX and plain markup, where
69" plain is not highlighted. The heuristic for finding TeX markup is if
70" one of the following occurs anywhere in the file:
71" - \documentclass
72" - \begin{env} (for env != code)
73" - \part, \chapter, \section, \subsection, \subsubsection, etc
74if b:lhs_markup == "unknown"
Bram Moolenaar5c736222010-01-06 20:54:52 +010075 if search('\\documentclass\|\\begin{\(code}\)\@!\|\\\(sub\)*section\|\\chapter|\\part','W') != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 let b:lhs_markup = "tex"
Bram Moolenaar9faec4e2021-02-27 16:38:07 +010077 elseif search('```haskell','W') != 0
78 let b:lhs_markup = "md"
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 else
80 let b:lhs_markup = "plain"
81 endif
82endif
83
Bram Moolenaar5c736222010-01-06 20:54:52 +010084" If user wants us to highlight TeX syntax or guess thinks it's TeX, read it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085if b:lhs_markup == "tex"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020086 runtime! syntax/tex.vim
87 unlet b:current_syntax
88 " Tex.vim removes "_" from 'iskeyword', but we need it for Haskell.
89 setlocal isk+=_
Bram Moolenaar5c736222010-01-06 20:54:52 +010090 syntax cluster lhsTeXContainer contains=tex.*Zone,texAbstract
Bram Moolenaar9faec4e2021-02-27 16:38:07 +010091elseif b:lhs_markup == "md"
92 runtime! syntax/markdown.vim
93 unlet b:current_syntax
94 syntax cluster lhsTeXContainer contains=markdown.*
Bram Moolenaar5c736222010-01-06 20:54:52 +010095else
96 syntax cluster lhsTeXContainer contains=.*
Bram Moolenaar071d4272004-06-13 20:20:40 +000097endif
98
99" Literate Haskell is Haskell in between text, so at least read Haskell
100" highlighting
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200101syntax include @haskellTop syntax/haskell.vim
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar5c736222010-01-06 20:54:52 +0100103syntax region lhsHaskellBirdTrack start="^>" end="\%(^[^>]\)\@=" contains=@haskellTop,lhsBirdTrack containedin=@lhsTeXContainer
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200104syntax region lhsHaskellBeginEndBlock start="^\\begin{code}\s*$" matchgroup=NONE end="\%(^\\end{code}.*$\)\@=" contains=@haskellTop,beginCodeBegin containedin=@lhsTeXContainer
Bram Moolenaar9faec4e2021-02-27 16:38:07 +0100105syntax region lhsHaskellMDBlock start="^```haskell$" matchgroup=NONE end="^```$" keepend contains=@haskellTop,lhsMarkdownCode containedin=@lhsTeXContainer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
107syntax match lhsBirdTrack "^>" contained
108
Bram Moolenaar9faec4e2021-02-27 16:38:07 +0100109syntax match lhsMarkdownCode "^\(```haskell\|^```\)$" contained
110
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111syntax match beginCodeBegin "^\\begin" nextgroup=beginCodeCode contained
112syntax region beginCodeCode matchgroup=texDelimiter start="{" end="}"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
114" Define the default highlighting.
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200115" Only when an item doesn't have highlighting yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200117hi def link lhsBirdTrack Comment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
Bram Moolenaar9faec4e2021-02-27 16:38:07 +0100119hi def link lhsMarkdownCode Comment
120
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200121hi def link beginCodeBegin texCmdName
122hi def link beginCodeCode texSection
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
125" Restore cursor to original position, as it may have been disturbed
126" by the searches in our guessing code
127call cursor (s:oldline, s:oldcolumn)
128
129unlet s:oldline
130unlet s:oldcolumn
131
132let b:current_syntax = "lhaskell"
133
134" vim: ts=8