# vim: filetype=neomuttrc
#source "~/.config/mutt/accounts/protonmail"


# This file should include
# imap_user, imap_pass, realname and from
source ~/.config/mutt/work-email.secret

# Using davmail to interface with exchange
#set folder      = "imap://127.0.0.1:1143/"
#set spoolfile   = +INBOX
#mailboxes       = +INBOX


########################
#  Email Folder Setup  #
########################

# Folder with emails
set folder = "$HOME/.mail/work" # https://neomutt.org/guide/reference#folder
# Type of mailbox
set mbox_type = Maildir # https://neomutt.org/guide/reference#mbox_type
# Directory to poll for new mail
set spoolfile = +Inbox # https://neomutt.org/guide/reference#spoolfile
# Directory to save sent messages into
set record = +Sent # https://neomutt.org/guide/reference#record
# Sets the drafts folder
set postponed = +Drafts # https://neomutt.org/guide/reference#postponed
# File that headers will be cached
set header_cache = ~/.cache/mutt # https://neomutt.org/guide/reference#header_cache
# Find all mailboxes in the mail folder
mailboxes +Sent +Drafts +Junk +Trash +Projects `find $HOME/.mail/work/Projects -mindepth 1 -not \( -name 'tmp' -o -name 'new' -o -name 'cur' \) -type d -printf "+'Projects/%P' "`

######################
#  NotMuch Settings  #
######################
# All the notmuch settings are documented here: https://neomutt.org/feature/notmuch

# Points to the notmuch directory
set nm_default_url = "notmuch://$HOME/.mail/work"
# Makes notmuch return threads rather than messages
set nm_query_type = "threads"
# Set a virtual impox for Inbox, this will mean (with the above option) that threads in the inbox will show emails I sent if in reply to an email
virtual-mailboxes "My INBOX" "notmuch://?query=tag:inbox"
# Makes virtual mailbox open at the start ( I think )
set virtual_spoolfile = yes
# Binding for notmuch search
bind index \\ vfolder-from-query

####################
#  Sending Emails  #
####################

# Use davmail for sending emails
#set smtp_url="smtp://$imap_user:$imap_pass@127.0.0.1:1025" # https://neomutt.org/guide/reference#smtp-url
# Use my msmtp / markdown wrapper script to send emails using the work account
set sendmail = "/home/jonathan/.bin/emails/send-from-mutt work"

#########################
#  Index View Settings  #
#########################

# Sort by threads
set sort = threads # https://neomutt.org/guide/reference#sort
# Sort threads by last date recieved - newest first
set sort_aux = reverse-last-date-received # https://neomutt.org/guide/reference#sort_aux
# Show date in year/month/day hour:minute format
set date_format="%y/%m/%d %I:%M%p" # https://neomutt.org/guide/reference#date_format
# Vim like keybindings
bind index G last-entry
bind index g noop
bind index gg first-entry
# Reply bindings
bind index r reply
bind index R group-reply
# Collapsing / Uncollapsing threads
bind index c collapse-thread
bind index C collapse-all
# Search back
bind index N search-opposite
# Easily copy / move messages
macro index C "<copy-message>?<toggle-mailboxes>" "copy a message to a mailbox"
macro index M "<save-message>?<toggle-mailboxes>" "move a message to a mailbox"

macro index o "<shell-escape>check-emails inbox<enter>" "Check Emails"
macro index O "<shell-escape>check-emails<enter>" "Check Emails"
######################
#  Sidebar Settings  #
######################

set sidebar_divider_char = '│'
set sidebar_delim_chars = "/"
set sidebar_short_path
set sidebar_folder_indent
set sidebar_visible = yes
set sidebar_width = 24
set sidebar_format = '%B%?F? [%F]?%* %?N?%N/? %?S?%S?'
bind index,pager \Cj sidebar-next
bind index,pager \Ck sidebar-prev
bind index,pager \Co sidebar-open
bind index,pager \Cb sidebar-toggle-visible


