From 9b9b7633c13aa9f99ffb5683374f5c11a4966a93 Mon Sep 17 00:00:00 2001 From: Ricardo Wurmus Date: Mon, 3 Jun 2019 17:36:36 +0200 Subject: modules: completer: Simplify. * modules/language/python/completer.scm (aif): Remove macro. (complete-fkn): Simplify, comment, and add docstring; rename... (complete-python): ...to this. (reg): Rename this variable... (dotted-words-regexp): ...to this. --- modules/language/python/completer.scm | 76 +++++++++++++++++------------------ modules/language/python/spec.scm | 2 +- 2 files changed, 39 insertions(+), 39 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)) diff --git a/modules/language/python/spec.scm b/modules/language/python/spec.scm index ea545ac..a267f72 100644 --- a/modules/language/python/spec.scm +++ b/modules/language/python/spec.scm @@ -71,7 +71,7 @@ (catch #t (lambda () (set! (@@ (ice-9 readline) *readline-completion-function*) - (complete-fkn e))) + (complete-python e))) (lambda x #f)) (define-language python -- cgit v1.2.3