diff options
Diffstat (limited to 'modules/language/python/completer.scm')
-rw-r--r-- | modules/language/python/completer.scm | 76 |
1 files changed, 38 insertions, 38 deletions
diff --git a/modules/language/python/completer.scm b/modules/language/python/completer.scm index 047248e..a3d3677 100644 --- a/modules/language/python/completer.scm +++ b/modules/language/python/completer.scm @@ -2,47 +2,47 @@ #:use-module (language python list) #:use-module (language python dir) #:use-module (system base language) + #:use-module (ice-9 match) #:use-module (ice-9 regex) - #:export (complete-fkn)) + #:export (complete-python)) -(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) +(define (old) + (@@ (ice-9 readline) *readline-completion-function*)) -(define (old) (@@ (ice-9 readline) *readline-completion-function*)) +(define dotted-words-regexp "(\\w+(\\.\\w+)*)\\.(\\w*)$") -(define reg "(\\w+(\\.\\w+)*)\\.(\\w*)$") +(define (complete-python proc) + "Return a readline completion procedure for completing dotted Python +names." + (define completions '()) + (define regexp #f) + (define prefix "") + (letrec ((complete + (lambda (text continue?) + (cond + (continue? + (match completions + (() #f) + ((candidate . rest) + (set! completions rest) + (if (string-match regexp candidate) + (string-append prefix "." candidate) + (complete text #t))))) -(define (complete-fkn eval) - (let ((old (old)) - (strs '() ) - (pp "" ) - (regexp #f )) - (letrec - ((compl - (lambda (text continue?) - (if continue? - (if (null? strs) - #f - (let ((str (car strs))) - (set! strs (cdr strs)) - (if (string-match regexp str) - (string-append pp "." str) - (compl text #t)))) - (if (and (equal? (language-name (current-language)) 'python) - (in "." text)) - (aif it (string-match reg text) - (let* ((n (match:count it)) - (p (match:substring it 1)) - (t (match:substring it (- n 1))) - (d (to-list (dir (eval p))))) - (begin - (set! strs d) - (set! pp p) - (set! regexp (string-append - "^" (if (string-null? t) - "[^_]" - (regexp-quote t)))) - (compl text #t))) - #f) - (old text continue?)))))) - compl))) + ;; Initialize completions + ((and (equal? (language-name (current-language)) 'python) + (in "." text)) + (and=> (string-match dotted-words-regexp text) + (lambda (m) + (let ((head (match:substring m 1)) + (tail (match:substring m (1- (match:count m))))) + (set! prefix head) + (set! completions (to-list (dir (proc head)))) + (set! regexp (string-append + "^" (if (string-null? tail) + "[^_]" + (regexp-quote tail)))) + (complete text #t))))) + (else ((old) text continue?)))))) + complete)) |