########################
#  Composing Settings  #
########################

# Use nvim but don't force text width (looks terible if read on a phone)
set editor = "nvim +':set textwidth=0'" # https://neomutt.org/guide/reference#editor
# Puts email headers in Vim
set edit_headers=yes # https://neomutt.org/guide/reference#edit_headers
# Go straight to vim when writing an email
# Means that we will not be prompted for to / subject before opening email
set autoedit=yes # https://neomutt.org/guide/reference#autoedit
# Use From header
set use_from = yes # https://neomutt.org/guide/reference#use_from
# When forwading, put Fwd: before original subject
set forward_format = "Fwd: %s" # https://neomutt.org/guide/reference#forward_format
# Include the forwarded message in the body
set forward_quote = yes # https://neomutt.org/guide/reference#forward_quote
# Include the replied mesaage in the body
set include = yes # https://neomutt.org/guide/reference#include
# Adds a header that is used to determine whether my send script should convert the markdown to html
my_hdr Markdown: false

######################
#  Viewing settings  #
######################

# Don't show + on wrapped lines
set markers=no # https://neomutt.org/guide/reference#markers
# Wrap lines at 78 chars
set wrap=78 # https://neomutt.org/guide/reference#wrap
# When at the end, don't go to next email when trying to scroll
set pager_stop=yes # https://neomutt.org/guide/reference#pager_stop
# Mailcap file is used to tell mutt how to open different types of file
set mailcap_path = "~/.config/mutt/mailcap" # https://neomutt.org/guide/mimesupport#3-%C2%A0mime-viewer-configuration-with-mailcap
# Tells Mutt to automatically view files with these mime types
auto_view text/html # https://neomutt.org/guide/reference#auto_view
auto_view application/pgp-encrypted # https://neomutt.org/guide/reference#auto_view
auto_view text/calendar # https://neomutt.org/guide/reference#auto_view
# Order to try and show multipart emails
alternative_order text/plain text/calendar text/enriched text/html
# Macro to toggle alternates (plain <-> html), based on:
# https://groups.google.com/d/msg/comp.mail.mutt/9I702oMwQQE/JqdLnp3j9WAJ
macro pager ,@aoh= "<enter-command>unalternative_order *; alternative_order text/calendar text/html text/plain text/enriched; macro pager A ,@aot= 'toggle alternative order'<enter><exit><display-message>"
macro pager ,@aot= "<enter-command>unalternative_order *; alternative_order text/plain text/calendar text/enriched text/html; macro pager A ,@aoh= 'toggle alternative order'<enter><exit><display-message>"
macro pager A ,@aoh= "toggle alternative order"
# Use urlscan to handle links in messages
macro pager U "<enter-command>set pipe_decode = yes<enter><pipe-message>urlscan  -dc -r 'linkhandler {}'<enter><enter-command>set pipe_decode = no<enter>" "view URLs"
bind pager r reply
bind pager R group-reply
# Vim like keybindings
bind pager g noop
bind pager G bottom
bind pager gg top
bind pager j next-line
bind pager k previous-line

##############
#  Contacts  #
##############

set query_command= "abook --mutt-query '%s'"
macro index,pager  a "<pipe-message>abook --add-email-quiet<return>" "Add this sender to Abook"

###################
#  Misc Settings  #
###################

# Don't ask to push enter after external command is run
set wait_key = no # https://neomutt.org/guide/reference#wait_key
# Send a notification when a new email arrives
# set new_mail_command = "notify-send 'New Email'" # https://neomutt.org/guide/reference#new_mail_command
# Makes escape key cancel inputs. Normally it's ctrl+G (weird)
set abort_key = "<Esc>" # https://neomutt.org/guide/reference#abort_key
# My Name
set realname = "Jonathan Hodgson"

#############
#  Colours  #
#############

