Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: fc in non-interactive shells and vared



On Fri, Oct 26, 2007 at 02:43:52PM +0100, Stephane Chazelas wrote:
[...]
> > zle-line-init() { stty onlret; }
> > zle -N zle-line-init
> [...]
> 
> I realised it wouldn't work anyway, I had misinterpreted what
> onlret was doing. I just found another work around, I do
> something like:
> 
>   accept-and-bye() {
>     print -r -- "$BUFFER"
>     stat +link -A pid /proc/self
>     trap - TERM
>     kill $pid
>   }
>   zle -N accept-and-bye
>   bindkey '\r' accept-and-bye
>   bindkey '\n' accept-and-bye
>   a=
>   search=$(vared -p "Search: " -eh a)
> 
> The /proc/self above makes it Linux specific though (exit
> doesn't work: NL is still displayed).
> 
> I'll do more experiments by running zsh -c so that I can use $$.
[...]

I could combine both solutions, it now works perfectly thanks to
your help.

I used:

search=$(
  histfile=$histfile zsh -ic '
  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 "$histfile" 100
  a=; vared -p "Search: " -eh a'
)

I found that HUP worked best. TERM doesn't do anything and USR1
nukes the terminal for some reason (?!).

See script attached.

Next step is to add completion (based on the output of mairix
-d).

-- 
Stéphane
#! /bin/sh -
# 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 mairix, the mail indexing/searching utility for mutt.
# in your ~/.muttrc:
# macro generic S "<enter-command>set my_cmd = \`mutt-mairix\`<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.

mairix_base=~/local/Mail/.mairix
histfile=$mairix_base/cmdhist

# mairix result folder in mutt folder syntax:
mfolder=+.mairix/mfolder

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 and write prompt.
tput ed

cmd="<refresh>"

search=$(
  histfile=$histfile zsh -ic '
  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 "$histfile" 100
  a=; vared -p "Search: " -eh a'
)

if [ "$?" -ne 138 ]; then
  case $search in
    ("<BREAK>") ;;
    ("")
      # rebuild the index
      mairix -F > /dev/null &
      ;;
    (+*)
      # append mode
      mairix -a ${search#+} > /dev/null &
      cmd="<refresh><change-folder-readonly>$mfolder<return>"
      ;;
    (*)
      mairix $search > /dev/null &
      cmd="<refresh><change-folder-readonly>$mfolder<return>"
      ;;
  esac
fi

# clear our mess
printf '\r'; tput ed

# restore cursor position
tput rc

# and tty settings
stty "$saved_tty_settings"

printf %s "$cmd" >&3


Messages sorted by: Reverse Date, Date, Thread, Author