unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How do you pre-evaluate a function passed to a macro?
@ 2009-11-13 14:18 Gregory J. Grubbs
  0 siblings, 0 replies; 3+ messages in thread
From: Gregory J. Grubbs @ 2009-11-13 14:18 UTC (permalink / raw)
  To: help-gnu-emacs

I'm calling a function, wanting to pass nil instead of the empty string
when the string argument is zero length.  But the function is defined as
a macro, so the argument gets passed in unevaluated, which fails.  

How do I force evaluation of the argument before it is passed in?

--8<---------------cut here---------------start------------->8---
;; myname is a string taken from user input
(bbdb-search (bbdb-records) (if (> (length myname) 0) myname nil) nil nil)
--8<---------------cut here---------------end--------------->8---

The above call produces the following backtrace:
--8<---------------cut here---------------start------------->8---
  signal(error ("name must be atomic"))
  error("name must be atomic")
  (or (stringp name) (symbolp name) (error "name must be atomic"))
  (let (clauses) (or (stringp name) (symbolp name) (error "name must be atomic")) (or (stringp company) (symbolp company) (error "company must be atomic")) (or (stringp net) (symbolp net) (error "net must be atomic")) (or (stringp notes) (symbolp notes) (error "notes must be atomic")) (or (stringp phone) (symbolp phone) (error "phone must be atomic")) (if phone (setq clauses ...)) (if notes (setq clauses ...)) (if name (setq clauses ...)) (if net (setq clauses ...)) (if company (setq clauses ...)) (\` (let ... ... ...)))
  (lambda (records &optional name company net notes phone) "Search RECORDS for optional arguments NAME, COMPANY, NET, NOTES, PHONE.\nThis macro only emits code for those things being searched for;\nliteral nils at compile-time cause no code to be emitted.\n\nIf you want to reverse the search, bind `bbdb-search-invert' to t." (let (clauses) (or ... ... ...) (or ... ... ...) (or ... ... ...) (or ... ... ...) (or ... ... ...) (if phone ...) (if notes ...) (if name ...) (if net ...) (if company ...) (\` ...)))((bbdb-records) (if (> (length myname) 0) myname nil) nil nil)
  (bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil nil)
  eval((bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil nil))
  eval-expression((bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil nil) nil)
  call-interactively(eval-expression nil nil)
--8<---------------cut here---------------end--------------->8---





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

* Re: How do you pre-evaluate a function passed to a macro?
       [not found] <mailman.10640.1258121962.2239.help-gnu-emacs@gnu.org>
@ 2009-11-13 17:01 ` Barry Margolin
  2009-11-14  8:18 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Barry Margolin @ 2009-11-13 17:01 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.10640.1258121962.2239.help-gnu-emacs@gnu.org>,
 gregory@dynapse.com (Gregory J. Grubbs) wrote:

> I'm calling a function, wanting to pass nil instead of the empty string
> when the string argument is zero length.  But the function is defined as
> a macro, so the argument gets passed in unevaluated, which fails.  
> 
> How do I force evaluation of the argument before it is passed in?

Use eval:

(eval `(bbdb-search (bbdb-records) 
                    ',(if (> (length myname) 0) myname nil)
                    nil nil))

> 
> --8<---------------cut here---------------start------------->8---
> ;; myname is a string taken from user input
> (bbdb-search (bbdb-records) (if (> (length myname) 0) myname nil) nil nil)
> --8<---------------cut here---------------end--------------->8---
> 
> The above call produces the following backtrace:
> --8<---------------cut here---------------start------------->8---
>   signal(error ("name must be atomic"))
>   error("name must be atomic")
>   (or (stringp name) (symbolp name) (error "name must be atomic"))
>   (let (clauses) (or (stringp name) (symbolp name) (error "name must be 
>   atomic")) (or (stringp company) (symbolp company) (error "company must be 
>   atomic")) (or (stringp net) (symbolp net) (error "net must be atomic")) (or 
>   (stringp notes) (symbolp notes) (error "notes must be atomic")) (or 
>   (stringp phone) (symbolp phone) (error "phone must be atomic")) (if phone 
>   (setq clauses ...)) (if notes (setq clauses ...)) (if name (setq clauses 
>   ...)) (if net (setq clauses ...)) (if company (setq clauses ...)) (\` (let 
>   ... ... ...)))
>   (lambda (records &optional name company net notes phone) "Search RECORDS 
>   for optional arguments NAME, COMPANY, NET, NOTES, PHONE.\nThis macro only 
>   emits code for those things being searched for;\nliteral nils at 
>   compile-time cause no code to be emitted.\n\nIf you want to reverse the 
>   search, bind `bbdb-search-invert' to t." (let (clauses) (or ... ... ...) 
>   (or ... ... ...) (or ... ... ...) (or ... ... ...) (or ... ... ...) (if 
>   phone ...) (if notes ...) (if name ...) (if net ...) (if company ...) (\` 
>   ...)))((bbdb-records) (if (> (length myname) 0) myname nil) nil nil)
>   (bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil nil)
>   eval((bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil nil))
>   eval-expression((bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil 
>   nil) nil)
>   call-interactively(eval-expression nil nil)
> --8<---------------cut here---------------end--------------->8---

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


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

* Re: How do you pre-evaluate a function passed to a macro?
       [not found] <mailman.10640.1258121962.2239.help-gnu-emacs@gnu.org>
  2009-11-13 17:01 ` How do you pre-evaluate a function passed to a macro? Barry Margolin
@ 2009-11-14  8:18 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2009-11-14  8:18 UTC (permalink / raw)
  To: help-gnu-emacs

> I'm calling a function, wanting to pass nil instead of the empty string
> when the string argument is zero length.  But the function is defined as
> a macro, so the argument gets passed in unevaluated, which fails.  

> How do I force evaluation of the argument before it is passed in?

> --8<---------------cut here---------------start------------->8---
> ;; myname is a string taken from user input
> (bbdb-search (bbdb-records) (if (> (length myname) 0) myname nil) nil
> nil)

(if (> (length myname) 0)
    (bbdb-search (bbdb-records) myname nil nil)
  (bbdb-search (bbdb-records) nil nil nil))


        Stefan


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

end of thread, other threads:[~2009-11-14  8:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.10640.1258121962.2239.help-gnu-emacs@gnu.org>
2009-11-13 17:01 ` How do you pre-evaluate a function passed to a macro? Barry Margolin
2009-11-14  8:18 ` Stefan Monnier
2009-11-13 14:18 Gregory J. Grubbs

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).