unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* replace-regexp-in-string, using the dyn scoped variable STR
@ 2009-11-11 19:18 Lennart Borgman
  2009-11-11 20:25 ` Stefan Monnier
  2009-11-12  9:59 ` Juri Linkov
  0 siblings, 2 replies; 9+ messages in thread
From: Lennart Borgman @ 2009-11-11 19:18 UTC (permalink / raw)
  To: Emacs-Devel devel

Sometimes it is necessary to use the variable str which is dynamically
scoped in replace-regexp-in-string. Below is an example of this.

Could we please document the use of str (and perhaps name it quite differently)?

Example:

;;(test-org-freemind-unescape-str-to-org "mA≌B<C<=")
;;(test-org-freemind-unescape-str-to-org "<<")
(defun test-org-freemind-unescape-str-to-org (fm-str)
  (let ((org-str fm-str))
    (setq org-str (replace-regexp-in-string
                   "&#x\\([a-f0-9]\\{2,4\\}\\);"
                   (lambda (m)
                     (char-to-string
                      ;; Note: str is scoped dynamically from
                      ;; `replace-regexp-in-string'.
                      (+ (string-to-number (match-string 1 str) 16)
                         0
                         )))
                   org-str))))




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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-11 19:18 replace-regexp-in-string, using the dyn scoped variable STR Lennart Borgman
@ 2009-11-11 20:25 ` Stefan Monnier
  2009-11-11 20:40   ` Lennart Borgman
  2009-11-12  9:59 ` Juri Linkov
  1 sibling, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2009-11-11 20:25 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Emacs-Devel devel

> Sometimes it is necessary to use the variable str which is dynamically
> scoped in replace-regexp-in-string. Below is an example of this.

This is bad practice.  It may seem convenient in some cases, but we
definitely do not want to encourage it.


        Stefan




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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-11 20:25 ` Stefan Monnier
@ 2009-11-11 20:40   ` Lennart Borgman
  0 siblings, 0 replies; 9+ messages in thread
From: Lennart Borgman @ 2009-11-11 20:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Emacs-Devel devel

On Wed, Nov 11, 2009 at 9:25 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>> Sometimes it is necessary to use the variable str which is dynamically
>> scoped in replace-regexp-in-string. Below is an example of this.
>
> This is bad practice.  It may seem convenient in some cases, but we
> definitely do not want to encourage it.

That is ok with me, but then maybe we could find another way to to the
replacement in cases like this conventiently?




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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-11 19:18 replace-regexp-in-string, using the dyn scoped variable STR Lennart Borgman
  2009-11-11 20:25 ` Stefan Monnier
@ 2009-11-12  9:59 ` Juri Linkov
  2009-11-12 10:17   ` Lennart Borgman
  2009-11-12 15:26   ` Stefan Monnier
  1 sibling, 2 replies; 9+ messages in thread
From: Juri Linkov @ 2009-11-12  9:59 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: Emacs-Devel devel

>     (setq org-str (replace-regexp-in-string
>                    "&#x\\([a-f0-9]\\{2,4\\}\\);"
>                    (lambda (m)
>                      (char-to-string
>                       ;; Note: str is scoped dynamically from
>                       ;; `replace-regexp-in-string'.
>                       (+ (string-to-number (match-string 1 str) 16)

`str' has the same value as `m'.

-- 
Juri Linkov
http://www.jurta.org/emacs/




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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-12  9:59 ` Juri Linkov
@ 2009-11-12 10:17   ` Lennart Borgman
  2009-11-12 15:26   ` Stefan Monnier
  1 sibling, 0 replies; 9+ messages in thread
From: Lennart Borgman @ 2009-11-12 10:17 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Emacs-Devel devel

On Thu, Nov 12, 2009 at 10:59 AM, Juri Linkov <juri@jurta.org> wrote:
>>     (setq org-str (replace-regexp-in-string
>>                    "&#x\\([a-f0-9]\\{2,4\\}\\);"
>>                    (lambda (m)
>>                      (char-to-string
>>                       ;; Note: str is scoped dynamically from
>>                       ;; `replace-regexp-in-string'.
>>                       (+ (string-to-number (match-string 1 str) 16)
>
> `str' has the same value as `m'.

Thanks Juri. (Wonder how I missed that?)

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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-12  9:59 ` Juri Linkov
  2009-11-12 10:17   ` Lennart Borgman
@ 2009-11-12 15:26   ` Stefan Monnier
  2009-11-12 15:58     ` Andreas Schwab
  1 sibling, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2009-11-12 15:26 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Lennart Borgman, Emacs-Devel devel

>> (setq org-str (replace-regexp-in-string
>> "&#x\\([a-f0-9]\\{2,4\\}\\);"
>> (lambda (m)
>> (char-to-string
>> ;; Note: str is scoped dynamically from
>> ;; `replace-regexp-in-string'.
>> (+ (string-to-number (match-string 1 str) 16)

> `str' has the same value as `m'.

No, it doesn't: str is "the whole string" whereas m is only the
(match-string 0) part of it (which is why it's more convenient to use
(match-string 1 str) in Lennart's example than to try and figure out
where submatch 1 is in m, which basically requires either doing another
string-match or using ad-hoc methods (e.g. (substring m 3 -1))).


        Stefan




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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-12 15:26   ` Stefan Monnier
@ 2009-11-12 15:58     ` Andreas Schwab
  2009-11-12 17:05       ` Stefan Monnier
  0 siblings, 1 reply; 9+ messages in thread
From: Andreas Schwab @ 2009-11-12 15:58 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Juri Linkov, Lennart Borgman, Emacs-Devel devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>> (setq org-str (replace-regexp-in-string
>>> "&#x\\([a-f0-9]\\{2,4\\}\\);"
>>> (lambda (m)
>>> (char-to-string
>>> ;; Note: str is scoped dynamically from
>>> ;; `replace-regexp-in-string'.
>>> (+ (string-to-number (match-string 1 str) 16)
>
>> `str' has the same value as `m'.
>
> No, it doesn't: str is "the whole string" whereas m is only the
> (match-string 0) part of it

Which is always a prefix of str, and only different from str if the
match was empty.

> (which is why it's more convenient to use (match-string 1 str) in
> Lennart's example than to try and figure out where submatch 1 is in m,

The submatch 1 of m is at (match-beginning 1).

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-12 15:58     ` Andreas Schwab
@ 2009-11-12 17:05       ` Stefan Monnier
  2009-11-12 22:07         ` Lennart Borgman
  0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2009-11-12 17:05 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Juri Linkov, Lennart Borgman, Emacs-Devel devel

>>> `str' has the same value as `m'.
>> No, it doesn't: str is "the whole string" whereas m is only the
>> (match-string 0) part of it
> Which is always a prefix of str, and only different from str if the
> match was empty.

Duh, you're right.  Not sure what I was smoking when I tried replacing
str with m.  So, even more reasons to discourage the use of `str'.


        Stefan




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

* Re: replace-regexp-in-string, using the dyn scoped variable STR
  2009-11-12 17:05       ` Stefan Monnier
@ 2009-11-12 22:07         ` Lennart Borgman
  0 siblings, 0 replies; 9+ messages in thread
From: Lennart Borgman @ 2009-11-12 22:07 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Juri Linkov, Andreas Schwab, Emacs-Devel devel

On Thu, Nov 12, 2009 at 6:05 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>>>> `str' has the same value as `m'.
>>> No, it doesn't: str is "the whole string" whereas m is only the
>>> (match-string 0) part of it
>> Which is always a prefix of str, and only different from str if the
>> match was empty.
>
> Duh, you're right.  Not sure what I was smoking when I tried replacing
> str with m.  So, even more reasons to discourage the use of `str'.


Thanks for the help. I still wonder why I used `str'. I am not smoking.




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

end of thread, other threads:[~2009-11-12 22:07 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-11 19:18 replace-regexp-in-string, using the dyn scoped variable STR Lennart Borgman
2009-11-11 20:25 ` Stefan Monnier
2009-11-11 20:40   ` Lennart Borgman
2009-11-12  9:59 ` Juri Linkov
2009-11-12 10:17   ` Lennart Borgman
2009-11-12 15:26   ` Stefan Monnier
2009-11-12 15:58     ` Andreas Schwab
2009-11-12 17:05       ` Stefan Monnier
2009-11-12 22:07         ` Lennart Borgman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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