[inherit('sys$library:starlet','lib$spawn','smgdefs','smg$repaint_line',
'BB.PEN')]
module find_message;

[global] 
procedure find_message(base : integer := 1);

{Search the current topic message list for message number requested.}

var
  message_number : integer;
  message_number_string : string;

begin
  actual_message_count := topic_list^.message_counters.total_message;
  out_of_bounds := true;
  found := false;
  dir_save := dir_curr;

  {Query for message number.  Response must be numeric.  Most other responses
   will assume the user entered a zero.  A <CR> response generates an error
   message.}
  if (actual_message_count > base) then
    begin
      write_menu('','Message number <CR> | Zero <CR> for previous menu: ');
      readstr(message_number_string);
      if (message_number_string = key_exit) then 
        message_number_string := '0';
    end
  else
    message_number_string := '1';

  smg$unpaste_virtual_display(error_display,pasteboard);
  readv(message_number_string,message_number,error:=continue);

  if (message_number_string <> '0') then
    if (length(message_number_string) > 0) then
      begin
        out_of_bounds := ((message_number < 1) or 
                          (message_number > message_count)) and
                          (actual_message_count > 0);

        if (not (out_of_bounds)) then
          begin
            dir_save := dir_curr;
            dir_curr := dir_root;
            repeat
              dir_curr := dir_curr^.next;
              found := (dir_curr^.number = message_number);
            until ((found) or (dir_curr^.next = nil));
          end;

        if (not (found)) then
          begin
            if (not (out_of_bounds)) then
              write_error('Unable to find message.')
            else
              write_error('Message number out of bounds.');
            dir_curr := dir_save;
          end;
      end
    else
      write_error('Need a message number to act on.')
end;

end.
