From: weber <hugows@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: bind-string-match: comments please
Date: Tue, 30 Sep 2008 12:22:45 -0700 (PDT) [thread overview]
Message-ID: <d9f16093-5255-4b79-b48f-44a1c366b4d9@d70g2000hsc.googlegroups.com> (raw)
In-Reply-To: 1222799700.566372@arno.fh-trier.de
On 30 set, 15:30, Andreas Politz <poli...@fh-trier.de> wrote:
> weber wrote:
> > I've written this helper function:
>
> > (defun bind-string-match (regexp string &optional start)
> > (let ((ret (string-match regexp string start)))
> > (when ret
> > (catch 'end
> > (dolist (i (number-sequence 1 10))
> > (let ((match (match-string i string)))
> > (if match
> > (eval `(setq ,(intern (concat "$" (number-to-string i))) ,match))
> > (throw 'end t))))))
> > ret))
>
> > so that I can access the match groups with $1.. instead of (match-
> > string 1 string).
> > Is there any recommendation against this type of functions? Other than
> > polluting the global namespace?
>
> I think sooner or later you get into trouble, if you don't clear
> the match variables. They could still hold values from 10 matches
> ago, while the current regexp doesn't match at all.
>
> > Also, can one rewrite it without the `eval'?
>
> The 'q' in setq stands for quoted. So, just using set should
> work.
>
>
>
> > TIA,
> > weber
>
> > PS: I'm not a Perl programmer!
>
> Maybe what you really want is some kind of macro ?
>
> (defmacro with-string-matches (string &rest body)
> `(let ,(mapcar (lambda (submatch)
> (list (intern (format "$%d" submatch))
> (match-string submatch (eval string))))
> (number-sequence 0 10))
> ,@body))
>
> (let ((str "abbbbc") idx)
> (setq idx (string-match "a\\(b+\\)c" str))
> (with-string-matches str
> (message "%s %s" $0 $1)))
>
> -ap
Yeah, I was drifting that way after recalling pg's anaphoric
macros....
Thanks a lot :)
-weber
prev parent reply other threads:[~2008-09-30 19:22 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-30 15:02 bind-string-match: comments please weber
2008-09-30 18:30 ` Andreas Politz
2008-09-30 19:22 ` weber [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d9f16093-5255-4b79-b48f-44a1c366b4d9@d70g2000hsc.googlegroups.com \
--to=hugows@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.