[ocaml-ctypes] Setting completion function variable for Readline

Jeremy Yallop yallop at gmail.com
Mon Sep 23 08:10:30 BST 2013


On 22 September 2013 20:50, Phil Hagelberg <phil at hagelb.org> wrote:
> I've been using ctypes to call into GNU Readline, and it's been working
> out nicely so far. However, I'd like to implement my own custom
> completer functionality, and I'm having some difficulty with
> that. Readline's documentation[1] says you can set the
> "rl_completion_entry_function" variable to a function pointer which can
> handle your completion:
>
>> Variable: rl_compentry_func_t * rl_completion_entry_function
>>     This is a pointer to the generator function for
>>     rl_completion_matches(). If the value of
>>     rl_completion_entry_function is NULL then the default filename
>>     generator function, rl_filename_completion_function(), is used. An
>>     application-specific completion function is a function whose address
>>     is assigned to rl_completion_entry_function and whose return values
>>     are used to generate possible completions.
>
> But I can't find any way to do this from OCaml, with or without
> Ctypes. I see some mention of passing OCaml functions as arguments to C
> functions, but in this case I need to put it in a variable instead. I
> don't actually know C myself, or I'd imagine it'd be easy to do with a
> small shim written in C. Is this possible with Ctypes?

Yes: you can use the foreign_value function[0] to retrieve the address
of the global, and then set it directly using the assignment operator
<- at .  I've just pushed a small but complete example which might serve
as a useful starting point:

   https://github.com/ocamllabs/ocaml-ctypes/commit/3d2e8c02f3

[0] http://ocamllabs.github.io/ocaml-ctypes/Foreign.html#VALforeign_value
[1] http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VAL%28%3C-@%29


More information about the Ctypes mailing list