Subject: Re: reverse-menu-complete re-starting completion on 5.2?
From: Bart Schaefer <[email-masked]>
To: Zsh workers <[email-masked]>
Date: Wed, 9 Mar 2016 14:45:05 -0800
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Mar 9, 12:41pm, Mikael Magnusson wrote:
} Subject: Re: reverse-menu-complete re-starting completion on 5.2?
}
} On Wed, Mar 9, 2016 at 12:12 PM, Mikael Magnusson <[email-masked]> wrote:
} > On Mon, Mar 7, 2016 at 3:05 AM, Mikael Magnusson <[email-masked]> wrote:
} >> On Sun, Mar 6, 2016 at 6:41 PM, Bart Schaefer <[email-masked]> wrote:
} >>> On Feb 29,  1:45am, Oliver Kiddle wrote:
} >>> }
} >>> } [...] I'm really not sure why we need to be so
} >>> } strict about the completion widget matching the last completion widget.
} >>>
} >>> The only reason I can think of is in case menu completion is invoked from
} >>> within a user-defined widget, or otherwise entered by a different widget
} >>>
} >>> Maybe we'd need another flag action for "zle -f" to continue/interrupt

Turns out "zle auto-suffix-retain" already serves this purpose.

This is tricky because widgets created with "zle -C" start directly in
completion context, which means that "zle" generates an error; i.e., if
we naively stick this into _generic:

    _generic:zle:1: widgets can only be called when ZLE is active

(The error should probably be more specific, since a completion widget is
merely the wrong *kind* of widget.)

The solution is to make a normal widget for auto-suffix-retain, then invoke
the completion widget with "zle the-widget -w".  This seems a bit more work
than should be necessary, but see example below.

} >> I can tell you I quite often complete a directory name using normal
} >> completion, and then press ctrl-n to complete files inside by latest
} >> modification date.
} 
} zstyle ':completion:most-recent-file:*' match-original both
} zstyle ':completion:most-recent-file:*' file-sort modification
} zstyle ':completion:most-recent-file:*' file-patterns '*:all\ files'
} zstyle ':completion:most-recent-file:*' hidden all
} zstyle ':completion:most-recent-file:*' completer _files
} zle -C most-recent-file menu-complete _generic
} 
} bindkey "^N"      most-recent-file

Just as-is, this works as you desire if the initial directory completion
is not ambiguous, but cycles if there are multiple possible matches in
the current menu.

} I would like ^N after TAB to start completion over (even if this was
} originally a ^N completion), but TAB after ^N to continue the same
} completion.

The following uses the normal-widget-calls-completion trick.  There may
be a better name for it.

-- 8< --
# Replaces bindkey in the above, otherwise all the same

complete-most-recent-file () {
  [[ $LASTWIDGET != $WIDGET ]] && zle auto-suffix-retain
  zle most-recent-file -w
}
zle -N complete-most-recent-file

bindkey "^N" complete-most-recent-file
-- 8< --

If we're OK with this, Oliver's patch can remain, but it's possible that
there are other user-defined widgets that will start behaving unexpectedly.