jed-users mailing list

[2017 Date Index] [2017 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

[jed-users] Re: getkey ()


On 2017-08-22 Guenter Milde wrote:

> get_keystring: library function

I am using your utilities, Günter, but this one has the same
shortcoming as John's - I can't get it to insert the
completions one by one, rather only the last completion is
inserted.

Just in case you can see something obviously improvable in my
complete function (do_complete), it follows here accompanied by
the two helper functions, file_as_str () and get_tail ()

  Morten

                -------------------------------

private define file_as_str (file)
{
   variable str = "";
   variable fp = fopen (file, "r");

   if (fp == NULL)
     error ("could not open $file"$);

   str = strjoin (fgetslines(fp), "");
   () = fclose (fp);

   return str;
}

private define get_tail (str, stub)
{
   return str[[strbytelen (stub):]];
}

private variable Compl_Array;

public define do_complete (completions_file)
{
   variable stub = "", completions, tails, insertion = "", i = 0;
   variable lb, le, ch;

   ifnot (1 == file_status (completions_file))
     return error ("$completions_file does not exist"$);

   ifnot (__is_initialized (&Compl_Array))
     {
        Compl_Array = strchop (file_as_str (completions_file), '\n', 0);
        Compl_Array = Compl_Array[where (array_map (Int_Type,
                                                    &strlen, Compl_Array))];
     }

   if (looking_at (")"))
     {
        () = bol_fsearch ("\n");
        return indent_line ();
     }

   ifnot ( (eolp ()) || (looking_at (" ")))
     return indent_region_or_line;

   define_word (get_word_chars + "/#_");
   stub = get_word ();

   ifnot (strlen (stub))
     return call ("indent_line");

   completions = 
     Compl_Array [where (array_map (Int_Type,
                                    &string_match, Compl_Array, "^" + stub, 1))];

   tails = array_map (String_Type, &get_tail, completions, stub);
   tails = array_map (String_Type, &strreplace, tails, "{", "\n{\n");
   tails = array_map (String_Type, &strreplace, tails, "}", "\n}");

   if (length (tails) > 0)
     {
        ungetkey ('\t');
        forever
          {
             if (i == length (tails))
               {
                  flush ("no more completions");
                  update_sans_update_hook (0);
                  i = 0;
               }

             if (markp ())
               del_region ();

             lb = what_line ();

             switch (getkey ())
               { case 9: % tab
                    {
                       push_visible_mark ();
                       insertion = tails[i];
                       insert (insertion);
                       update_sans_update_hook (0);
                    }
               }
               { case 127: return; } % backspace
               { 
                    {
                       insertion = tails[i-1];
                       insert (insertion);
                       break;
                    }
               }

             i++;
          }

        le = what_line ();

        if ((le - lb) > 1)
          {
             goto_line (lb);
             push_visible_mark ();
             goto_line (le);
          }

        indent_region_or_line ();

        foreach (["\"\"","(","<","*"])
          {
             ch = ();

             if (is_substr (stub + insertion, ch))
               {
                  () = bsearch (ch);
                  if (ch == "(" or ch == "<" or ch == "\"\"")
                    return go_right_1 ();
                  else
                    return go_left_1 ();
               }
          }
     }
   else
     return flush ("no completion for \"$stub\""$);
}

_______________________________________________
For list information, visit <http://jedsoft.org/jed/mailinglists.html>.


[2017 date index] [2017 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]