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: John Mastro
Subject: Re: hydra: Have hint shown immediately
Date: Wed, 29 Apr 2015 17:06:40 -0700

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



reply via email to

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