* using lisp in replacement string
@ 2014-12-24 14:25 Guido Van Hoecke
2014-12-24 14:27 ` Dmitry Gutov
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Guido Van Hoecke @ 2014-12-24 14:25 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: using lisp in replacement string
2014-12-24 14:25 using lisp in replacement string Guido Van Hoecke
@ 2014-12-24 14:27 ` Dmitry Gutov
2014-12-24 14:40 ` Guido Van Hoecke
2014-12-24 14:56 ` Nicolas Richard
2014-12-25 9:25 ` Andreas Röhler
2 siblings, 1 reply; 12+ messages in thread
From: Dmitry Gutov @ 2014-12-24 14:27 UTC (permalink / raw)
To: Guido Van Hoecke, help-gnu-emacs
On 12/24/2014 04:25 PM, Guido Van Hoecke wrote:
> (replace-regexp "|\\([^|]*\\)|" \\,(calc-eval \\1) nil (point-min)(point-max))
> but this causes Debugger entered--Lisp error: (void-variable \\)
The second argument doesn't look like a string to me. Are you missing
the quotes?
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: using lisp in replacement string
2014-12-24 14:27 ` Dmitry Gutov
@ 2014-12-24 14:40 ` Guido Van Hoecke
2014-12-24 14:52 ` Dmitry Gutov
0 siblings, 1 reply; 12+ messages in thread
From: Guido Van Hoecke @ 2014-12-24 14:40 UTC (permalink / raw)
To: Dmitry Gutov; +Cc: help-gnu-emacs
Hi Dimitry,
On 24 December 2014 at 15:27, Dmitry Gutov <dgutov@yandex.ru> wrote:
> On 12/24/2014 04:25 PM, Guido Van Hoecke wrote:
>
> (replace-regexp "|\\([^|]*\\)|" \\,(calc-eval \\1) nil
>> (point-min)(point-max))
>> but this causes Debugger entered--Lisp error: (void-variable \\)
>>
>
> The second argument doesn't look like a string to me. Are you missing the
> quotes?
>
Not really, if you quote it, it replaces the formulas by the calc-eval
expression :(
Guido
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: using lisp in replacement string
2014-12-24 14:40 ` Guido Van Hoecke
@ 2014-12-24 14:52 ` Dmitry Gutov
[not found] ` <CAEySM9GB6z1yBJSy8AzwN4a6LK4KRQBYQZ2-fZRDtmQ0XTCYng@mail.gmail.com>
2014-12-24 21:36 ` ronaldo.mercado
0 siblings, 2 replies; 12+ messages in thread
From: Dmitry Gutov @ 2014-12-24 14:52 UTC (permalink / raw)
To: Guido Van Hoecke; +Cc: help-gnu-emacs
On 12/24/2014 04:40 PM, Guido Van Hoecke wrote:
> Not really, if you quote it, it replaces the formulas by the calc-eval
> expression :(
If you don't quote it, it's not a string.
Anyway, check out these lines from `replace-regexp' docstring:
"This function is for interactive use only;
in Lisp code use `re-search-forward' and `replace-match' instead."
...
"In interactive calls, the replacement text may contain `\,'"
You're not using it interactively.
^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <CAEySM9GB6z1yBJSy8AzwN4a6LK4KRQBYQZ2-fZRDtmQ0XTCYng@mail.gmail.com>]
* Re: using lisp in replacement string
[not found] ` <CAEySM9GB6z1yBJSy8AzwN4a6LK4KRQBYQZ2-fZRDtmQ0XTCYng@mail.gmail.com>
@ 2014-12-24 16:25 ` Guido Van Hoecke
[not found] ` <mailman.16678.1419438348.1147.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 12+ messages in thread
From: Guido Van Hoecke @ 2014-12-24 16:25 UTC (permalink / raw)
To: help-gnu-emacs
Hi,
"This function is for interactive use only;
>> in Lisp code use `re-search-forward' and `replace-match' instead."
>> ...
>> "In interactive calls, the replacement text may contain `\,'"
>>
>
I still do not get it. Say following content in the *scratch* buffer:
|2+6*21|
|3*15|
|7-3|
(defun test1 ()
"just display the lisp form rather than execute it"
(interactive)
(goto-char (point-min))
(while (re-search-forward "^|\\([^|]*\\)|$" nil t)
(replace-match "(calc-eval \"\\1\")" nil nil nil 1)))
(defun test2 ()
"try to replace the searched string with the result of the lisp form"
(interactive)
(goto-char (point-min))
(while (re-search-forward "^|\\([^|]*\\)|$" nil t)
(replace-match (calc-eval "\\1") nil nil nil 1)))
M-x test1 replaces the first three lines as follows:
|(calc-eval "2+6*21")|
|(calc-eval "3*15")|
|(calc-eval "7-3")|
C-x C-e of each of the calc-eval statements yields the expected result.
After undoing these changes and returning to the initial three lines,
M-x test2 complains:
while: Wrong type argument: stringp, (0 "Expected a number")
Any help would be highly appreciated,
Guido
^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <mailman.16678.1419438348.1147.help-gnu-emacs@gnu.org>]
* Re: using lisp in replacement string
[not found] ` <mailman.16678.1419438348.1147.help-gnu-emacs@gnu.org>
@ 2014-12-24 16:43 ` Barry Margolin
0 siblings, 0 replies; 12+ messages in thread
From: Barry Margolin @ 2014-12-24 16:43 UTC (permalink / raw)
To: help-gnu-emacs
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 963 bytes --]
In article <mailman.16678.1419438348.1147.help-gnu-emacs@gnu.org>,
Guido Van Hoecke <guivho@gmail.com> wrote:
> Hi,
>
> "This function is for interactive use only;
> >> in Lisp code use `re-search-forward' and `replace-match' instead."
> >> ...
> >> "In interactive calls, the replacement text may contain `\,'"
> >>
> >
> I still do not get it. Say following content in the *scratch* buffer:
This isn't done in replace-match. It's specific to replace-regexp, and
it checks whether it was invoked interactively rather than from Lisp to
determine whether to look for that string. It's a total hack.
If you're writing your own Lisp functions, you should use match-string
to get the matched string or capture group, do whatever you want to
produce the replacement (e.g. call calc-eval), and then call
replace-match with this result.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: using lisp in replacement string
2014-12-24 14:52 ` Dmitry Gutov
[not found] ` <CAEySM9GB6z1yBJSy8AzwN4a6LK4KRQBYQZ2-fZRDtmQ0XTCYng@mail.gmail.com>
@ 2014-12-24 21:36 ` ronaldo.mercado
1 sibling, 0 replies; 12+ messages in thread
From: ronaldo.mercado @ 2014-12-24 21:36 UTC (permalink / raw)
To: dgutov, guivho; +Cc: help-gnu-emacs
Hello, the function below performs a global search-replace
(defun hh ()
(interactive)
(let (match-beginning match-end match-string replacement)
(goto-char (point-min))
(while (re-search-forward "|\\([^|]*\\)|" nil t)
(setq match-beginning (match-beginning 0))
(setq match-end (match-end 0))
(setq replacement (concat "|" (calc-eval (match-string 1)) "|") )
(goto-char match-beginning)
(re-search-forward "|\\([^|]*\\)|" match-end nil)
(replace-match replacement t t nil 0))))
I did not understand why I needed to perform re-search-forward twice though. without the second search, results were garbled.
________________________________________
From: help-gnu-emacs-bounces+ronaldo.mercado=diamond.ac.uk@gnu.org [help-gnu-emacs-bounces+ronaldo.mercado=diamond.ac.uk@gnu.org] on behalf of Dmitry Gutov [dgutov@yandex.ru]
Sent: 24 December 2014 14:52
To: Guido Van Hoecke
Cc: help-gnu-emacs
Subject: Re: using lisp in replacement string
On 12/24/2014 04:40 PM, Guido Van Hoecke wrote:
> Not really, if you quote it, it replaces the formulas by the calc-eval
> expression :(
If you don't quote it, it's not a string.
Anyway, check out these lines from `replace-regexp' docstring:
"This function is for interactive use only;
in Lisp code use `re-search-forward' and `replace-match' instead."
...
"In interactive calls, the replacement text may contain `\,'"
You're not using it interactively.
--
This e-mail and any attachments may contain confidential, copyright and or privileged material, and are for the use of the intended addressee only. If you are not the intended addressee or an authorised recipient of the addressee please notify us of receipt by returning the e-mail and do not use, copy, retain, distribute or disclose the information in or attached to the e-mail.
Any opinions expressed within this e-mail are those of the individual and not necessarily of Diamond Light Source Ltd.
Diamond Light Source Ltd. cannot guarantee that this e-mail or any attachments are free from viruses and we cannot accept liability for any damage which you may sustain as a result of software viruses which may be transmitted in or with the message.
Diamond Light Source Limited (company no. 4375679). Registered in England and Wales with its registered office at Diamond House, Harwell Science and Innovation Campus, Didcot, Oxfordshire, OX11 0DE, United Kingdom
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: using lisp in replacement string
2014-12-24 14:25 using lisp in replacement string Guido Van Hoecke
2014-12-24 14:27 ` Dmitry Gutov
@ 2014-12-24 14:56 ` Nicolas Richard
2014-12-25 9:25 ` Andreas Röhler
2 siblings, 0 replies; 12+ messages in thread
From: Nicolas Richard @ 2014-12-24 14:56 UTC (permalink / raw)
To: Guido Van Hoecke; +Cc: help-gnu-emacs
Hello,
Guido Van Hoecke <guivho@gmail.com> writes:
> "You can use Lisp expressions to calculate parts of the replacement
> string. To do this, write `\,' followed by the expression in the
> replacement string.
> [...]
> I got the distinct feeling I'm missing something very basic here...
The \, construct is meant for interactive use.
Try it interactively, then do C-x M-: to see what lisp form it produced.
HTH,
--
Nicolas Richard
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: using lisp in replacement string
2014-12-24 14:25 using lisp in replacement string Guido Van Hoecke
2014-12-24 14:27 ` Dmitry Gutov
2014-12-24 14:56 ` Nicolas Richard
@ 2014-12-25 9:25 ` Andreas Röhler
2014-12-25 11:50 ` Guido Van Hoecke
2 siblings, 1 reply; 12+ messages in thread
From: Andreas Röhler @ 2014-12-25 9:25 UTC (permalink / raw)
To: help-gnu-emacs
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) )))
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: using lisp in replacement string
2014-12-25 9:25 ` Andreas Röhler
@ 2014-12-25 11:50 ` Guido Van Hoecke
0 siblings, 0 replies; 12+ messages in thread
From: Guido Van Hoecke @ 2014-12-25 11:50 UTC (permalink / raw)
To: Andreas Röhler; +Cc: help-gnu-emacs
Hi Andreas,
On 25 December 2014 at 10:25, Andreas Röhler <andreas.roehler@easy-emacs.de>
wrote:
> On 24.12.2014 15:25, Guido Van Hoecke wrote:
>
>>
>> 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: \,
>>
>
> 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) )))
>
> Works like a charm, thanks a lot!
Guido
^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <mailman.16672.1419431131.1147.help-gnu-emacs@gnu.org>]
* Re: using lisp in replacement string
[not found] <mailman.16672.1419431131.1147.help-gnu-emacs@gnu.org>
@ 2014-12-29 4:40 ` Emanuel Berg
2014-12-29 17:55 ` Guido Van Hoecke
0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2014-12-29 4:40 UTC (permalink / raw)
To: help-gnu-emacs
Guido Van Hoecke <guivho@gmail.com> writes:
> 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
Yes, you can do that! I did that once with a file like
this:
normal bright
bk r g y bl m c w bk r g y bl m c w
r 0 233 50 170 100 200 0 150 110 255 0 220 133 255 0 190
g 0 0 150 170 100 0 170 150 110 33 190 220 133 0 190 190
b 0 0 50 0 200 200 170 150 110 33 0 0 255 255 190 190
I thought it could be cool to, for example, add 10% to
each - the span is (as you see) 0 to 255, so +10% from
150 would be ... 177?
Anyway, I got rid of that code because it was much
easier and more powerful to just tweak it by hand,
digit by digit. Still, it is the exact situation as
yours - replace a value by another value, that is a
function of the first value - or, replace x by f(x) -
so I can tell you how I did it. (Let this be a lesson
to naive Elispers - including those who verbalize the
lesson - always keep your code, even that which you
don't use...)
Now: check out this, from the help of
`replace-regexp'.
(while (re-search-forward REGEXP nil t)
(replace-match TO-STRING nil nil))
So instead of the `replace-match' stuff above, you
write a function that examines `match-beginning',
`match-end', and `match-string', and then use that as
input to your Elisp, to produce the on-the-fly
TO-STRING (in the phrasing of the above Elisp).
Good luck!
(And when you get it to work, post it here :))
--
underground experts united
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: using lisp in replacement string
2014-12-29 4:40 ` Emanuel Berg
@ 2014-12-29 17:55 ` Guido Van Hoecke
0 siblings, 0 replies; 12+ messages in thread
From: Guido Van Hoecke @ 2014-12-29 17:55 UTC (permalink / raw)
To: Emanuel Berg; +Cc: help-gnu-emacs
Hi Emanuel,
On 29 December 2014 at 05:40, Emanuel Berg <embe8573@student.uu.se> wrote:
> Guido Van Hoecke <guivho@gmail.com> writes:
>
> > 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
>
> Yes, you can do that! I did that once with a file like
> this:
> ...
> (And when you get it to work, post it here :))
>
> The final solution looks like this (the actual input is an 8 column org
table and the field for the computation is the fifth field):
(let (quantity)
(while (re-search-forward
"^|[^|]*|[^|]*|[^|]*|[^|]*|\\([^|]+\\)|.*$" nil t 1)
(save-match-data
(setq quantity (calc-eval (match-string-no-properties 1))))
(delete-region (match-beginning 1) (match-end 1))
(goto-char (match-beginning 1))
(insert quantity)
(end-of-line)))
I never found a working replace-match approach.
Many thanks to all volunteers that helped to come to this solution.
Guido
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2014-12-29 17:55 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-24 14:25 using lisp in replacement string Guido Van Hoecke
2014-12-24 14:27 ` Dmitry Gutov
2014-12-24 14:40 ` Guido Van Hoecke
2014-12-24 14:52 ` Dmitry Gutov
[not found] ` <CAEySM9GB6z1yBJSy8AzwN4a6LK4KRQBYQZ2-fZRDtmQ0XTCYng@mail.gmail.com>
2014-12-24 16:25 ` Guido Van Hoecke
[not found] ` <mailman.16678.1419438348.1147.help-gnu-emacs@gnu.org>
2014-12-24 16:43 ` Barry Margolin
2014-12-24 21:36 ` ronaldo.mercado
2014-12-24 14:56 ` Nicolas Richard
2014-12-25 9:25 ` Andreas Röhler
2014-12-25 11:50 ` Guido Van Hoecke
[not found] <mailman.16672.1419431131.1147.help-gnu-emacs@gnu.org>
2014-12-29 4:40 ` Emanuel Berg
2014-12-29 17:55 ` Guido Van Hoecke
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).