Skip to content

Commit 7a56531

Browse files
committed
WIP Allow to automatically open attachments
This adds a proof of concept mailcap declaration. If we can lookup a command, use it as a preference otherwise show an input editor. Keep the possibility to open the attachment with a different command by binding the input editor to a different keystroke tho. This patch would also fix #174 since it is now possible to automatically invoke a program to see HTML output. Fixes #182 and #174
1 parent 0916fad commit 7a56531

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/Config/Main.hs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import qualified Graphics.Vty as V
1212
import qualified Data.ByteString as B
1313
import qualified Data.ByteString.Lazy as LB
1414
import qualified Data.Text as T
15+
import qualified Data.Map as Map
1516
import System.Environment (lookupEnv)
1617
import System.Directory (getHomeDirectory)
1718
import Data.Maybe (fromMaybe)
@@ -178,6 +179,7 @@ defaultConfig =
178179
, _mvMailListOfAttachmentsKeybindings = mailAttachmentsKeybindings
179180
, _mvOpenWithKeybindings = openWithKeybindings
180181
, _mvPipeToKeybindings = pipeToKeybindings
182+
, _mvMailcap = Map.fromList [(("text", "html"), "elinks -force-html")]
181183
}
182184
, _confIndexView = IndexViewSettings
183185
{ _ivBrowseThreadsKeybindings = browseThreadsKeybindings

src/Types.hs

+4
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ data MailViewSettings = MailViewSettings
342342
, _mvMailListOfAttachmentsKeybindings :: [Keybinding 'ViewMail 'MailListOfAttachments]
343343
, _mvOpenWithKeybindings :: [Keybinding 'ViewMail 'MailAttachmentOpenWithEditor]
344344
, _mvPipeToKeybindings :: [Keybinding 'ViewMail 'MailAttachmentPipeToEditor]
345+
, _mvMailcap :: Map.Map (CI.CI B.ByteString, CI.CI B.ByteString) String
345346
}
346347
deriving (Generic, NFData)
347348

@@ -369,6 +370,9 @@ mvOpenWithKeybindings = lens _mvOpenWithKeybindings (\s x -> s { _mvOpenWithKeyb
369370
mvPipeToKeybindings :: Lens' MailViewSettings [Keybinding 'ViewMail 'MailAttachmentPipeToEditor]
370371
mvPipeToKeybindings = lens _mvPipeToKeybindings (\s x -> s { _mvPipeToKeybindings = x })
371372

373+
mvMailcap :: Lens' MailViewSettings (Map.Map (CI.CI B.ByteString, CI.CI B.ByteString) String)
374+
mvMailcap = lens _mvMailcap (\s x -> s { _mvMailcap = x })
375+
372376
data ViewName
373377
= Threads
374378
| Mails

src/UI/Actions.hs

+20-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module UI.Actions (
2828
Scrollable(..)
2929
, quit
3030
, focus
31+
, foo
3132
, done
3233
, abort
3334
, noop
@@ -112,8 +113,9 @@ import Data.MIME
112113
contentDisposition, dispositionType, headers, filename,
113114
parseContentType, attachments, isAttachment, entities,
114115
matchContentType, contentType, mailboxList, renderMailboxes,
115-
addressList, renderAddresses, renderRFC5422Date, MIMEMessage,
116-
WireEntity, DispositionType(..), ContentType(..), Mailbox(..))
116+
addressList, renderAddresses, renderRFC5422Date, ctType, ctSubtype,
117+
MIMEMessage, WireEntity, DispositionType(..), ContentType(..),
118+
Mailbox(..))
117119
import qualified Storage.Notmuch as Notmuch
118120
import Storage.ParsedMail
119121
(parseMail, getTo, getFrom, getSubject, toQuotedMail, entityToBytes)
@@ -516,6 +518,22 @@ invokeEditor = Action ["invoke external editor"] (Brick.suspendAndResume . liftI
516518
edit :: Action 'ComposeView 'ComposeListOfAttachments (T.Next AppState)
517519
edit = Action ["edit file"] (Brick.suspendAndResume . liftIO . editAttachment)
518520

521+
foo :: Action 'ViewMail ctx (T.Next AppState)
522+
foo =
523+
Action
524+
{ _aDescription = ["pass to external command"]
525+
, _aAction = \s -> let
526+
contenttype = preview (asMailView . mvAttachments . to L.listSelectedElement . _Just . _2 . headers . contentType) s
527+
in case contenttype of
528+
Nothing -> Brick.continue $ setError (GenericError "Couldn't determine content type") s
529+
(Just ct) -> let maybeCommand = preview (asConfig . confMailView . mvMailcap . ix (view ctType ct, view ctSubtype ct)) s
530+
in case maybeCommand of
531+
(Just cmd) -> Brick.suspendAndResume $ liftIO $ openCommand' s cmd
532+
Nothing -> Brick.continue
533+
$ s & set (asViews . vsViews . at ViewMail . _Just . vFocus) MailAttachmentOpenWithEditor
534+
. set (asViews . vsViews . at ViewMail . _Just . vWidgets . ix MailAttachmentOpenWithEditor . veState) Visible
535+
}
536+
519537
openWithCommand :: Action 'ViewMail 'MailAttachmentOpenWithEditor (T.Next AppState)
520538
openWithCommand =
521539
Action

src/UI/Mail/Keybindings.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ mailAttachmentsKeybindings =
4141
[ Keybinding (V.EvKey (V.KChar 'j') []) (listDown `chain` continue)
4242
, Keybinding (V.EvKey (V.KChar 'k') []) (listUp `chain` continue)
4343
, Keybinding (V.EvKey (V.KChar 'q') []) (abort `chain'` (focus :: Action 'ViewMail 'ScrollingMailView AppState) `chain` continue)
44-
, Keybinding (V.EvKey V.KEnter []) (noop `chain'` (focus :: Action 'ViewMail 'MailAttachmentOpenWithEditor AppState) `chain` continue)
44+
, Keybinding (V.EvKey V.KEnter []) foo
45+
, Keybinding (V.EvKey (V.KChar 'o') []) (noop `chain'` (focus :: Action 'ViewMail 'MailAttachmentOpenWithEditor AppState) `chain` continue)
4546
, Keybinding (V.EvKey (V.KChar '|') []) (noop `chain'` (focus :: Action 'ViewMail 'MailAttachmentPipeToEditor AppState) `chain` continue)
4647
]
4748

0 commit comments

Comments
 (0)