aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Utility/Emacs.hs
blob: 27f08303d23aea0b536e2d7916e7fb773a00de19 (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
module Utility.Emacs ( getEmacsOutput
                     , parseEmacsOutput) where

import           Control.Applicative ((<$>))
import           Control.Monad       (when, foldM)
import           Data.Maybe.Read
import           System.Directory    (getHomeDirectory)
import           System.Process      (readProcess)
import           Text.Regex.Posix    ((=~))
import           Types.Reminder

parseEmacsOutput :: String -> [Reminder]
parseEmacsOutput = foldr step [] . drop 2 . lines
  where
    step line rems = maybe rems (++ rems) $ parseLine line

parseLine              :: String -> Maybe [Reminder]
parseLine line         = do
    when (not lineMatch) $ fail ""
    let hStr:mStr:s:[] = drop 1 . concat $ lineMatchStrings
    h <- readMaybe hStr
    m <- readMaybe mStr
    staggeredReminders =<< makeReminder h m s
  where
    apptRegexp         = " ([0-9]{1,2}):([0-9][0-9])[-]{0,1}[0-9:]{0,5}[.]* (.*)$"
    snipRegexp         = "[ ]{2,}:.*:*$"

    lineMatch        = line =~ apptRegexp :: Bool
    lineMatchStrings = line =~ apptRegexp :: [[String]]

staggeredReminders   :: Reminder -> Maybe [Reminder]
staggeredReminders r = undefined

-- staggeredReminders :: Int -> Int -> String -> [Reminder]
-- staggeredReminders hour mins text = foldl' step [] [60, 15, 0]
--   where step rems diff = let hour'
--                                | diff > mins = hour - 1
--                                | otherwise = hour
--                              -- could do with addition mod 60
--                              mins'
--                                | mins >= diff = mins - diff
--                                | otherwise = 60 + (mins - diff)
--                          in rems ++ [Reminder hour' mins' text]

getEmacsOutput :: IO String
getEmacsOutput = do
    args <- makeEmacsArgs <$> getHomeDirectory
    readProcess "emacs" args ""

makeEmacsArgs      :: String -> [String]
makeEmacsArgs home = [ "-batch"
                     , "-l", home ++ "/.emacs.d/init.el"
                     , "-eval", "(setq org-agenda-sticky nil)"
                     , "-eval", "(org-batch-agenda \"D\")" ]