Paste: readline support

Author: erikc
Mode: factor
Date: Thu, 19 May 2011 05:05:57
Plain Text |
USING: alien.c-types alien.data alien.libraries alien.strings                                                          
destructors io io.encodings.utf8 kernel libc locals                                                                    
sequences tools.completion namespaces vocabs                                                                           
accessors prettyprint fry math threads ;                                                                               
QUALIFIED: readline.ffi                                                                                                
IN: readline                                                                                                           
                                                                                                                       
: readline ( prompt -- str )                                                                                           
    [                                                                                                                  
        readline.ffi:readline [                                                                                        
            |free utf8 alien>string [                                                                                  
                [ ] [ readline.ffi:add_history ] if-empty                                                              
            ] keep                                                                                                     
        ] when*                                                                                                        
    ] with-destructors ;                                                                                               
                                                                                                                       
SYMBOL: completions                                                                                                    
                                                                                                                       
: prefixed-words ( prefix -- words )                                                                                   
    '[ name>> _ head? ] all-words swap filter [ name>> ] map ;                                                         
                                                                                                                       
: word-completion ( -- alien )                                                                                         
    [| name state |                                                                                                    
      state dup zero?                                                                                                  
      [ name prefixed-words dup completions tset ]                                                                     
      [ completions tget ] if ?nth [                                                                                   
          utf8 malloc-string                                                                                           
      ] [ f completions tset f ] if*                                                                                   
    ] readline.ffi:rl_compentry_func_t ;                                                                               
                                                                                                                       
word-completion "rl_completion_entry_function" "readline"                                                              
load-library dlsym-raw 0 void* set-alien-value

Annotation: new version

Author: erikc
Mode: factor
Date: Thu, 19 May 2011 05:43:04
Plain Text |
USING: alien.c-types alien.data alien.libraries alien.strings                                                          
destructors io io.encodings.utf8 kernel libc locals
sequences tools.completion namespaces vocabs                                                                           
accessors prettyprint fry math threads ;
QUALIFIED: readline.ffi
IN: readline

<PRIVATE
SYMBOL: completions

: prefixed-words ( prefix -- words )
    '[ name>> _ head? ] all-words swap filter [ name>> ] map ;

: clear-completions ( -- )
    f completions tset ;

: get-completions ( prefix -- completions )
    completions tget dup [ nip ] [ drop
        prefixed-words dup completions tset
    ] if ;
PRIVATE>

: readline ( prompt -- str )
    [
        readline.ffi:readline [
            |free utf8 alien>string [
                [ ] [ readline.ffi:add_history ] if-empty
            ] keep
        ] [ f ] if*
    ] with-destructors ;

: word-completion ( -- alien )
    [| name state |
      state name get-completions ?nth
      [ utf8 malloc-string ]
      [ clear-completions f ] if*
    ] readline.ffi:rl_compentry_func_t ;

word-completion readline.ffi:set-rl_completion_entry_function

New Annotation

Summary:
Author:
Mode:
Body: