jed-users mailing list

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

Re: Proposed new addition to Jed...


Dave Laird <dlaird@xxxxxxxxxx> wrote:

> I've tried that, and haven't the slightest clue how to (1) send the
> highlighted word to dictd, or how to (2) open a read-only buffer in the
> current running copy of Jed with the dict returns, in the case of the
> thesaurus. 
> [...]
> Any other ideas? 

I have attached "dict_lookup.sl" which will give you a convenient(?)
interface to choosing a translation/synonym. It is something I have just
cut out of my po_mode where it is used with Freedict Translation
Dictionaries, but it can also be used with the moby-thesaurus. I had to
make a few modifications, so it may have some rough edges. Instructions
for installation and use are in the file.


Regards,

Morten
%% Installation: Copy this file to a place in Jed's library path. In
%% your ~/.jedrc insert:
%% 
%% () = evalfile ("dict_lookup.sl");
%%
%% and if in Xjed you want to be able to double click on a word in e.g.
%% text_mode then insert:
%%
%% define text_mode_hook ()
%%  {
%%   set_buffer_hook ("mouse_2click", &dict_lookup->dict_mouse_2click_hook ());
%%  }
%%
%% Change the variables "Dict_Dictionary" and "Charset" to your own liking
%% Finally bind the function "dict_lookup_word" to some key of choice.
%%
%% Use: If the editing point is on a word or you double click on one (xjed)
%%      it will be looked up and a default translation shown in the
%%      minibuffer. If there is more than one match, they can be browsed
%%      by hitting TAB and can be chosen with completion. Hitting
%%      <enter> will let the chosen translation replace the marked
%%      word/region. Ctrl-g quits the prompt.
%% 
%%           
implements ("dict_lookup");

static variable Dict_Dictionary = "moby-thesaurus";
static variable Charset = "iso-8859-1";

%% The dict output is always in UTF-8, so convert it to user's charset
%% until UTF-8 is standard for most Jed users.
static define dict_iconv (str)
{
   str = str_quote_string (str, "()\\^$[]*.+?", '\\');

   variable cmd = sprintf ("echo %s | iconv -f utf-8 -t %s", str, Charset);
   variable fp = popen (cmd, "r");
   
   if (fp != NULL)
     {
        () = fread (&str, String_Type, 50000, fp);
     }
   
   () = pclose (fp);
   
   str = strtrim (str);
   return str;
}

%% Unfortunately there is no standard for formatting the dict output
%% and a variety of formats exists which may differ with various
%% distributions, depending on how the dictionary was converted into
%% dict format, and since there is no option to the dict command that
%% will spit out just the translations, something must be done to
%% isolate them in the output. The following assumes that the lines with
%% the translations always have some indentation, and preferably more
%% indentation than the headword. If the lines with the translations
%% have no indentation, this lookup function will fail.
public define dict_lookup_word ()
{
   variable prompt, indent, trans, tran, word, def, cmd;
   variable len, pos, fp, i = 0, max_indent = 0;
   variable dict_cmd = "dict -P - -C -d";
   
   ERROR_BLOCK { pop_mark (0); }
   
   if (markp ())
     {
        () = dupmark ();
        word = sprintf ("\"%s\"", bufsubstr ());
     }
   else
     {
        bskip_word_chars ();
        push_mark ();
        skip_word_chars ();
        () = dupmark ();
        word = bufsubstr ();
     }

   !if (strlen (word))
     {
        pop_mark (0);
        word = read_mini ("Word to look up:", "", "");
     }

   cmd = sprintf ("%s %s %s", dict_cmd, Dict_Dictionary, word);
   prompt = sprintf ("%s ->:", word),
   
   fp = popen (cmd, "r");
   
   if (fp != NULL)
     {
        () = fread (&trans, String_Type, 50000, fp);
     }
   
   () = pclose (fp);
   
   trans = strchop (trans, '\n', 0);
   
   % get the value of the maximum indentation
   foreach (trans)
     {
        $0 = ();
        if (string_match ($0, "^[ \t]*", 1))
          (, len) = string_match_nth (0);
        if (len > max_indent) max_indent = len;
     }
   
   if (max_indent == 0)
     { 
        pop_mark (0);
        verror ("no translation for \"%s\"", word);
     }
   
   % create a string consisting of the number of spaces from the value
   % of max_indent.
   indent = String_Type[max_indent];
   for (i = 0; i <= max_indent-1; i++) indent[i] = " ";
   indent = strjoin (indent, "");
   
   % isolate the lines with the most indentation in the dict
   % output.
   trans = 
     trans [where 
            (array_map (Int_Type, &string_match, trans, 
                            sprintf ("^%s", indent), 1))];
   
   % in case there is information about word class, domain or
   % pronunciation in square or angle brackets like eg. <adj.> [bot.],
   % then filter them out from the output.
   _for (0, length (trans)-1, 1)
     {
        i = ();
        tran = trans[i];
        if (string_match (tran, "[[<].*[]>]", 1))
          {
             (pos, len) = string_match_nth (0);
             tran = tran [[0:pos-1]];
          }
        trans [i] = tran;
     }
   trans = strjoin (trans, "\n");
   trans = strcompress (trans, ",,\n");
   trans = strcompress (trans, "  ");
   trans = str_replace_all (trans, ", ", ",");
   trans = strchop (trans, ',', 0);
   def = trans [0];
   trans = strjoin (trans, ",");
   trans = dict_iconv (trans);
   tran = read_with_completion (trans, prompt, def, "", 's');
   if (markp ()) del_region ();
   insert (tran + " ");
}

static define dict_mouse_2click_hook (line, col, but, shift)
{
   dict_lookup_word ();
   return (0);
}

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