Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think.com!compass!worley From: worley@compass.com (Dale Worley) Newsgroups: comp.emacs Subject: Re: Problem with macros Message-ID: Date: 14 Nov 90 16:03:15 GMT References: <1183@red15.qtp.ufl.edu> Sender: root@compass.com Organization: Compass, Inc., Wakefield, MA, U.S.A. Lines: 64 In-reply-to: beck@qtp.ufl.edu's message of 13 Nov 90 21:51:07 GMT In article <1183@red15.qtp.ufl.edu> beck@qtp.ufl.edu (Sullivan Beck) writes: (defmacro test (char) (` (save-excursion (set-buffer (get-buffer tmp-buffer)) (save-excursion (goto-char 1) (forward-line 2) (forward-char 3) (delete-char 1) (insert char))))) (setq char "*") (test "!") First, remember what a macro does: The code (test "!") is effectively replaced by (save-excursion (set-buffer (get-buffer tmp-buffer)) (save-excursion (goto-char 1) (forward-line 2) (forward-char 3) (delete-char 1) (insert char))) Note that this code is *not* inside the scope of the defmacro, so char is not bound to "!" when it is executed. What you want to do is replace the "char" in the expansion by the value of the argument "char" to the invocation of the macro. The way to do this is is to "unquote" "char". For example: (` (a b (, c))) If "c" has the value 3, then this evaluates to (a b 3) Therefore, you want the macro body to be (` (save-excursion (set-buffer (get-buffer tmp-buffer)) (save-excursion (goto-char 1) (forward-line 2) (forward-char 3) (delete-char 1) (insert (, char))))) When playing around with macros, it can be very useful to use the macroexpand function to see what particular expressions expand to. (Set the second (ENVIRONMENT) argument to nil.) Also, in E-lisp, people use the word "quote" for the form (quote ...) or '... . When you are using "`", it is called "backquote". Dale Worley Compass, Inc. worley@compass.com -- In 1976, Seymour Cray was asked "In twenty years, what language will supercomputers be programmed in?" His answer was, "I don't know what it will *be*, but it will be *called* Fortran." And now we have Fortran 8x (now Fortran 90)...