help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hydra: Have hint shown immediately


From: Joost Kremers
Subject: Re: hydra: Have hint shown immediately
Date: 3 May 2015 19:52:03 GMT
User-agent: slrn/pre1.0.0-18 (Linux)

Florian Lindner wrote:
> (defhydra hydra-google (global-map "C-c /" :color blue)
>   "Search Engines"
>   ("c" (lambda ()
>          (interactive)
>          (flo/start-search 
> "http://en.cppreference.com/mwiki/index.php?title=Special:Search&search=%s";))
>    "C++ Reference")
>   ("g" (lambda ()
>          (interactive)
>          (flo/start-search "https://google.de/#q=%s";))
>    "Google")
>   )
>
> flo/start-search looks like that:
>
> (defun flo/start-search (url)
>   (interactive)
>   (browse-url (replace-regexp-in-string
>                (regexp-quote "%s")
>                (if (region-active-p)
>                    (buffer-substring (region-beginning) (region-end))
>                  (read-string "Search for: " (thing-at-point 'symbol) 
> "searches" nil t)
>                  )
>                url)
>               )
>   )
>
>
> interactive is just for testing, it will vanish probably. Since this is my 
> first elisp "program" please criticise!

Well, one thing: don't put closing parens on their own line:

(defun flo/start-search (url)
  (interactive)
  (browse-url (replace-regexp-in-string
               (regexp-quote "%s")
               (if (region-active-p)
                   (buffer-substring (region-beginning) (region-end))
                 (read-string "Search for: " (thing-at-point 'symbol)
                              "searches" nil t))
               url)))

When you read Lisp code, try and ignore the parens, just look at the 
indentation.

>
> My question: How can I make the hydra menu to be shown as soon as I press C-
> c /? Becaue I exit the hydra after pressing the key I'll never see the menu 
> and forget option when more search engines are aded.

You can do that by binding the hydra with `define-key' (or
`global-set-key') rather than passing it a key map:

(defhydra hydra-google (:color blue)
  "Search Engines"
  ("c"
   (flo/start-search 
"http://en.cppreference.com/mwiki/index.php?title=Special:Search&search=%s";)
   "C++ Reference")
  ("g"
   (flo/start-search "https://google.de/#q=%s";)
   "Google"))
(define-key global-map "C-c /" #'hydra-google/body)

Note also that if you pass a single sexp as CMD in a hydra head, it is
automatically wrapped in a lambda, so you can get rid of the explicit
lambdas in your hydra.



-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]