2014-07-21 02:19:52 +04:00
# http://haskell.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# Detection
# ‾‾‾‾‾‾‾‾‾
hook global BufCreate .*[.](hs) %{
2017-11-03 10:34:41 +03:00
set-option buffer filetype haskell
2014-07-21 02:19:52 +04:00
}
2019-04-10 06:54:19 +03:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=haskell %{
2019-03-13 21:15:59 +03:00
require-module haskell
2019-04-10 06:54:19 +03:00
2019-09-26 16:25:57 +03:00
set-option buffer extra_word_chars '_' "'"
2019-10-16 12:19:43 +03:00
hook window ModeChange pop:insert:.* -group haskell-trim-indent haskell-trim-indent
2019-04-10 06:54:19 +03:00
hook window InsertChar \n -group haskell-indent haskell-indent-on-new-line
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window haskell-.+ }
}
hook -group haskell-highlight global WinSetOption filetype=haskell %{
add-highlighter window/haskell ref haskell
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/haskell }
2019-03-13 21:15:59 +03:00
}
2019-04-10 06:54:19 +03:00
2019-03-13 21:15:59 +03:00
provide-module haskell %[
2014-07-21 02:19:52 +04:00
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell regions
add-highlighter shared/haskell/code default-region group
2018-07-02 13:59:12 +03:00
add-highlighter shared/haskell/string region (?<!'\\)(?<!')" (?<!\\)(\\\\)*" fill string
add-highlighter shared/haskell/macro region ^\h*?\K# (?<!\\)\n fill meta
add-highlighter shared/haskell/pragma region -recurse \{- \{-# '#-\}' fill meta
add-highlighter shared/haskell/comment region -recurse \{- \{- -\} fill comment
add-highlighter shared/haskell/line_comment region --(?:[^!#$%&*+./<>?@\\\^|~=]|$) $ fill comment
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex (?<!')\b0x+[A-Fa-f0-9]+ 0:value
add-highlighter shared/haskell/code/ regex (?<!')\b\d+([.]\d+)? 0:value
add-highlighter shared/haskell/code/ regex (?<!')\b(import|hiding|qualified|module)(?!')\b 0:keyword
add-highlighter shared/haskell/code/ regex (?<!')\b(import)(?!')\b[^\n]+(?<!')\b(as)(?!')\b 2:keyword
add-highlighter shared/haskell/code/ regex (?<!')\b(class|data|default|deriving|infix|infixl|infixr|instance|module|newtype|pattern|type|where)(?!')\b 0:keyword
add-highlighter shared/haskell/code/ regex (?<!')\b(case|do|else|if|in|let|mdo|of|proc|rec|then)(?!')\b 0:attribute
2017-05-15 22:45:05 +03:00
# The complications below is because period has many uses:
# As function composition operator (possibly without spaces) like "." and "f.g"
# Hierarchical modules like "Data.Maybe"
# Qualified imports like "Data.Maybe.Just", "Data.Maybe.maybe", "Control.Applicative.<$>"
# Quantifier separator in "forall a . [a] -> [a]"
# Enum comprehensions like "[1..]" and "[a..b]" (making ".." and "Module..." illegal)
# matches uppercase identifiers: Monad Control.Monad
# not non-space separated dot: Just.const
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex \b([A-Z]['\w]*\.)*[A-Z]['\w]*(?!['\w])(?![.a-z]) 0:variable
2017-05-15 22:45:05 +03:00
# matches infix identifier: `mod` `Apa._T'M`
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex `\b([A-Z]['\w]*\.)*[\w]['\w]*` 0:operator
2017-05-15 22:45:05 +03:00
# matches imported operators: M.! M.. Control.Monad.>>
# not operator keywords: M... M.->
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex \b[A-Z]['\w]*\.[~<=>|:!?/.@$*&#%+\^\-\\]+ 0:operator
2017-05-15 22:45:05 +03:00
# matches dot: .
# not possibly incomplete import: a.
# not other operators: !. .!
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex (?<![\w~<=>|:!?/.@$*&#%+\^\-\\])\.(?![~<=>|:!?/.@$*&#%+\^\-\\]) 0:operator
2017-05-15 22:45:05 +03:00
# matches other operators: ... > < <= ^ <*> <$> etc
# not dot: .
# not operator keywords: @ .. -> :: ~
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex (?<![~<=>|:!?/.@$*&#%+\^\-\\])[~<=>|:!?/.@$*&#%+\^\-\\]+ 0:operator
2017-05-15 22:45:05 +03:00
# matches operator keywords: @ ->
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex (?<![~<=>|:!?/.@$*&#%+\^\-\\])(@|~|<-|->|=>|::|=|:|[|])(?![~<=>|:!?/.@$*&#%+\^\-\\]) 1:keyword
2017-05-15 22:45:05 +03:00
# matches: forall [..variables..] .
# not the variables
2019-11-18 11:14:02 +03:00
add-highlighter shared/haskell/code/ regex \b(forall|∀)\b[^.\n]*?(\.) 1:keyword 2:keyword
2017-05-15 22:45:05 +03:00
# matches 'x' '\\' '\'' '\n' '\0'
# not incomplete literals: '\'
# not valid identifiers: w' _'
2018-06-28 15:10:22 +03:00
add-highlighter shared/haskell/code/ regex \B'([^\\]|[\\]['"\w\d\\])' 0:string
2017-05-15 22:45:05 +03:00
# this has to come after operators so '-' etc is correct
2014-07-21 02:19:52 +04:00
2019-09-29 01:38:43 +03:00
# matches function names in type signatures
add-highlighter shared/haskell/code/ regex ^\h*(?:(?:where|let|default)\h+)?([_a-z]['\w]*)\s+::\s 1:meta
# matches quasiquotes
2019-11-18 11:14:41 +03:00
add-highlighter shared/haskell/code/ regex \[\b[\w]['\w]*\|(.*)\|\] 1:string
2019-09-29 01:38:43 +03:00
2019-11-18 11:15:33 +03:00
# matches deriving strategies
add-highlighter shared/haskell/code/ regex \bderiving\s+\b(stock|newtype|anyclass|via)\b 1:keyword
add-highlighter shared/haskell/code/ regex \bderiving\s+[^\s]+?\s+\b(via)\b 1:keyword
2014-07-21 02:19:52 +04:00
# Commands
# ‾‾‾‾‾‾‾‾
# http://en.wikibooks.org/wiki/Haskell/Indentation
2018-12-19 12:10:26 +03:00
define-command -hidden haskell-trim-indent %{
2015-11-04 12:48:47 +03:00
# remove trailing white spaces
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft -itersel <a-x> s \h+$ <ret> d }
2014-07-21 02:19:52 +04:00
}
2017-11-03 10:34:41 +03:00
define-command -hidden haskell-indent-on-new-line %{
2017-11-03 11:09:45 +03:00
evaluate-commands -draft -itersel %{
2017-01-11 16:56:48 +03:00
# copy -- comments prefix and following white spaces
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft k <a-x> s ^\h*\K--\h* <ret> y gh j P }
2014-07-21 02:19:52 +04:00
# preserve previous line indent
2019-10-22 12:02:06 +03:00
try %{ execute-keys -draft <semicolon> K <a-&> }
2014-07-21 02:19:52 +04:00
# align to first clause
2019-10-22 12:02:06 +03:00
try %{ execute-keys -draft <semicolon> k x X s ^\h*(if|then|else)?\h*(([\w']+\h+)+=)?\h*(case\h+[\w']+\h+of|do|let|where)\h+\K.* <ret> s \A|.\z <ret> & }
2014-07-21 02:19:52 +04:00
# filter previous line
2018-12-19 12:10:26 +03:00
try %{ execute-keys -draft k : haskell-trim-indent <ret> }
2014-07-21 02:19:52 +04:00
# indent after lines beginning with condition or ending with expression or =(
2019-10-22 12:02:06 +03:00
try %{ execute-keys -draft <semicolon> k x <a-k> ^\h*(if)|(case\h+[\w']+\h+of|do|let|where|[=(])$ <ret> j <a-gt> }
2014-07-21 02:19:52 +04:00
}
}
2019-03-13 21:15:59 +03:00
]