anmhi - anmhi - nmh(7) interface that (ab)uses less, lesskey, tmux and entr
 (HTM) hg clone https://bitbucket.org/iamleot/anmhi
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       anmhi
       ---
            1 #!/bin/sh
            2 
            3 #
            4 # Copyright (c) 2018 Leonardo Taccari
            5 # All rights reserved.
            6 # 
            7 # Redistribution and use in source and binary forms, with or without
            8 # modification, are permitted provided that the following conditions
            9 # are met:
           10 # 
           11 # 1. Redistributions of source code must retain the above copyright
           12 #    notice, this list of conditions and the following disclaimer.
           13 # 2. Redistributions in binary form must reproduce the above copyright
           14 #    notice, this list of conditions and the following disclaimer in the
           15 #    documentation and/or other materials provided with the distribution.
           16 # 
           17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
           18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
           19 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
           20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
           21 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
           22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
           23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
           24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
           25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
           26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
           27 # POSSIBILITY OF SUCH DAMAGE.
           28 #
           29 
           30 : ${MHTMPDIR:=/tmp}
           31 
           32 : ${ANMHI_LESSKEY:="$(mhpath +)/.lesskey"}
           33 : ${ANMHI_MARK_SEQUENCE:="marked"}
           34 : ${ANMHI_MENU_TOOL:="fzy"}
           35 : ${ANMHI_SCAN_MESSAGES:=100}
           36 : ${ANMHI_SEQUENCES:=".mhcurseqs"}
           37 : ${ANMHI_UNSEEN_SEQUENCE:="$(mhparam unseen-sequence)"}
           38 
           39 anmhi_lesskey="${ANMHI_LESSKEY}"
           40 anmhi_lines="$(stty size | cut -d ' ' -f 1)"
           41 anmhi_mark_sequence="${ANMHI_MARK_SEQUENCE}"
           42 anmhi_menu_tool="${ANMHI_MENU_TOOL}"
           43 anmhi_scan_format='%<(cur)>%| %>%6(msg) %<(mymbox{from})%<{to}To: %13(unquote(decode(friendly{to})))%>%>%<(zero)%17(unquote(decode(friendly{from})))%> %3(day{date}) %3(month{date}) %02(mday{date}) %02(hour{date}):%02(min{date}) %(decode{subject})'
           44 anmhi_scan_messages="${ANMHI_SCAN_MESSAGES}"
           45 anmhi_sequences="${ANMHI_SEQUENCES}"
           46 anmhi_unseen_sequence="${ANMHI_UNSEEN_SEQUENCE}"
           47 
           48 
           49 #
           50 # Parse and do the next action.
           51 #
           52 anmhi_action()
           53 {
           54         local a r
           55 
           56         a=$(printf "$(printf '\\%o' "$1")")
           57 
           58         case "${a}" in
           59         C)
           60                 # Compose a message
           61                 comp ; show cur | anmhi_pager
           62                 r=$?
           63                 ;;
           64         F)
           65                 # Fix current message
           66                 mhfixmsg ; show cur | anmhi_pager
           67                 r=$?
           68                 ;;
           69         L)
           70                 # Select a message interactively
           71                 m="$(scan -format "${anmhi_scan_format}" \
           72                     cur:-${anmhi_scan_messages} cur:${anmhi_scan_messages} |
           73                     anmhi_menu | awk '/> / { print $2; next } { print $1 }')"
           74                 if [ "${m}" ]; then
           75                         show "${m}" | anmhi_pager
           76                 else
           77                         show cur | anmhi_pager
           78                 fi
           79                 r=$?
           80                 ;;
           81         M)
           82                 # Mark current message with ${anmhi_mark_sequence}
           83                 mark -sequence "${anmhi_mark_sequence}" ; show cur | anmhi_pager
           84                 r=$?
           85                 ;;
           86         N)
           87                 # Go to the next folder with unread messages
           88                 fnext ; show cur | anmhi_pager
           89                 r=$?
           90                 ;;
           91         P)
           92                 # Go to the previous folder with unread messages
           93                 fprev ; show cur | anmhi_pager
           94                 r=$?
           95                 ;;
           96         R)
           97                 # Reply (-nogroup)
           98                 repl -nogroup
           99                 show cur | anmhi_pager
          100                 r=$?
          101                 ;;
          102         T)
          103                 # Mark a message interactively asking for a sequence
          104                 if which rlwrap; then
          105                         cd "${MHTMPDIR}"
          106                         mark -list |
          107                         grep -Ev "(cur|`mhparam unseen-sequence`)" |
          108                         cut -d ':' -f 1 > "${anmhi_sequences}"
          109                         clear
          110                         sequence=$(rlwrap -f "${anmhi_sequences}" -s 0 -o cat | tr -d '[:space:]')
          111                         rm "${anmhi_sequences}"
          112                         cd -
          113                 else
          114                         clear
          115                         read sequence
          116                 fi
          117                 if [ "${sequence}" ]; then
          118                         mark -sequence "${sequence}"
          119                 fi
          120                 show cur | anmhi_pager
          121                 r=$?
          122                 ;;
          123         U)
          124                 # Show all unseen messages
          125                 unseen | anmhi_pager
          126                 r=$?
          127                 ;;
          128         a)
          129                 # Mark all messages in the current folder as read
          130                 mark -sequence "${anmhi_unseen_sequence}" \
          131                     -delete "${anmhi_unseen_sequence}"
          132                 show last | anmhi_pager
          133                 r=$?
          134                 ;;
          135         c)
          136                 # Show the current message
          137                 show cur | anmhi_pager
          138                 r=$?
          139                 ;;
          140         f)
          141                 # Select a folder interactively
          142                 f="$(folders -fast | anmhi_menu)"
          143 
          144                 if [ "${f}" ]; then
          145                         folder -nocreate +"${f}"
          146                 fi
          147                 show cur | anmhi_pager
          148                 r=$?
          149                 ;;
          150         n)
          151                 # Show the next message
          152                 { next || show cur ; } | anmhi_pager
          153                 r=$?
          154                 ;;
          155         p)
          156                 # Show the previous message
          157                 { prev || show cur ; } | anmhi_pager
          158                 r=$?
          159                 ;;
          160         u)
          161                 # Show all unseen messages in the current folder
          162                 scan -format "${anmhi_scan_format}" unseen | anmhi_pager
          163                 r=$?
          164                 ;;
          165         r)
          166                 # Reply (-group)
          167                 repl -group
          168                 show cur | anmhi_pager
          169                 r=$?
          170                 ;;
          171         esac
          172 
          173         return ${r}
          174 }
          175 
          176 #
          177 # Generate a lesskey(1) file.
          178 #
          179 anmhi_lesskey()
          180 {
          181 
          182         if [ ! -f "${anmhi_lesskey}" ]; then
          183                 lesskey -o "${anmhi_lesskey}" - <<EOF
          184                 C       quit    C
          185                 F       quit    F
          186                 L       quit    L
          187                 M       quit    M
          188                 R       quit    R
          189                 T       quit    T
          190                 U       quit    U
          191                 ^n      quit    N
          192                 ^p      quit    P
          193                 \en     quit    n
          194                 \ep     quit    p
          195                 \kr     quit    n
          196                 \kl     quit    p
          197                 }       quit    n
          198                 {       quit    p
          199                 a       quit    a
          200                 c       quit    c
          201                 f       quit    f
          202                 h       quit    p
          203                 l       quit    n
          204                 r       quit    r
          205                 q       quit
          206                 u       quit    u
          207 EOF
          208         fi
          209 }
          210 
          211 #
          212 # Invoke text selector menu with the desidered options.
          213 #
          214 anmhi_menu()
          215 {
          216 
          217         ${anmhi_menu_tool} -l "${anmhi_lines}"
          218 }
          219 
          220 #
          221 # Invoke the PAGER with the desidered options.
          222 #
          223 anmhi_pager()
          224 {
          225 
          226         less -c -k "${anmhi_lesskey}"
          227         return $?
          228 }
          229 
          230 
          231 #
          232 # anmhi, a nmh(7) interface. anmhi is a simple interface to various nmh(7)
          233 # tools to interactively manage emails with a PAGER.
          234 #
          235 main()
          236 {
          237         anmhi_lesskey
          238 
          239         if [ $# -gt 0 ]; then
          240                 args="$@"
          241         fi
          242 
          243         show $args | anmhi_pager
          244         r=$?
          245 
          246         until anmhi_action ${r}; do
          247                 r=$?
          248         done
          249 }
          250 
          251 main $@