Colorizing messages in mutt (stab 2)

This image shows the different scores (“[1000]” scores red background, “[800]” magenta text, etc.). At work I use a slightly different set.

I think I got it, at least in the index (for some reason it doesn’t work from the pager, maybe it’s the source command there)! I’m able to tag a message with a score, and have that score translate into a different color. Here’s the relevant part of my .muttrc:

# Star scoring 
source ~/.mutt/stars
bind index,pager s noop # I use 'y' to archive messages, no need for 's' to be bound to <save-message>.  This frees it up so I can use it below.

set my_red_star = 1000
set my_magenta_question = 800
set my_yellow_bang = 600
set my_green_star = 400
set my_green_check = 200
set my_blue_info = 100
set my_del_star = 0

macro index,pager sr "<pipe-entry>~/bin/stars $my_red_star<enter><enter-command>source ~/.muttrc<enter>" "Mark the current message with RED"
macro index,pager sm "<pipe-entry>~/bin/stars $my_magenta_question<enter><enter-command>source ~/.muttrc<enter>" "Mark the current message with MAGENTA"
macro index,pager sy "<pipe-entry>~/bin/stars $my_yellow_bang<enter><enter-command>source ~/.muttrc<enter>" "Mark the current message with YELLOW"
macro index,pager sc "<pipe-entry>~/bin/stars $my_green_check<enter><enter-command>source ~/.muttrc<enter>" "Mark the current message with GREEN"
macro index,pager si "<pipe-entry>~/bin/stars $my_blue_info<enter><enter-command>source ~/.muttrc<enter>" "Mark the current message with BLUE"
macro index,pager sg "<pipe-entry>~/bin/stars $my_green_check<enter><enter-command>source ~/.muttrc<enter>" "Mark the current message with GREEN"
macro index,pager sd "<pipe-entry>~/bin/stars $my_del_star<enter><pipe-entry>~/bin/del_stars<enter><enter-command>source ~/.muttrc<enter><sync-mailbox>" "Remove color marking from the current message"

color index default brightred '~n 1000-1100'  # Mark the message with red!
color index magenta default '~n 800-999'  # Mark the message with magenta!
color index black brightyellow '~n 600-799'  # Mark the message with yellow!
color index default green '~n 400-599'  # Mark the message with green!
color index green default '~n 200-399'  # Mark the message with green (check)!
color index brightblue default '~n 100-199'  # Mark the message with blue!

Not much different than I had before. The problems I had were the .mutt/stars, bin/stars, bin/del_stars. It mostly worked once I got it set up, except for messages that had special regular-expression reserved characters in the message ID. Here’s the bin/stars file I have:

#!/bin/bash
SCORE=$1                                
msgid=$(sed '/^Message-I[Dd]:[[:space:]]*/!d; s///; s/[<>]//g; s/[][$|]/\\&/g; q')
echo "score \"~i '$msgid'\" $SCORE" >> ~/.mutt/stars 

The key was the single quotes around the actual message ID. And the del_stars file:

#!/bin/bash

msgid=$(grep -m 1 '^Message-I[Dd]' | awk '{print $2}' | sed 's/[<>]//g; s/[^^]/[&]/g; s/\^/\\^/g')
sed -i "/$msgid/d" ~/.mutt/stars

After some further testing, it still doesn’t work. I have one company that sends me a bill summary, and the Message-Id has square-brackets around an RFC1918 address. Rather than fight with mutt trying to get it to read and score the message properly, I just added the company’s bill email address to my .mutt/stars file, with the following line:

score "~f CompanyEmail@company.com" 600

Also, to get the del_star script to work most times I have to close mutt completely before the score is removed. But otherwise it works!