summaryrefslogtreecommitdiff
path: root/modules/language/python/completer.scm
blob: a3d3677ac34a925939d5eaa2b65422f9da9181aa (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(define-module (language python completer)
  #: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-python))

(define (old)
  (@@ (ice-9 readline) *readline-completion-function*))

(define dotted-words-regexp "(\\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)))))

               ;; 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))