我想在emacs中實現vim commandT plugin。此代碼大多是matcher的譯文。加速emacs中的字符串匹配
我在這裏有一些elisp,在我的上網本上使用還是太慢 - 我該如何加快速度?
(eval-when-compile (require 'cl))
(defun commandT-fuzzy-match (choices search-string)
(sort (loop for choice in choices
for score = (commandT-fuzzy-score choice search-string (commandT-max-score-per-char choice search-string))
if (> score 0.0) collect (list score choice))
#'(lambda (a b) (> (first a) (first b)))
))
(defun* commandT-fuzzy-score (choice search-string &optional (score-per-char (commandT-max-score-per-char choice search-string)) (choice-pointer 0) (last-found nil))
(condition-case error
(loop for search-char across search-string
sum (loop until (char-equal search-char (elt choice choice-pointer))
do (incf choice-pointer)
finally return (let ((factor (cond (last-found (* 0.75 (/ 1.0 (- choice-pointer last-found))))
(t 1.0))))
(setq last-found choice-pointer)
(max (commandT-fuzzy-score choice search-string score-per-char (1+ choice-pointer) last-found)
(* factor score-per-char)))))
(args-out-of-range 0.0) ; end of string hit without match found.
))
(defun commandT-max-score-per-char (choice search-string)
(/ (+ (/ 1.0 (length choice)) (/ 1.0 (length search-string))) 2))
確保編譯該部分,因爲這已經有很大幫助。 而基準:
(let ((choices (split-string (shell-command-to-string "curl http://sprunge.us/FcEL") "\n")))
(benchmark-run-compiled 10
(commandT-fuzzy-match choices "az")))
一些解釋代碼應該這樣做,或者一些評論會有幫助。 – Thomas
聽起來很像「iswitchb」和/或「ido」提供的東西。如果Textmate從Emacs那裏得到它,我不會感到驚訝...... http://emacswiki.org/emacs/IswitchBuffers – tripleee
[Here](http://www.emacswiki.org/emacs/Icicles_-_Fuzzy_Completion)是一個描述Emacs Lisp中的各種模糊匹配,並引用了實現代碼。 – Drew