help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: using lisp in replacement string


From: Andreas Röhler
Subject: Re: using lisp in replacement string
Date: Thu, 25 Dec 2014 10:25:44 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.3.0

On 24.12.2014 15:25, Guido Van Hoecke wrote:
Hi,

I am trying to apply what I found in the Emacs manual
-> Search (15 Searching and replacement)
-> Replace (15.10 Replacement Commands)
-> Regexp Replace (15.10.2 Regexp Replacement)
"You can use Lisp expressions to calculate parts of the replacement
string.  To do this, write `\,' followed by the expression in the
replacement string.  Each replacement calculates the value of the
expression and converts it to text without quoting (if it's a string,
this means using the string's contents), and uses it in the replacement
string in place of the expression itself."

So I have a temp buffer with following content
|2+6*21|
|3*15|
|7-3|
and I want to replace the formulas between the '|' characters by the
result of passing them to calc-eval:

(replace-regexp "|\\([^|]*\\)|" \\,(calc-eval \\1) nil (point-min)(point-max))
but this causes Debugger entered--Lisp error: (void-variable \\)

So I try it with single slash:
(replace-regexp "|\\([^|]*\\)|" \,(calc-eval \\1) nil (point-min)(point-max))
and then I get : Symbol's value as variable is void: \,

I got the distinct feeling I'm missing something very basic here...

Please advise

TIA,

Guido



As Dimitry said, \, is for use in interactive call only.
Beside you don't need it, as forms might be evaluated the common way.

Try this:

(defun my-calc-eval ()
  (interactive)
  (let (erg)
    (goto-char (point-min))
    (while (re-search-forward "|\\([^|]+\\)|" nil t 1)
      (save-match-data
        (setq erg (calc-eval (match-string-no-properties 1))))
      ;; (message "%s" (match-string-no-properties 1))
      (delete-region (match-beginning 1) (match-end 1))
      (goto-char (match-beginning 1))
      (insert erg)
      (end-of-line) )))



reply via email to

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