changed algorithm for command-completion to speed it up

This commit is contained in:
frese 2003-12-10 23:35:04 +00:00
parent 4b715095ca
commit ac9cce3473
3 changed files with 88 additions and 20 deletions

View File

@ -1,3 +1,63 @@
;; cached prefix version
(define *completion-check-executable* #f)
(define *cache-alist* '())
(define (strings-with-prefix strings prefix)
(let ((plen (string-length prefix)))
(filter (lambda (s)
(and (>= (string-length s) plen)
(string=? (substring s 0 plen) prefix)))
strings)))
(define (check-cache-for-prefix prefix)
(let ((p (assoc prefix *cache-alist*)))
(if p
(cdr p)
(let loop ((nprefix prefix))
(if (string=? nprefix "")
#f
(let* ((nprefix (substring prefix 0 (- (string-length nprefix) 1)))
(p (assoc nprefix *cache-alist*)))
(if p
(strings-with-prefix (cdr p) prefix)
(loop nprefix))))))))
(define (has-prefix prefix s)
(let ((pl (string-length prefix)))
(and (>= (string-length s) (string-length prefix))
(string=? (substring s 0 pl) prefix))))
(define (add-to-cache prefix list)
(let ((prefixes (strings-with-prefix (map car *cache-alist*) prefix)))
(receive (longer-ones others)
(partition (lambda (p)
(has-prefix prefix (car p)))
*cache-alist*)
(set! *cache-alist*
(cons (cons prefix (append list (append-map cdr longer-ones)))
others)))))
(define (executables-in-path/prefix prefix)
(let ((maybe-result (check-cache-for-prefix prefix)))
(or maybe-result
(let ((result (append-map! (lambda (dir)
(dir-executables/prefix dir prefix))
(thread-fluid exec-path-list))))
(add-to-cache prefix result)
result))))
(define (dir-executables/prefix dir prefix)
(if (file-readable? dir)
(map file-name-nondirectory
(let ((files (glob (string-append dir "/" prefix "*"))))
(if *completion-check-executable*
(filter executable? files)
files)))
'()))
;; caching version
(define *cache* #f) (define *cache* #f)
@ -16,7 +76,7 @@
(filter executable? (glob (string-append dir "/*")))) (filter executable? (glob (string-append dir "/*"))))
'())) '()))
(define (executable? name) (define (executable? name) ;; TODO maybe check if executable by this user
(with-errno-handler (with-errno-handler
((errno packet) ((errno packet)
(else #f)) (else #f))

View File

@ -80,7 +80,8 @@
(files button)) (files button))
(define-structure file-name-completion (define-structure file-name-completion
(export executables-in-path) (export executables-in-path
executables-in-path/prefix)
(open scheme-with-scsh (open scheme-with-scsh
srfi-1 srfi-1
thread-fluids) thread-fluids)

View File

@ -316,7 +316,7 @@
(exec (prompt (root-wm:dpy root-wm) (wm:window cm) (exec (prompt (root-wm:dpy root-wm) (wm:window cm)
(get-option-value (root-wm:options root-wm) (get-option-value (root-wm:options root-wm)
'execute-question) 'execute-question)
#f (finite-complete (executables-in-path))))) #f command-complete)))
(and exec (and exec
(not (string=? exec "")) (not (string=? exec ""))
(let* ((p (make-string-input-port exec)) (let* ((p (make-string-input-port exec))
@ -372,7 +372,11 @@
(define (finite-complete strings) (define (finite-complete strings)
(lambda (str pos) (lambda (str pos)
(do-complete strings str pos)))
(define (do-complete strings str pos)
(let* ((s (substring str 0 pos)) (let* ((s (substring str 0 pos))
(rest (substring str pos (string-length str)))
(candidates (candidates
(filter (lambda (str) (filter (lambda (str)
(and (<= (string-length s) (string-length str)) (and (<= (string-length s) (string-length str))
@ -382,12 +386,12 @@
(cond (cond
((null? candidates) (cons str pos)) ((null? candidates) (cons str pos))
((null? (cdr candidates)) ((null? (cdr candidates))
(cons (car candidates) ;; or insert ?? (cons (string-append (car candidates) rest)
(string-length (car candidates)))) (string-length (car candidates))))
((not (or (equal? common "") (equal? common s))) ((not (or (equal? common "") (equal? common s)))
(cons common ;; or insert?? (cons (string-append common rest)
(string-length common))) (string-length common)))
(else candidates))))) (else candidates))))
(define (common-substring strings) (define (common-substring strings)
(cond (cond
@ -403,6 +407,9 @@
(substring s 0 i))))) (substring s 0 i)))))
(loop 0)))))) (loop 0))))))
(define (command-complete str pos)
(do-complete (executables-in-path/prefix (substring str 0 pos)) str pos))
;; *** observing managers ******************************************** ;; *** observing managers ********************************************
(define (add-manager! root-wm manager) (define (add-manager! root-wm manager)