
# Overwrite a lib function that shows multiple matches of help system
# in pager. We give opportunity to choose a match.

#############################################################################
##
#F  HELP_SHOW_MATCHES( <book>, <topic>, <frombegin> )  . . .  show list of
#F  matches or single match directly
##  
if not IsBound(NoSelectHelpMatches) then
  NoSelectHelpMatches := false;
fi;
IsFunction(HELP_SHOW_MATCHES);
HELP_SHOW_MATCHES_LIB := HELP_SHOW_MATCHES;
MakeReadWriteGVar("HELP_SHOW_MATCHES");
HELP_SHOW_MATCHES := function( books, topic, frombegin )
  local   exact,  match,  x,  lines,  cnt,  i,  str,  n,  c;
  
  if NoSelectHelpMatches then
    return HELP_SHOW_MATCHES_LIB(books, topic, frombegin);
  fi;

  # first get lists of exact and other matches
  x := HELP_GET_MATCHES( books, topic, frombegin );
  exact := x[1];
  match := x[2];
  
  # no topic found
  if 0 = Length(match) and 0 = Length(exact)  then
    Print( "Help: no matching entry found\n" );
    return false;
    
  # one exact or together one topic found
  elif 1 = Length(exact) or (0 = Length(exact) and 1 = Length(match)) then
    if Length(exact) = 0 then exact := match; fi;
    i := exact[1];
    str := Concatenation("Help: Showing `", i[1].bookname,": ", 
                                               i[1].entries[i[2]][1], "'\n");
    # to avoid line breaking when str contains escape sequences:
    n := 0;
    while n < Length(str) do
      Print(str{[n+1..Minimum(Length(str), 
                                    n + QuoInt(SizeScreen()[1] ,2))]}, "\c");
      n := n + QuoInt(SizeScreen()[1] ,2);
    od;
    HELP_PRINT_MATCH(i);
    return true;

  # more than one topic found, show overview in pager
  else
    lines := [];
##          ["Help: several entries match this topic - type ?2 to get match [2]\n"];
    HELP_LAST.TOPICS:=[];
    cnt := 0;
    # show exact matches first
    match := Concatenation(exact, match);
    for i  in match  do
      cnt := cnt+1;
      topic := Concatenation(i[1].bookname,": ",i[1].entries[i[2]][1]);
      Add(HELP_LAST.TOPICS, i);
      Add(lines, NCurses.AttributeLineFromEscape(topic));
    od;
    c := NCurses.Select(rec(items := lines, 
         none := true, 
         border := true,
         header := [NCurses.attrs.BOLD, 
           "   Choose an entry to view, 'q' for none (or use ?<nr> later):"]) );
    if c <> false then
      HELP_SHOW_FROM_LAST_TOPICS(c);
    fi;
    return true;
  fi;
end;
MakeReadOnlyGVar("HELP_SHOW_MATCHES");
MakeReadOnlyGVar("HELP_SHOW_MATCHES_LIB");

