all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Joost Kremers <joost.m.kremers@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: hydra: Have hint shown immediately
Date: 3 May 2015 19:52:03 GMT	[thread overview]
Message-ID: <slrnmkcv33.q97.joost.m.kremers@j.kremers4.news.arnhem.chello.nl> (raw)
In-Reply-To: mailman.1882.1430294575.904.help-gnu-emacs@gnu.org

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	other threads:[~2015-05-03 19:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1882.1430294575.904.help-gnu-emacs@gnu.org>
2015-05-03 19:52 ` Joost Kremers [this message]
2015-04-29  8:02 hydra: Have hint shown immediately Florian Lindner
2015-04-30  0:06 ` John Mastro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=slrnmkcv33.q97.joost.m.kremers@j.kremers4.news.arnhem.chello.nl \
    --to=joost.m.kremers@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.