Skip to content

Instantly share code, notes, and snippets.

@s5unty
Created September 11, 2011 13:47
Show Gist options
  • Save s5unty/1209602 to your computer and use it in GitHub Desktop.
Save s5unty/1209602 to your computer and use it in GitHub Desktop.
wrapper around [mu|recoll], the [mail|file] indexing/searching utility
#! /bin/sh -
# inspiration from: http://www.zsh.org/mla/users/2007/msg01187.html
# require a POSIX sh, on those systems where the POSIX sh is not in /bin
# (like Solaris), you may need to adapt the shebang line above
# (/usr/xpg4/bin/sh on Solaris). You also need a terminfo aware "tput",
# ncurses one (the default on most systems) will do.
# wrapper around mu, the mail indexing/searching utility for mutt.
# in your ~/.muttrc:
# macro generic S "<enter-command>set my_cmd = \`mutt-mu\`<return><enter-command>push \$my_cmd<return>"
# we're not using <shell-escape> because we want to prompt the user in
# mutt's prompt area and still have mutt's index visible.
set -f
# restore stdin/stdout to the terminal, fd 3 goes to mutt's backticks.
exec < /dev/tty 3>&1 > /dev/tty
# save tty settings before modifying them
saved_tty_settings=$(stty -g)
trap '
printf "\r"; tput ed; tput rc
printf "<refresh>" >&3
stty "$saved_tty_settings"
exit
' INT TERM
# retrieve the size of the screen.
set $(stty size)
# save cursor position:
tput sc
# go to last line of the screen
tput cup "$1" 0
# Clear.
tput ed
mu_base=~/.mu
cmdhist=$mu_base/cmdhist
lastcmd=
print $lastcmd
exit 0
# run zsh in interactive mode (for history to work and .zshrc to
# be read).
search=$(
cmdhist=$cmdhist ZDOTDIR=$mu_base zsh -ic '
stty sane
bindkey -e
send-break() {
print "<BREAK>"
kill -HUP $$
}
accept-line() {
print -r -- "$BUFFER"
print -rs -- "$BUFFER"
fc -P
kill -HUP $$
}
zle -N accept-line
zle -N send-break
fc -p -a "$cmdhist" 100
a=; vared -p "搜寻(所有):" -eh a'
)
# mu result folder in mutt folder syntax:
MAILDIR=/sun/maildir
# 用每次搜索的关键字作一个虚拟邮箱?就像 sup 那样?
# MFOLDER=$(echo "$search" | sed 's/\s*\(~\w\)\s\(.\+\)/\1:\2/g')
MFOLDER=bingo
export MAILDIR
export MFOLDER
case $search in
("<BREAK>") ;;
("")
# rebuild the index
args="index --autoupgrade"
cmd="<refresh>"
;;
("~F")
args="find 'g:f' --format=links --clearlinks --linksdir=\"$MAILDIR/$MFOLDER\""
cmd="<refresh><change-folder-readonly>=$MFOLDER<return>"
;;
("~Q")
args="find 'g:f' --format=links --clearlinks --linksdir=\"$MAILDIR/$MFOLDER\""
cmd="<refresh><change-folder-readonly>=$MFOLDER<return>"
;;
(~f*)
args=$(echo "$search" | sed 's/\s*~f\s\+//g' | sed 's/|/\n/g' | awk '\
{
if (NR==1) {
str = sprintf("f:%s*", $1)
} else {
str = sprintf("%s OR f:%s*", str, $1)
}
}
END {
if (NR!=0) {
printf("find \"%s\" --format=links --clearlinks --linksdir=\"%s/%s\"", str, ENVIRON["MAILDIR"], ENVIRON["MFOLDER"])
}
}
')
cmd="<refresh><change-folder-readonly>=$MFOLDER<return>"
;;
(~t*)
args=$(echo "$search" | sed 's/\s*~t\s\+//g' | sed 's/|/\n/g' | awk '\
{
if (NR==1) {
str = sprintf("t:%s*", $1)
} else {
str = sprintf("%s OR t:%s*", str, $1)
}
}
END {
if (NR!=0) {
printf("find \"%s\" --format=links --clearlinks --linksdir=\"%s/%s\"", str, ENVIRON["MAILDIR"], ENVIRON["MFOLDER"])
}
}
')
cmd="<refresh><change-folder-readonly>=$MFOLDER<return>"
;;
(~d*)
args=$(echo "$search" | sed 's/\s*~d\s\+\([<>=]\?[^-+*]*\)\?\([-+*]\?\)\(.*$\)/\1\n\2\n\3/g' | sed '/^$/d' | awk '\
{
if (NR==1) {
str = sprintf("d:%s", $1)
}
}
END {
if (NR==1) {
str = sprintf("%s", str)
}
printf("find \"%s\" --format=links --clearlinks --linksdir=\"%s/%s\"", str, ENVIRON["MAILDIR"], ENVIRON["MFOLDER"])
}
')
cmd="<refresh><change-folder-readonly>=$MFOLDER<return>"
;;
(~s*)
# 空格隔开的多个参数,表示逻辑或的关系
key=$(echo "$search" | sed 's/ /\\\|/g')
# 把搜到的邮件作成待搜索的 message-id 列表
args=$(mu find '' -f 'i s' | grep $key | awk '\
{
if (NR==1) {
str = sprintf("i:%s", $1)
} else {
str = sprintf("%s OR i:%s", str, $1)
}
}
END {
if (NR!=0) {
printf("find \"%s\" --format=links --clearlinks --linksdir=\"%s/%s\"", str, ENVIRON["MAILDIR"], ENVIRON["MFOLDER"])
}
}
')
cmd="<refresh><change-folder-readonly>=$MFOLDER<return>"
;;
(*)
# 挖哈哈,mutt 也能全邮箱正文搜索拉,速度杠杠的
key=$(echo "$search")
sh -c "find $MAILDIR/$MFOLDER -type l -exec rm {} \;" # 清空虚拟邮箱
sh -c "recoll -t -q ${key} | grep message/rfc822 | sed -s 's,^.*\('$MAILDIR'[^]]*\)\].*$,\"\1\",' | xargs ln -sft $MAILDIR/$MFOLDER/cur/"
args=
;;
esac
if [ -z ${args} ]; then
# recoll
# TODO 只高亮最近一次的搜索关键字,删除过时的高亮关键字
cmd="<refresh><change-folder-readonly>=$MFOLDER<return>"
cmd=${cmd}"<enter-command>color\ body\ white\ blue\ ${key}<return>"
cmd=${cmd}"<enter-command>color\ index\ white\ blue\ ${key}<return>"
else
# mu
sh -c "mu $args > /dev/null 2>&1 3>&- &"
fi
# clear our mess
printf '\r'; tput ed
# restore cursor position
tput rc
# and tty settings
stty "$saved_tty_settings"
printf %s "$cmd" >&3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment