nmh-workers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Nmh-workers] showing the n messages 'around' a particular message


From: Robert Elz
Subject: Re: [Nmh-workers] showing the n messages 'around' a particular message
Date: Thu, 19 Jan 2006 12:49:02 +0700

    Date:        Wed, 18 Jan 2006 20:44:42 +0700
    From:        Robert Elz <address@hidden>
    Message-ID:  <address@hidden>

  | Usage is
  |     xxx [any non message designator scan args] [-N] [A:[[-]N]]

That should have (of course) been ...

        xxx [any non message designator scan args] [-N] [A[:[-]N]]

(but I doubt anyone ever thought different, if you tried it).

  | There are a couple of odd corner cases that aren't handled sanely
  | (one is where there are many "vacant" message numbers before the
  | message that's to be put in the middle - output still appears, but
  | it probably isn't what you really want).

The following version does a lot better in that case, now this is
(aside from no error messages) more or less what was requested, I think.

kre

#! /bin/sh

# MH scan listing, with selected message (approximately) centred

# Usage: $0 [non-message-scan-args] [-N] [msg[:[-]N]]

N=20            # default number of scan lines

CMD=${0##*/}
set -- $( mhparam "${CMD}" ) "$@"       # extract args from MH profile

# Args for internal uses of scan, not used for final output
SC_ARGS="-format %(msg) -noheader -noclear -width 20"

CUR=''
AC="$#"; I=0; Z=''
while [ ${I} -lt ${AC} ]
do
        I=$(( ${I} + 1 ))

        X="$1"
        shift

        # handle extra arg required by an earlier one
        test -n "${Z}" && {
                set -- "$@" "${X}"
                Z=''
                continue
        }

        case "${X}" in

        cur)            ;;
        cur:[0-9]*)     N="${X#cur:}" ;;
        cur:-[0-9]*)    N="${X#cur:-}" ;;

        next)           CUR=NEXT ;;
        next:[0-9]*)    CUR=NEXT; N="${X#next:}" ;;
        next:-[0-9]*)   CUR=NEXT; N="${X#next:-}" ;;

        prev)           CUR=PREV;;
        prev:[0-9]*)    CUR=PREV; N="${X#prev:}" ;;
        prev:-[0-9]*)   CUR=PREV; N="${X#prev:-}" ;;

        all)            N=1000000;;

        [A-Za-z]*)      echo "Sequences don't work here" >&2; exit 1;;

        -[0-9]*)        N="${X#-}";;
        [0-9]*:[0-9]*)  CUR="${X%:*}"; N="${X#*:}";;
        [0-9]*:-[0-9]*) CUR="${X%:*}"; N="${X#*:-}";;
        [0-9]*)         CUR="${X}";;

        #       -f* covers -file, -form and -format ...
        -f*)            set -- "$@" "${X}"; Z=Z;;
        -width)         set -- "$@" "${X}"; Z=Z;;

        *)              set -- "$@" "${X}";;

        esac
done

case "${CUR}" in
NEXT)   CUR=$( scan "$@" ${SC_ARGS} next ) || exit 1 ;;
PREV)   CUR=$( scan "$@" ${SC_ARGS} prev ) || exit 1 ;;
esac

M=$(( ( ${N} + 1 ) / 2 ))

if test -n "${CUR}" || CUR=$( scan "$@" ${SC_ARGS} cur )
then
        FIRST=$( scan "$@" ${SC_ARGS} first ) || exit 1
        LAST=$( scan "$@" ${SC_ARGS} last ) || exit 1

        if [ $(( ${LAST} - ${FIRST} )) -lt ${N} ]
        then
                # Folder cannot contain N messages, list everything
                exec scan "$@"

        elif [ $(( ${CUR} - ${FIRST} )) -lt ${M} ]
        then
                # Start would be before beginning, start at start instead
                exec scan "$@" "${FIRST}:${N}"

        elif [ $(( ${LAST} - ${CUR} )) -lt ${M} ]
        then
                # End would be after last, go backwards from final message
                exec scan "$@" "${LAST}:-${N}"
        else
                # This is where it gets a bit trickier...

                # find which message is N/2 messages earlier
                # (as best we can do)
                START=$( scan "$@" ${SC_ARGS} ${CUR}:-${M} | sed 1q )

                if [ $( scan "$@" "${START}:${N}" ${SC_ARGS} |
                                                        wc -l ) -lt "${N}" ]
                then
                        # Going forwards we get less than N lines
                        # (might mean that there are < N/2 lines after cur)
                                                                                
                        # so, list backwards instead, we might get more
                        LAST=$( scan "$@" "${CUR}:${M}" ${SC_ARGS} | tail -1 )
                        exec scan "$@" "${LAST}:-${N}"

                else
                        # Otherwise list N messages starting N/2 messages ago
                        exec scan "$@" "${START}:${N}"
                fi
        fi
fi






reply via email to

[Prev in Thread] Current Thread [Next in Thread]