On 20May14 08:34 +1000, Cameron Simpson wrote:
> On 19May2014 21:08, Rejo Zenger <r...@zenger.nl> wrote:
> >Any implementation of mu into mutt I have found or could think of
> >requires a two step process. One that does the actual search and
> >compiles a list of results, another to browse this list. [1]
> >
> >Now, I would like to do this more smoothly: only give one command (one
> >shortcut), provide the search string and have the results returned
> >immediately. Does anyone know if this is possible - other than hacking
> >the code of mutt?
> >
> >[1] http://dev.mutt.org/trac/wiki/UseCases/SearchingMail
> 
> My approach to this is twofold.

Some time ago I found a solution, which neatly integrates external
searching into mutt. It mimics the typical UI of mutt which enables the
user to enter the search string at the command line and then presents
the search results in the pager (a view on a read-only, temprary
folder).

I cannot find the original source which inspired me, so I just paste my
solution:

mairix_impl.sh
{{{
#!/bin/bash

CONFIG="$HOME/.mutt/mairixrc"

base=$(grep '^base=' $CONFIG | cut -d\= -f2)
mfolder=$(grep '^mfolder=' $CONFIG | cut -d\= -f2)

# prevent rm / further down...
[[ -z "$base/$mfolder" ]] && exit 1

searchdir="$base/$mfolder"

set -f                          # disable globbing.
exec < /dev/tty 3>&1 > /dev/tty # restore stdin/stdout to the terminal,
                                # fd 3 goes to mutt's backticks.
saved_tty_settings=$(stty -g)   # save tty settings before modifying
                                # them

# trap <Ctrl-G> to cancel search
trap '
  printf "\r"; tput ed; tput rc
  printf "/" >&3
  stty "$saved_tty_settings"
  exit
' INT TERM

# put the terminal in cooked mode. Set eof to <return> so that pressing
# <return> doesn't move the cursor to the next line. Disable <Ctrl-Z>
stty icanon echo -ctlecho crterase eof '^M' intr '^G' susp ''

set $(stty size) # retrieve the size of the screen
tput sc          # save cursor position
tput cup "$1" 0  # go to last line of the screen
tput ed          # clear and write prompt
tput sgr0
printf 'Mairix search for: '

# read from the terminal. We can't use "read" because, there won't be
# any NL in the input as <return> is eof.
search=$(dd count=1 2>/dev/null)

# clear the folder and execute a fresh search
( 
  #rm -rf "$searchdir"
  mairix -f $HOME/.mutt/mairixrc -p
  mairix -f $HOME/.mutt/mairixrc --threads $search
) &>/dev/null

# fix the terminal
printf '\r'; tput ed; tput rc
stty "$saved_tty_settings"

# to be executed by mutt when we return
printf "<change-folder-readonly>=$mfolder<return>" >&3
}}}

mairixrc
{{{
# where you keep your mail
base=$HOME/Maildir

# colon separated list of maildirs to index.
#
# I have two accounts each in their own subfolder. the '...' means there 
# are subdirectories to search as well; it's like saying GMail/* and 
# GMX/*
maildir=.:.trash...

# I omit gmail's archive folder so as to pevent duplicate hits
omit=

# search results will be copied to base/<this folder> for viewing in 
# mutt
mfolder=.mfolder

# and the path to the index itself
database=$HOME/.mutt/mailrix.db
}}}

muttrc [snippet]
{{{
# integrate mairix http://pbrisbin.com/posts/mairix
macro generic ,/ "<enter-command>set my_cmd = 
\`$HOME/.mutt/mairix_impl.sh\`<return><enter-command>push \$my_cmd<return>" 
"search messages via mairix"
}}}


Ha, there is also the link to the original mairix integration at
pbrisbin.com.


BTW, in the same way there is an integration of a CheckAttach script
which is very much helpful to me :)
See the last entry here:
http://dev.mutt.org/trac/wiki/ConfigTricks/CheckAttach




Cheers,


-- 
Bastian

Reply via email to