# Default index colors:
color index yellow default '.*'
color index_author red default '.*'
color index_number blue default
color index_subject cyan default '.*'

# New mail is boldened:
color index brightyellow black "~N"
color index_author brightred black "~N"
color index_subject brightcyan black "~N"

# Other colors and aesthetic settings:
mono bold bold
mono underline underline
mono indicator reverse
mono error bold
color normal default default
color indicator brightblack white
color sidebar_highlight red default
color sidebar_divider brightblack black
color sidebar_flagged red black
color sidebar_new green black
color normal brightyellow default
color error red default
color tilde black default
color message cyan default
color markers red white
color attachment white default
color search brightmagenta default
color status brightyellow black
color hdrdefault brightgreen default
color quoted green default
color quoted1 blue default
color quoted2 cyan default
color quoted3 yellow default
color quoted4 red default
color quoted5 brightred default
color signature brightgreen default
color bold black default
color underline black default
color normal default default

# Regex highlighting:
color header blue default ".*"
color header brightmagenta default "^(From)"
color header brightcyan default "^(Subject)"
color header brightwhite default "^(CC|BCC)"
color body brightred default "[\-\.+_a-zA-Z0-9]+@[\-\.a-zA-Z0-9]+" # Email addresses
color body brightblue default "(https?|ftp)://[\-\.,/%~_:?&=\#a-zA-Z0-9]+" # URL
color body green default "\`[^\`]*\`" # Green text between ` and `
color body brightblue default "^# \.*" # Headings as bold blue
color body brightcyan default "^## \.*" # Subheadings as bold cyan
color body brightgreen default "^### \.*" # Subsubheadings as bold green
color body yellow default "^(\t| )*(-|\\*) \.*" # List items as yellow
color body brightcyan default "[;:][-o][)/(|]" # emoticons
color body brightcyan default "[;:][)(|]" # emoticons
color body brightcyan default "[ ][*][^*]*[*][ ]?" # more emoticon?
color body brightcyan default "[ ]?[*][^*]*[*][ ]" # more emoticon?
color body red default "(BAD signature)"
color body cyan default "(Good signature)"
color body brightblack default "^gpg: Good signature .*"
color body brightyellow default "^gpg: "
color body brightyellow red "^gpg: BAD signature from.*"
mono body bold "^gpg: Good signature"
mono body bold "^gpg: BAD signature from.*"
color body red default "([a-z][a-z0-9+-]*://(((([a-z0-9_.!~*'();:&=+$,-]|%[0-9a-f][0-9a-f])*@)?((([a-z0-9]([a-z0-9-]*[a-z0-9])?)\\.)*([a-z]([a-z0-9-]*[a-z0-9])?)\\.?|[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)(:[0-9]+)?)|([a-z0-9_.!~*'()$,;:@&=+-]|%[0-9a-f][0-9a-f])+)(/([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*(;([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*)*(/([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*(;([a-z0-9_.!~*'():@&=+$,-]|%[0-9a-f][0-9a-f])*)*)*)?(\\?([a-z0-9_.!~*'();/?:@&=+$,-]|%[0-9a-f][0-9a-f])*)?(#([a-z0-9_.!~*'();/?:@&=+$,-]|%[0-9a-f][0-9a-f])*)?|(www|ftp)\\.(([a-z0-9]([a-z0-9-]*[a-z0-9])?)\\.)*([a-z]([a-z0-9-]*[a-z0-9])?)\\.?(:[0-9]+)?(/([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*(;([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*)*(/([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*(;([-a-z0-9_.!~*'():@&=+$,]|%[0-9a-f][0-9a-f])*)*)*)?(\\?([-a-z0-9_.!~*'();/?:@&=+$,]|%[0-9a-f][0-9a-f])*)?(#([-a-z0-9_.!~*'();/?:@&=+$,]|%[0-9a-f][0-9a-f])*)?)[^].,:;!)? \t\r\n<>\"]"
