unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* hydra: Have hint shown immediately
@ 2015-04-29  8:02 Florian Lindner
  2015-04-30  0:06 ` John Mastro
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Lindner @ 2015-04-29  8:02 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I wonder if anyone is familiar with the hydra package: 
https://github.com/abo-abo/hydra

I have an hydra to call search engines:

(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!


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.

Thanks,
Florian




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: hydra: Have hint shown immediately
  2015-04-29  8:02 hydra: Have hint shown immediately Florian Lindner
@ 2015-04-30  0:06 ` John Mastro
  0 siblings, 0 replies; 3+ messages in thread
From: John Mastro @ 2015-04-30  0:06 UTC (permalink / raw)
  To: Florian Lindner, help-gnu-emacs@gnu.org

Hi Florian,

> (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!

I don't know the answer regarding Hydra, but I can offer some
subjective, mostly cosmetic feedback on `flo/start-search'.

As a talking piece, here's something very similar which will hopefully
show a couple small idioms that you may find useful:

(defun flo/start-search (url)
  (interactive)
  (let ((default (if (use-region-p)
                     (buffer-substring-no-properties (region-beginning)
                                                     (region-end))
                   (thing-at-point 'symbol))))
    (browse-url
     (format url (read-string (if default
                                  (format "Search for (default %s): "
                                          default)
                                "Search for: ")
                              nil
                              nil
                              default)))))

The main differences are:
  1. Pass the default to `read-string' as argument DEFAULT rather than
     INITIAL-INPUT. Show the default in the prompt so you can tell it's
     available. The benefit is that you can still just hit RET to use
     the default, but you don't need to delete the default if you want
     to search for something else.

  2. Use `format' to add the search term to the URL, rather than
     `replace-regexp-in-string'.

  3. Prefer `use-region-p' over `region-active-p'.

  4. Don't leave trailing parens. I know it can seem a bit weird at
     first, but I promise it's worth getting used to.

Keep having fun with Emacs!

-- 
john



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: hydra: Have hint shown immediately
       [not found] <mailman.1882.1430294575.904.help-gnu-emacs@gnu.org>
@ 2015-05-03 19:52 ` Joost Kremers
  0 siblings, 0 replies; 3+ messages in thread
From: Joost Kremers @ 2015-05-03 19:52 UTC (permalink / raw)
  To: help-gnu-emacs

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)


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-05-03 19:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-29  8:02 hydra: Have hint shown immediately Florian Lindner
2015-04-30  0:06 ` John Mastro
     [not found] <mailman.1882.1430294575.904.help-gnu-emacs@gnu.org>
2015-05-03 19:52 ` Joost Kremers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).