5

So, I'm trying to make a generic web search function in Elisp:

(defun look-up-base (url-formatter)
  (let (search url)
    (setq search(thing-at-point 'symbol))
    (setq url (url-formatter search))
    (browse-url url))
  )

This function will just grab the word under the cursor, format the word for web search using url-formatter and then open up the search string in the web browser to perform the search.

Next, I try to implement a function which will Google the word under the cursor, using the previous function as a basis.

(defun google ()
  (interactive)
  (look-up-base (lambda (search) (concat "http://www.google.com/search?q=" search))))

Emacs will not complain if I try to evaluate it, but when I try to use it, Emacs gives me this error message:

setq: Symbol's function definition is void: url-formatter

And I have no clue why this happens. I can't see anything wrong with the function, what am I doing wrong?

Eric
  • 559
  • 2
  • 8
  • 14

1 Answers1

10

I think you need to use funcall:

Instead of (url-formatter search) you should have (funcall url-formatter search).

Lisp expects the name of a function as the first element of a list. If instead you have a symbol associated with a lambda expression or function name, you need to use funcall.

ChrisJ
  • 5,161
  • 25
  • 20
  • 3
    Note that this is true for at least elisp and common lisp but it's still mostly an artifact of function and variable scoping/name spacing. In scheme and clojure and other lisps that do not have separate namespaces for functions and variables, you can use lambda expressions as the first element of an evaluated list and have it work as a function call. – Joost Diepenmaat Mar 18 '11 at 23:17