emacs-helm / helm-dictionary

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

run with a single dictionary and provide default search term #31

Open mooseyboots opened 2 years ago

mooseyboots commented 2 years ago

i have various dictionaries specified in helm-dictionary-database and i'm interested in being able to choose which one of them to use when i run helm-dictionary. i'm mainly interested in this to speed it up. i have dicts for different languages but usually only want to search for a term in one of them at a time.

i'd also like to feed it a default input string, so i can call it from a function and provide the search term.

i came up with this:

(defun helm-dictionary (&optional dictionary query)
  (interactive)
  (let ((helm-source-dictionary
         (if dictionary
           (helm-dictionary-build (car dictionary) (cdr dictionary))
           (mapcar
            (lambda (x) (helm-dictionary-build (car x) (cdr x)))
            (if (stringp helm-dictionary-database)
                (list (cons "Search dictionary" helm-dictionary-database))
              helm-dictionary-database))))
         (input (or query (thing-at-point 'word))))
        (helm :sources (append helm-source-dictionary (list helm-source-dictionary-online))
              :full-frame t
              :default input
              :candidate-number-limit 500
              :buffer "*helm dictionary*")))

then i call it like so:

  (let ((query (fetch-query-string-from-somewhere)))
    (helm-dictionary (assoc "de-en" helm-dictionary-database) query)))

the dictionary arg seems to work right, though i'm not sure if thats the best way to do it?

and for now the query argument isn't respected by helm's :default. i then tried again without my new arg, and, at least for me, it also doesn't seem to use (thing-at-point 'word) either. helm-dictionary opens with no default input. am i missing something there?

mooseyboots commented 2 years ago

ah, i just saw #30. switching back to :default input to :input input in this case seems to work.

let binding helm-maybe-use-default-as-input from my caller function doesn't seem to work but maybe i can live with that.

now i wonder how i can hand helm-dictionary my query string surrounded by \b to narrow the search down, as concatenating converts the \b to ^H, which returns zero results.

tmalsburg commented 2 years ago

Perhaps you just have to use \\b when concatenating?

mooseyboots commented 2 years ago

that works! thanks.

i also asked helm about the use of :resume. it would be great if it would work without an input, as then the performance issue re loading lots of dictionaries each time would be solved, but alas, thierry says it can't be done.

an updated version of my modified helm-dict is here: https://github.com/mooseyboots/helm-dictionary/commit/de34f0416b3ddba930cdfdc659d17e441dfe92dd.

works for me: when i'm viewing results in emacs-leo i can now instantly run helm-dictionary with just one dictionary enabled and search for the same term.