Skip to content

Instantly share code, notes, and snippets.

@dmwit
Created August 20, 2018 12:10
Show Gist options
  • Select an option

  • Save dmwit/88c2c9806191075b32b491864efaafc9 to your computer and use it in GitHub Desktop.

Select an option

Save dmwit/88c2c9806191075b32b491864efaafc9 to your computer and use it in GitHub Desktop.
updated haskell.vim for fragamus
let b:align_language_pragma = 0
setlocal cpoptions+=M
iabbrev <buffer> H# {-# #-}<Left><Left><Left><Left>
iabbrev <buffer> HA Applicative
iabbrev <buffer> HB Bounded, Enum, Eq, Ord, Read, Show
iabbrev <buffer> HBS ByteString
iabbrev <buffer> HC Control
iabbrev <buffer> HD Data
iabbrev <buffer> Hd deriving ()<Left>
iabbrev <buffer> Hda data =<CR>deriving (Eq, Ord, Read, Show)<Up><End><Left><Left>
iabbrev <buffer> Hdb deriving (Bounded, Enum, Eq, Ord, Read, Show)
iabbrev <buffer> Hde deriving (Eq, Ord, Read, Show)
iabbrev <buffer> Hne newtype =<CR>deriving (Eq, Ord, Read, Show)<Up><End><Left><Left>
iabbrev <buffer> Hnt newtype =<CR>deriving (Eq, Ord, Read, Show)<Up><End><Left><Left>
iabbrev <buffer> HE Eq, Ord, Read, Show
iabbrev <buffer> HF Functor
iabbrev <buffer> HH hiding
iabbrev <buffer> HI import
iabbrev <buffer> HL LANGUAGE
iabbrev <buffer> Hl Language
iabbrev <buffer> HM Monad
iabbrev <buffer> HS System
iabbrev <buffer> HT Text
iabbrev <buffer> HQ qualified
iabbrev <buffer> HW where
" If the first two lines are imports, "gg/^import\ " will jump to the second
" line instead of the first. To get around this, we use \zs to start the match
" late, then move back to the beginning of the import.
nnoremap <silent> <buffer> <leader>il gg:execute "keepjumps normal! /^i\\zsmport\\ \rh"<cr>:nohlsearch<cr>
nnoremap <silent> <buffer> <leader>db :call AddDebugImport()<cr>
inoremap <silent> <buffer> <leader>db <c-o>:call AddDebugImport()<cr>
nnoremap <silent> <buffer> <leader>de :call DeleteDebugImport()<cr>
inoremap <silent> <buffer> <leader>de <c-o>:call DeleteDebugImport()<cr>
nnoremap <silent> <buffer> <leader>t: :Tabularize /\V\(\w\\|\s\\|\^\\|)\)\zs::\ze\(\w\\|\s\\|\$\\|(\)<CR>
nnoremap <silent> <buffer> <leader>t> :Tabularize /\V\(\w\\|\s\\|\^\\|)\)\zs->\ze\(\w\\|\s\\|\$\\|(\)<CR>
nnoremap <silent> <buffer> <leader>t< :Tabularize /\V\(\w\\|\s\\|\^\\|)\)\zs<-\ze\(\w\\|\s\\|\$\\|(\)<CR>
nnoremap <silent> <buffer> <leader>t= :Tabularize /\V\(\w\\|\s\\|\^\\|)\)\zs=\ze\(\w\\|\s\\|\$\\|(\)<CR>
nnoremap <silent> <buffer> <leader>t$ :Tabularize /\V\(\w\\|\s\\|^\\|)\)\zs$\ze\(\w\\|\s\\|\$\\|(\)<CR>
nnoremap <silent> <buffer> <leader>t# :Tabularize /\V\(\w\\|\s\\|\^\\|)\)\zs#-}\ze\(\w\\|\s\\|\$\\|(\)<CR>
nnoremap <silent> <buffer> <leader>ta :Tabularize /\<as\><CR>
nnoremap <silent> <buffer> <leader>ti :Tabularize /\v\C^\s*import\s+(qualified\s+)?\zs[A-Z]/l1l0<CR>
nnoremap <silent> <buffer> <leader>q :co .<CR>:s/\V\^import/import qualified/<CR>:s/\v^[^(]*\S\zs\s*\(.*$//e<CR>:s/\v\.([^.]*)(\.(Strict\|Lazy))?$/.\1\2 as \1/<CR>:s/\C[a-z]\ze[^ ]*$//eg<CR>:nohlsearch<CR>
let b:surround_45 = "{- \r -}"
compiler cabal-build
function! AdjustView(view, location, lines_added)
if a:location <= a:view["lnum"]
let a:view["lnum"] = a:view["lnum"] + a:lines_added
if a:view["topline"] > 1
let a:view["topline"] = a:view["topline"] + a:lines_added
endif
endif
endfunction
" TODO: does not work well with literate Haskell
function! AddLanguagePragma(pragma)
let s:orig_pos = winsaveview()
let s:insertion_command = "O"
" step 1: try to find a suitable place to add it
" current heuristic is to try these in order:
" * under the last existing LANGUAGE pragma
" * over the module declaration line
" * at the top of the file
try
keepjumps normal G$?\c\m{-# language\>
let s:insertion_command = "o"
catch /E486:/
try
" If the first thing in the file is "module", this won't match,
" but that's okay because then the fall-through behavior would
" choose the same position as if this did match.
keepjumps normal gg/\C\m^\s*module\>
catch /E486:/
keepjumps normal gg
endtry
endtry
" step 2: add it
keepjumps execute "normal" s:insertion_command . "{-# LANGUAGE" a:pragma "#-}"
let s:pragma_pos = winsaveview()
" step 3: format it, if the appropriate plugin is installed
try
if b:align_language_pragma
try
Tabularize /#-}
catch /E492:/
endtry
endif
catch /E121:/
endtry
" TODO: step 4, sort the pragmas (perhaps add a b:sort_language_pragma option)
" step 5: jump back to the original position, or one line later if we
" inserted the pragma before where the cursor was before
call AdjustView(s:orig_pos, s:pragma_pos["lnum"], 1)
keepjumps call winrestview(s:orig_pos)
endfunction
" TODO: does not work well with literate Haskell or funky module indentations
function! AddDebugImport()
let s:orig_pos = winsaveview()
let s:dline = 0
" step 1: try to find a suitable place to add it
" current heuristic is to try these in order:
" * below the last import
" * below the module declaration
" * below the last language pragma
" * at the top of the file
try
keepjumps normal G$?\C\m^\s*import\>
catch /E486:/
try
" If the first line has the module declaration and nowrapscan is
" set, gg/module will not match. To fix this, we use \zs to start
" the match at the 'o' of module.
keepjumps normal gg/\C\m^\s*m\zsodule\>
/\C\m\<where\>
catch /E486:/
try
keepjumps normal G$?\c\m{-# language\>
catch /E486:/
keepjumps normal gg
let s:dline = -1
endtry
endtry
endtry
" step 2: add it
let s:import_pos = winsaveview()
if s:dline == -1
let s:lines = ["-- TODO: delete","import Debug.Trace",""]
else
let s:lines = ["", "-- TODO: delete","import Debug.Trace"]
endif
call append(line('.') + s:dline, s:lines)
" step 3: jump back to the original position, or three lines later if we
" inserted the import before where the cursor was before
call AdjustView(s:orig_pos, s:import_pos["lnum"] + s:dline, len(s:lines))
keepjumps call winrestview(s:orig_pos)
endfunction
function! DeleteDebugImport()
let s:orig_pos = winsaveview()
keeppatterns 1,$s/\m^-- TODO: delete\nimport Debug.Trace\n\s*\n//eI
keeppatterns 1,$s/\m^\s*\n-- TODO: delete\nimport Debug.Trace$//eI
" TODO: compute the updated position properly. seems hard
call winrestview(s:orig_pos)
endfunction
" g:haskell_extension_abbreviations is auto-generated
" TODO: give mappings for the ambiguous ones to tell the user they're
" ambiguous (e.g. map <leader>ldf to a message telling them to either use
" <leader>ldfu or <leader>ldfo); no need to muck with the abbreviations,
" though, since nothing bad happens if you don't finish them properly
let g:haskell_extension_abbreviations = {
\ 'a': 'Arrows',
\ 'aat': 'AllowAmbiguousTypes',
\ 'adt': 'AutoDeriveTypeable',
\ 'bl': 'BinaryLiterals',
\ 'bp': 'BangPatterns',
\ 'caffi': 'CApiFFI',
\ 'ccm': 'ConstrainedClassMethods',
\ 'ck': 'ConstraintKinds',
\ 'cpp': 'CPP',
\ 'dac': 'DeriveAnyClass',
\ 'daite': 'DoAndIfThenElse',
\ 'dc': 'DatatypeContexts',
\ 'ddt': 'DeriveDataTypeable',
\ 'dfo': 'DeriveFoldable',
\ 'dfu': 'DeriveFunctor',
\ 'dg': 'DeriveGeneric',
\ 'dk': 'DataKinds',
\ 'dr': 'DoRec',
\ 'drf': 'DisambiguateRecordFields',
\ 'ds': 'DefaultSignatures',
\ 'dt': 'DeriveTraversable',
\ 'ec': 'EmptyCase',
\ 'edd': 'EmptyDataDecls',
\ 'edr': 'ExtendedDefaultRules',
\ 'efa': 'ExplicitForAll',
\ 'en': 'ExplicitNamespaces',
\ 'eq': 'ExistentialQuantification',
\ 'er': 'ExtensibleRecords',
\ 'fc': 'FlexibleContexts',
\ 'fd': 'FunctionalDependencies',
\ 'ffi': 'ForeignFunctionInterface',
\ 'fi': 'FlexibleInstances',
\ 'g': 'Generics',
\ 'gadt': 'GADTs',
\ 'gadts': 'GADTSyntax',
\ 'ghcfip': 'GHCForeignImportPrim',
\ 'gnd': 'GeneralizedNewtypeDeriving',
\ 'hd': 'HereDocuments',
\ 'iffi': 'InterruptibleFFI',
\ 'ii': 'IncoherentInstances',
\ 'ipa': 'ImplicitParams',
\ 'ipr': 'ImplicitPrelude',
\ 'is': 'InstanceSigs',
\ 'it': 'ImpredicativeTypes',
\ 'jsffi': 'JavaScriptFFI',
\ 'ks': 'KindSignatures',
\ 'lc': 'LambdaCase',
\ 'lts': 'LiberalTypeSynonyms',
\ 'mc': 'MonadComprehensions',
\ 'mh': 'MagicHash',
\ 'mlb': 'MonoLocalBinds',
\ 'mpb': 'MonoPatBinds',
\ 'mptc': 'MultiParamTypeClasses',
\ 'mr': 'MonomorphismRestriction',
\ 'mwi': 'MultiWayIf',
\ 'na': 'NoArrows',
\ 'naat': 'NoAllowAmbiguousTypes',
\ 'nadt': 'NoAutoDeriveTypeable',
\ 'nbl': 'NoBinaryLiterals',
\ 'nbp': 'NoBangPatterns',
\ 'ncaffi': 'NoCApiFFI',
\ 'nccm': 'NoConstrainedClassMethods',
\ 'nck': 'NoConstraintKinds',
\ 'ncpp': 'NoCPP',
\ 'nd': 'NumDecimals',
\ 'ndac': 'NoDeriveAnyClass',
\ 'ndaite': 'NoDoAndIfThenElse',
\ 'ndc': 'NoDatatypeContexts',
\ 'nddt': 'NoDeriveDataTypeable',
\ 'ndfo': 'NoDeriveFoldable',
\ 'ndfu': 'NoDeriveFunctor',
\ 'ndg': 'NoDeriveGeneric',
\ 'ndk': 'NoDataKinds',
\ 'ndr': 'NoDoRec',
\ 'ndrf': 'NoDisambiguateRecordFields',
\ 'nds': 'NoDefaultSignatures',
\ 'ndt': 'NoDeriveTraversable',
\ 'nec': 'NoEmptyCase',
\ 'nedd': 'NoEmptyDataDecls',
\ 'nedr': 'NoExtendedDefaultRules',
\ 'nefa': 'NoExplicitForAll',
\ 'nen': 'NoExplicitNamespaces',
\ 'neq': 'NoExistentialQuantification',
\ 'ner': 'NoExtensibleRecords',
\ 'nfc': 'NoFlexibleContexts',
\ 'nfd': 'NoFunctionalDependencies',
\ 'nffi': 'NoForeignFunctionInterface',
\ 'nfi': 'NoFlexibleInstances',
\ 'nfp': 'NamedFieldPuns',
\ 'ng': 'NoGenerics',
\ 'ngadt': 'NoGADTs',
\ 'ngadts': 'NoGADTSyntax',
\ 'nghcfip': 'NoGHCForeignImportPrim',
\ 'ngnd': 'NoGeneralizedNewtypeDeriving',
\ 'nhd': 'NoHereDocuments',
\ 'ni': 'NondecreasingIndentation',
\ 'niffi': 'NoInterruptibleFFI',
\ 'nii': 'NoIncoherentInstances',
\ 'nipa': 'NoImplicitParams',
\ 'nipr': 'NoImplicitPrelude',
\ 'nis': 'NoInstanceSigs',
\ 'nit': 'NoImpredicativeTypes',
\ 'njsffi': 'NoJavaScriptFFI',
\ 'nks': 'NoKindSignatures',
\ 'nl': 'NegativeLiterals',
\ 'nlc': 'NoLambdaCase',
\ 'nlts': 'NoLiberalTypeSynonyms',
\ 'nmc': 'NoMonadComprehensions',
\ 'nmh': 'NoMagicHash',
\ 'nmlb': 'NoMonoLocalBinds',
\ 'nmpb': 'NoMonoPatBinds',
\ 'nmptc': 'NoMultiParamTypeClasses',
\ 'nmr': 'NoMonomorphismRestriction',
\ 'nmwi': 'NoMultiWayIf',
\ 'nnd': 'NoNumDecimals',
\ 'nnfp': 'NoNamedFieldPuns',
\ 'nni': 'NoNondecreasingIndentation',
\ 'nnl': 'NoNegativeLiterals',
\ 'nnpkp': 'NoNPlusKPatterns',
\ 'nnqo': 'NoNewQualifiedOperators',
\ 'nntc': 'NoNullaryTypeClasses',
\ 'nnwc': 'NoNamedWildCards',
\ 'noi': 'NoOverlappingInstances',
\ 'nol': 'NoOverloadedLists',
\ 'nos': 'NoOverloadedStrings',
\ 'npa': 'NoParallelArrays',
\ 'npc': 'NoPolymorphicComponents',
\ 'npg': 'NoPatternGuards',
\ 'npi': 'NoPackageImports',
\ 'npk': 'NoPolyKinds',
\ 'npkp': 'NPlusKPatterns',
\ 'nplc': 'NoParallelListComp',
\ 'npo': 'NoPostfixOperators',
\ 'npsi': 'NoPatternSignatures',
\ 'npsy': 'NoPatternSynonyms',
\ 'npts': 'NoPartialTypeSignatures',
\ 'nqo': 'NewQualifiedOperators',
\ 'nqq': 'NoQuasiQuotes',
\ 'nr2t': 'NoRank2Types',
\ 'nra': 'NoRoleAnnotations',
\ 'nrd': 'NoRecursiveDo',
\ 'nrnt': 'NoRankNTypes',
\ 'nrpa': 'NoRegularPatterns',
\ 'nrpr': 'NoRelaxedPolyRec',
\ 'nrpu': 'NoRecordPuns',
\ 'nrs': 'NoRebindableSyntax',
\ 'nrts': 'NoRestrictedTypeSynonyms',
\ 'nrwc': 'NoRecordWildCards',
\ 'ns': 'NoSafe',
\ 'nsd': 'NoStandaloneDeriving',
\ 'nsi': 'NoSafeImports',
\ 'nstv': 'NoScopedTypeVariables',
\ 'nt': 'NoTrustworthy',
\ 'nta': 'NoTypeApplications',
\ 'ntc': 'NullaryTypeClasses',
\ 'ntf': 'NoTypeFamilies',
\ 'nth': 'NoTemplateHaskell',
\ 'ntlc': 'NoTransformListComp',
\ 'nto': 'NoTypeOperators',
\ 'ntrs': 'NoTraditionalRecordSyntax',
\ 'nts': 'NoTupleSections',
\ 'ntsi': 'NoTypeSynonymInstances',
\ 'nu': 'NoUnsafe',
\ 'nuffit': 'NoUnliftedFFITypes',
\ 'nui': 'NoUndecidableInstances',
\ 'nus': 'NoUnicodeSyntax',
\ 'nut': 'NoUnboxedTuples',
\ 'nvp': 'NoViewPatterns',
\ 'nwc': 'NamedWildCards',
\ 'nxs': 'NoXmlSyntax',
\ 'oi': 'OverlappingInstances',
\ 'ol': 'OverloadedLists',
\ 'os': 'OverloadedStrings',
\ 'pa': 'ParallelArrays',
\ 'pc': 'PolymorphicComponents',
\ 'pg': 'PatternGuards',
\ 'pi': 'PackageImports',
\ 'pk': 'PolyKinds',
\ 'plc': 'ParallelListComp',
\ 'po': 'PostfixOperators',
\ 'psi': 'PatternSignatures',
\ 'psy': 'PatternSynonyms',
\ 'pts': 'PartialTypeSignatures',
\ 'qq': 'QuasiQuotes',
\ 'r2t': 'Rank2Types',
\ 'ra': 'RoleAnnotations',
\ 'rd': 'RecursiveDo',
\ 'rnt': 'RankNTypes',
\ 'rpa': 'RegularPatterns',
\ 'rpr': 'RelaxedPolyRec',
\ 'rpu': 'RecordPuns',
\ 'rs': 'RebindableSyntax',
\ 'rts': 'RestrictedTypeSynonyms',
\ 'rwc': 'RecordWildCards',
\ 's': 'Safe',
\ 'sd': 'StandaloneDeriving',
\ 'si': 'SafeImports',
\ 'stv': 'ScopedTypeVariables',
\ 't': 'Trustworthy',
\ 'ta': 'TypeApplications',
\ 'tf': 'TypeFamilies',
\ 'th': 'TemplateHaskell',
\ 'tlc': 'TransformListComp',
\ 'to': 'TypeOperators',
\ 'trs': 'TraditionalRecordSyntax',
\ 'ts': 'TupleSections',
\ 'tsi': 'TypeSynonymInstances',
\ 'u': 'Unsafe',
\ 'uffit': 'UnliftedFFITypes',
\ 'ui': 'UndecidableInstances',
\ 'us': 'UnicodeSyntax',
\ 'ut': 'UnboxedTuples',
\ 'vp': 'ViewPatterns',
\ 'xs': 'XmlSyntax',
\}
for [s:abbr, s:ext] in items(g:haskell_extension_abbreviations)
exec "iabbrev <buffer> l" . s:abbr . "/ " . s:ext
exec "nnoremap <buffer> <leader>L" . s:abbr . " O{-# LANGUAGE " . s:ext . " #-}<ESC>"
exec "nnoremap <buffer> <leader>l" . s:abbr . " :silent call AddLanguagePragma('" . s:ext . "')<CR>"
endfor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment