unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* problem with elisp
@ 2008-10-08 18:57 Seweryn Kokot
  2008-10-08 19:05 ` Nikolaj Schumacher
       [not found] ` <mailman.606.1223492748.25473.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 7+ messages in thread
From: Seweryn Kokot @ 2008-10-08 18:57 UTC (permalink / raw
  To: help-gnu-emacs

Hello,

Could you explain to me why the following function doesn't work in the first
version and when swapping line
	(let ((string-temp (make-temp-name ""))) 
with
    (let ((string-temp "@temp@"))           
it works correctly?


(defun my-swap-string (string-a string-b)
  (interactive "sFirst string to swap: \nsSecond string to swap: ")
  (save-excursion  
	(let ((string-temp (make-temp-name ""))) ; this doesn't work
;;	(let ((string-temp "@temp@"))            ; this works
	  (goto-char (point-min))
	  (while (search-forward string-a nil t)
		(replace-match string-temp))
	  (goto-char (point-min))
	  (while (search-forward string-b nil t)
		(replace-match string-a))
	  (goto-char (point-min))
	(while (search-forward string-temp nil t)
	  (replace-match string-b)))))


Thanks in advance.
-- 
regards,
Seweryn





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

* Re: problem with elisp
       [not found] <mailman.604.1223491809.25473.help-gnu-emacs@gnu.org>
@ 2008-10-08 18:59 ` Joost Kremers
  0 siblings, 0 replies; 7+ messages in thread
From: Joost Kremers @ 2008-10-08 18:59 UTC (permalink / raw
  To: help-gnu-emacs

Seweryn Kokot wrote:
> Could you explain to me why the following function doesn't work in the first

define "doesn't work". what does happen?

> (defun my-swap-string (string-a string-b)
>   (interactive "sFirst string to swap: \nsSecond string to swap: ")
>   (save-excursion  
> 	(let ((string-temp (make-temp-name ""))) ; this doesn't work

include the line

(message "%s" string-temp)

right after the let, and see what string-temp is set to.

FWIW your code works fine for me with make-temp-name...


-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


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

* Re: problem with elisp
  2008-10-08 18:57 Seweryn Kokot
@ 2008-10-08 19:05 ` Nikolaj Schumacher
  2008-10-08 20:04   ` Seweryn Kokot
       [not found] ` <mailman.606.1223492748.25473.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 7+ messages in thread
From: Nikolaj Schumacher @ 2008-10-08 19:05 UTC (permalink / raw
  To: Seweryn Kokot; +Cc: help-gnu-emacs

Seweryn Kokot <sewkokot@gmail.com> wrote:

> Could you explain to me why the following function doesn't work in the first
> version and when swapping line
> 	(let ((string-temp (make-temp-name "")))
> with
>     (let ((string-temp "@temp@"))
> it works correctly?

Could you specify what's not working?  It seems to work for me.

`make-temp-name' is not a good idea for what you're doing, though.
While the same string will never be returned twice, there is no
guarantee that the string doesn't exist in your buffer.  So make sure
you use a very unique prefix.  On the other hand, there's no added
benefit from using `make-temp-name' instead of just the prefix.


regards,
Nikolaj Schumacher




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

* Re: problem with elisp
       [not found] ` <mailman.606.1223492748.25473.help-gnu-emacs@gnu.org>
@ 2008-10-08 19:25   ` Barry Margolin
  0 siblings, 0 replies; 7+ messages in thread
From: Barry Margolin @ 2008-10-08 19:25 UTC (permalink / raw
  To: help-gnu-emacs

In article <mailman.606.1223492748.25473.help-gnu-emacs@gnu.org>,
 Nikolaj Schumacher <me@nschum.de> wrote:

> Seweryn Kokot <sewkokot@gmail.com> wrote:
> 
> > Could you explain to me why the following function doesn't work in the first
> > version and when swapping line
> > 	(let ((string-temp (make-temp-name "")))
> > with
> >     (let ((string-temp "@temp@"))
> > it works correctly?
> 
> Could you specify what's not working?  It seems to work for me.
> 
> `make-temp-name' is not a good idea for what you're doing, though.
> While the same string will never be returned twice, there is no
> guarantee that the string doesn't exist in your buffer.  So make sure
> you use a very unique prefix.  On the other hand, there's no added
> benefit from using `make-temp-name' instead of just the prefix.

Unless you try to use the function in the buffer containing the 
function's source code, since the prefix is guaranteed to exist in that 
buffer.  If you use make-temp-name with a suitably random prefix, the 
chance of an unintended match should be negligible.

-- 
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] 7+ messages in thread

* Re: problem with elisp
  2008-10-08 19:05 ` Nikolaj Schumacher
@ 2008-10-08 20:04   ` Seweryn Kokot
  2008-10-08 20:33     ` Nikolaj Schumacher
  0 siblings, 1 reply; 7+ messages in thread
From: Seweryn Kokot @ 2008-10-08 20:04 UTC (permalink / raw
  To: help-gnu-emacs

Nikolaj Schumacher <me@nschum.de> writes:

> Seweryn Kokot <sewkokot@gmail.com> wrote:
>
>> Could you explain to me why the following function doesn't work in the first
>> version and when swapping line
>> 	(let ((string-temp (make-temp-name "")))
>> with
>>     (let ((string-temp "@temp@"))
>> it works correctly?
>
> Could you specify what's not working?  It seems to work for me.
>
> `make-temp-name' is not a good idea for what you're doing, though.
> While the same string will never be returned twice, there is no
> guarantee that the string doesn't exist in your buffer.  So make sure
> you use a very unique prefix.  On the other hand, there's no added
> benefit from using `make-temp-name' instead of just the prefix.

I see the problem in this function.

First I tested it with for example a buffer containing
--- start of buffer
1
2
--- end of buffer

so swapping 1 with 2 gives

--- start of buffer
5816MHs
1
--- end of buffer

string-temp was 5826MHs and with these 3 lines 

	  (goto-char (point-min))
	  (while (search-forward string-b nil t) ; string-b=2
		(replace-match string-a))            ; string-a=1

string-temp is changed to 5816MHs and in the end, the last 3 lines do
nothing. 

Any idea how to solve the problem?

-- 
regards,
Seweryn





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

* Re: problem with elisp
  2008-10-08 20:04   ` Seweryn Kokot
@ 2008-10-08 20:33     ` Nikolaj Schumacher
  2008-10-09 21:23       ` Seweryn Kokot
  0 siblings, 1 reply; 7+ messages in thread
From: Nikolaj Schumacher @ 2008-10-08 20:33 UTC (permalink / raw
  To: Seweryn Kokot; +Cc: help-gnu-emacs

Seweryn Kokot <sewkokot@gmail.com> wrote:

> string-temp is changed to 5816MHs and in the end, the last 3 lines do
> nothing.
>
> Any idea how to solve the problem?

You can search for words only:

(re-search-forward (concat "\\b" (regexp-quote string-b) "\\b"))


Or you can give up the temp string and search for both at once like this:

(re-search-forward (concat "\\(" (regexp-quote string-a) "\\)\\|\\("
                           (regexp-quote string-a) "\\)"))

and then replace based on which one was found:

(if (match-beginning 1)
    (replace-match string-b)
  (replace-match string-a))

regards,
Nikolaj Schumacher




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

* Re: problem with elisp
  2008-10-08 20:33     ` Nikolaj Schumacher
@ 2008-10-09 21:23       ` Seweryn Kokot
  0 siblings, 0 replies; 7+ messages in thread
From: Seweryn Kokot @ 2008-10-09 21:23 UTC (permalink / raw
  To: help-gnu-emacs

Nikolaj Schumacher <me@nschum.de> writes:

> Or you can give up the temp string and search for both at once like this:
>
> (re-search-forward (concat "\\(" (regexp-quote string-a) "\\)\\|\\("
>                            (regexp-quote string-a) "\\)"))
>
> and then replace based on which one was found:
>
> (if (match-beginning 1)
>     (replace-match string-b)
>   (replace-match string-a))

I prefer this idea.

Thanks Nikolaj, Joost and Barry for help
-- 
regards,
Seweryn





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

end of thread, other threads:[~2008-10-09 21:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.604.1223491809.25473.help-gnu-emacs@gnu.org>
2008-10-08 18:59 ` problem with elisp Joost Kremers
2008-10-08 18:57 Seweryn Kokot
2008-10-08 19:05 ` Nikolaj Schumacher
2008-10-08 20:04   ` Seweryn Kokot
2008-10-08 20:33     ` Nikolaj Schumacher
2008-10-09 21:23       ` Seweryn Kokot
     [not found] ` <mailman.606.1223492748.25473.help-gnu-emacs@gnu.org>
2008-10-08 19:25   ` Barry Margolin

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