emacs-helm / helm-dictionary

Helm source for looking up dictionaries
31 stars 12 forks source link

Use candidates-files instead of candidates-process #9

Closed thierryvolpiatto closed 10 years ago

thierryvolpiatto commented 10 years ago

Here some code that can simplify helm-dictionary:

(defun helm-dict-transformer (candidates)
  "Formats entries retrieved from the data base."
  (loop for i in candidates
        for headerp = (string-match "\\`#" i)
        for entry = (split-string i " :: ")
        for l1terms = (split-string (car entry) " | ")
        for l2terms = (split-string (cadr entry) " | ")
        for filtered-helm-pattern = (replace-regexp-in-string "\\\\$" "" helm-pattern)
        for width = (save-excursion (with-helm-window (window-width)))
        unless headerp
        append
        (loop for l1term in l1terms
              for l2term in l2terms
              if (or (string-match filtered-helm-pattern l1term)
                     (string-match filtered-helm-pattern l2term))
              collect
              (cons 
                (concat
                  (truncate-string-to-width l1term (- (/ width 2) 1) 0 ?\s)
                  " "
                  (truncate-string-to-width l2term (- (/ width 2) 1) 0 ?\s))
                (cons l1term l2term)))))

(defvar helm-source-dict
  '((name . "test dict")
    (candidate-transformer . helm-dict-transformer)
    (candidates-file . helm-dictionary-database)
    (action . (("Insert initial term"  . helm-dictionary-insert-l1term)
               ("Insert translation term" . helm-dictionary-insert-l2term)))))
(helm :sources 'helm-source-dict :buffer "*helm test*")
thierryvolpiatto commented 10 years ago

This would fix also previous issue.

tmalsburg commented 10 years ago

I merged the branch candidates-file into master. Thanks, Thierry for the great suggestions.