# vim: filetype=muttrc
# Based on https://github.com/Nelyah/dotfiles

set editor=$EDITOR
set my_name = "Damien Broqua"
set realname = "Damien Broqua"
set imap_check_subscribed

# main options
set envelope_from

# Paths ----------------------------------------------
#set folder           = ~/.mail               # mailbox location
#set alias_file       = ~/.config/neomutt/alias         # where to store aliases
set header_cache     = ~/.neomutt/cache/headers # where to store headers
set message_cachedir = ~/.neomutt/cache/bodies  # where to store bodies
set certificate_file = ~/.neomutt/certificates  # where to store certs
set mailcap_path     = ~/.config/neomutt/mailcap       # entries for filetypes
set tmpdir           = ~/.neomutt/temp          # where to keep temp files

# Basic Options --------------------------------------
set wait_key = no        # shut up, mutt
set mbox_type = Maildir  # mailbox type
set timeout = 600        # idle time before scanning
set mail_check = 1800    # minimum time between scans
set mail_check_stats
unset move               # gmail does that
set delete               # don't ask, just do
unset confirmappend      # don't ask, just do!
set quit                 # don't ask, just do!!
unset mark_old           # read/new is good enough for me
set beep_new             # bell on new mails
set pipe_decode          # strip headers and eval mimes when piping
set thorough_search      # strip headers and eval mimes before searching

# sort/threading
set sort     = threads
set sort_aux = reverse-last-date-received
set sort_re

# look and feel
set pager_index_lines = 8
set pager_context     = 5
set pager_stop
set menu_scroll
set smart_wrap
set tilde
unset markers

# composing
set fcc_attach
unset mime_forward
set forward_format = "Fwd: %s"
set include
set forward_quote

set smileys="(>From )|(:[-^]?[][)(><|/DPp])"

# Prevent on attachment
set abort_noattach='ask-yes'
set attach_keyword="\\<[Cc]i-joint\\>|\\<[Ff]ichier joint\\>|\\<[Pp]ièce jointe\\>"

# Headers infos
ignore *                               # first, ignore all headers
unignore from: to: cc: date: subject:  # then, show only these
hdr_order from: to: cc: date: subject: # and in this order<Paste>

# Navigation
bind attach,index,pager \CD next-page
bind attach,index,pager \CB previous-page
bind index D purge-message
bind index,pager \Cb half-up
bind index,pager \Cd half-down
bind index,pager,attach g noop
bind index,pager,attach r group-reply
bind index,pager,attach \CR reply
#bind pager gg top
#bind pager G bottom
bind attach,index G last-entry
bind attach,index gg first-entry
bind attach,pager j next-line
bind attach,pager k previous-line
bind attach,pager J next-entry
bind attach,pager K previous-entry
#set query_command= "khard email --parsable --search-in-source-files '%s'"
#set query_command = "( khard email --parsable '%s' | sed -n '1!p'; mu cfind --format=mutt-ab '%s' )"
set query_command= "khard email --parsable %s"
bind editor <Tab> complete-query
bind editor ^T    complete
set edit_headers = yes

set sleep_time = 0
set autoedit
set edit_headers

# Allow Mutt to open a new IMAP connection automatically.
unset imap_passive

# Keep the IMAP connection alive by polling intermittently (time in seconds).
set imap_keepalive = 300

#auto_view text/html text/calendar application/ics # view html automatically
auto_view text/html

alternative_order text/enriched text/plain text/html text/*

#macro attach \CE "<pipe-message>khal import<enter>"


# Macros for switching accounts
macro index <f2> '<sync-mailbox><enter-command>source ~/.config/neomutt/accounts/darkou<enter><change-folder>!<enter>'
macro index <f3> '<sync-mailbox><enter-command>source ~/.config/neomutt/accounts/mousur<enter><change-folder>!<enter>'

# use tab to switch between accounts, inspired by
#  http://msmtp.sourceforge.net/doc/msmtp.html#Using-msmtp-with-Mutt
macro generic \Cx| "<enter-command>source"
macro generic \Cx& "<enter-command>macro index \\t \"\\Cx"
macro index <tab> "\Cx0"    # default change to account 1, this will be redefined once pressed
macro generic \Cx0 "\Cx| ~/.mutt/accounts/darkou\"\n\Cx&1\"<enter><change-folder>!<enter>"
macro generic \Cx1 "\Cx| ~/.mutt/accounts/mousur\"\n\Cx&0\"<enter><change-folder>!<enter>"


set index_format="%3C %Z %[!%m.%d.%y] %-19.19n %?X?(%1X)& ? %?M?(%02M)& ? %s%> %?y?[%Y]?"
set index_format="%Z %3C %{%b %d} %-17.17n (%5c) %s"
set index_format="%3C %Z %{%b %d} %-19.19n (%5c) %?X?(%1X)& ? %?M?(%02M)& ? %s%> %?y?[%Y]?"
#macro index L "<enter-command>unset wait_key<enter><shell-escape>read -p 'mu query: ' x; echo \$x >~/.mu/search_term<enter><shell-escape>mu find --format=links --linksdir=~/.mail/search/ --clearlinks \$(cat ~/.mu/search_term)<enter><enter-command>set wait_key<enter>" "show only messages matching a notmuch pattern"

#macro index gf "<change-folder>~/.mail/search<enter>" "mu find results"
#macro index \cl "<enter-command>unset wait_key<enter><shell-escape>mu find --format=links --linksdir=~/testmail/ --clearlinks \$(cat ~/.mu/search_term)<enter><enter-command>set wait_key<enter>" "mu find results"

#macro index <F8> \
#    "<enter-command>unset wait_key<enter><shell-escape>~/bin/mutt-notmuch --prompt search<enter><change-folder-readonly>~/.cache/mutt_results<enter>" \
#    "search mail (using notmuch)"
#macro index <F9> \
#    "<enter-command>unset wait_key<enter><pipe-message>~/bin/mutt-notmuch thread<enter><change-folder-readonly>~/.cache/mutt_results<enter><enter-command>set wait_key<enter>" \
#    "search and reconstruct owning thread (using notmuch)"

#macro index,pager \cu "<pipe-message> urlscan<Enter>" "call urlscan to extract URLs out of a message"
#macro attach,compose \cu "<pipe-entry> urlscan<Enter>" "call urlscan to extract URLs out of a message"

# Sync all email
# If you want to make everything indexable after syncing email, use the office-alias
#macro index,pager P "<shell-escape>mbsync -a<enter>" "run mbsync to sync all mail"
macro index zc "<collapse-thread>" "collapse thread"
macro index zo "<collapse-thread>" "collapse thread"
