aboutsummaryrefslogtreecommitdiff
path: root/src/Lazymail/Keymap.hs
blob: 9bb2aaa01f139e014b8965ce792f384362f46c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{- Lazymail default keymap.
 -
 - Copyright 2013 Raúl Benencia <rul@kalgan.cc>
 -
 - Licensed under the GNU GPL version 3 or higher
 -}

module Lazymail.Keymap
       ( defaultGlobalKeymap
       , defaultMaildirKeymap
       , defaultIndexKeymap
       , defaultEmailKeymap
       , defaultComposeKeymap
       , findHandler
       ) where

import UI.NCurses(Event(..), Key(..))

import Lazymail.Types(Keymap, LazymailState(..), Mode(..), LazymailConfig(..))
import Lazymail.Handlers( advanceMode, previousMode, scrollUp, scrollDown
                        , toComposeMode, getFrom, getTo, getSubject, getCc
                        , getBcc, getReplyTo, editEmail, sendEmail
                        )

defaultGlobalKeymap = [ ([EventCharacter '\n', EventCharacter ' ', EventSpecialKey KeyRightArrow], advanceMode)
                      , ([EventCharacter 'q', EventCharacter 'Q'], previousMode)
                      , ([EventSpecialKey KeyUpArrow, EventCharacter 'k'], scrollUp)
                      , ([EventSpecialKey KeyDownArrow, EventCharacter 'j'], scrollDown)
                      , ([EventCharacter 'm'], toComposeMode)
                      ]
defaultMaildirKeymap = []
defaultIndexKeymap   = []
defaultEmailKeymap   = []
defaultComposeKeymap = [ ([EventCharacter 'f'], getFrom)
                       , ([EventCharacter 't'], getTo)
                       , ([EventCharacter 's'], getSubject)
                       , ([EventCharacter 'c'], getCc)
                       , ([EventCharacter 'b'], getBcc)
                       , ([EventCharacter 'r'], getReplyTo)
                       , ([EventCharacter 'e'], editEmail)
                       , ([EventCharacter 'y'], sendEmail)
                       ]

-- | Try to find a keymap for the current mode. If nothing is found, then
--   try looking up in the global keymap.
findHandler st cfg ev = case modeHandler (mode st) ev of
  Nothing  -> globalHandler ev
  h@Just{} -> h
  where
    modeHandler MaildirMode = lookupHandler $ maildirModeKeymap cfg
    modeHandler IndexMode   = lookupHandler $ indexModeKeymap  cfg
    modeHandler EmailMode   = lookupHandler $ emailModeKeymap   cfg
    modeHandler ComposeMode = lookupHandler $ composeModeKeymap cfg

    globalHandler = lookupHandler $ globalKeymap cfg

    lookupHandler [] _   = Nothing
    lookupHandler (km:kms) ev
      | elem ev (fst km) = Just $ snd km
      | otherwise        = lookupHandler kms ev
nihil fit ex nihilo