[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Newbie regexp question
From: |
Friedrich Dominicus |
Subject: |
Re: Newbie regexp question |
Date: |
31 Oct 2002 15:41:22 +0100 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Common Lisp) |
Paul Cohen <paco@enea.se> writes:
> Hi all,
>
> Thanks to everyone who has been kind to take time to answer my question!
> Jay's answer is definitely closest to solving my problem.
>
> "Bingham, Jay" wrote:
>
> > After seeing Mike and Friedrich's exchange on your question I decided to
> > create my own solution, a general purpose function that allows the user to
> > specify the start and end as regular expressions and supply a replacement
> > for the text between them, and optionally by giving a numeric prefix
> > argument to the function force it to replace the tags as well.
>
> Neat.
>
> > In the process I noticed that Mike's function has a logic flaw. It will
> > never find the case of a missing end tag and instead deletes that final
> > start tag.
>
> Ok.
>
> > Here is my function if you want it.
>
> Yes I do! :-)
>
> >
> > (defun replace-between-regexp (start-re end-re repl-str &optional incl)
> > "Replace the text between two regular expressions supplied as arguments.
> > With a numeric argument the regular expressions are included.
> > When called non interactively incl should be nil for non-inclusion and
> > non-nil for inclusion."
> > (interactive "sStart regexp: \nsEnd regexp: \nsReplace between %s and %s
> > with: \nP")
> > (while (re-search-forward start-re nil t)
> > (let ((beg (if incl (match-beginning 0) (match-end 0)))
> > (end
> > (progn
> > (if (re-search-forward end-re nil t)
> > (if incl (match-end 0) (match-beginning 0))
> > nil))))
> > (if (not end)
> > (error "Unmatched \"%s\" sequence at position %d" start-re beg)
> > (delete-region beg end)
> > (insert repl-str)))))
>
> I have few comments/questions.
>
> I tried the above function in my *scratch* buffer by writing it and then
> adding the following lines (with line numbers!):
>
> 19. (replace-between-regexp "<!--Test-->" "<!--End of Test-->" "" 1)
> 20.
> 21. Pub
> 22. <!--Test-->
> 23. Foo
> 24. <!--End of Test-->
> 25. Bar
>
> I then evaluated the function with C-j. This resulted in:
>
> 19. (replace-between-regexp "<!--Test-->" "<!--End of Test-->" "" 1)
> 20.
> 21.
> 22. Pub
> 23. nil
> 24.
> 25. Bar
>
> With the cursor on line 24. My comments/questions are:
>
> 1) I understand that the "nil" on line 23 comes from the value of
> the last item in the function list, in this case "(insert
> repl-str)". But there is also a newline character is inserted after
> "nil". But I don't want either the "nil" or the extra newline
> character!
I'm a bit lazy to answer all or suggest other things but here's are my
thoughts on that
Call it with (replace-betwe.... "<!-- End of Test" nil t)
than check before the output
(when repl-string (insert repl-string))
>
> 2) Line 21 containing "pub" is moved forward to line 22. I guess
> this is just because I did C-j at the end of line 19 or?
Check the documentation of C-j
C-h k C-j gives it to you
>
> 3) It would be nice if the cursor would return to its original
> position after running the command. I tried adding the
> "save-excursion" command after the "interactive" line in the function
>but it didn't work. The cursor still ended up on line 24.
You have to enclose it all in save-excursion
It looks like this than:
(defun replace-between-regexp (start-re end-re repl-str &optional incl)
"Replace the text between two regular expressions supplied as arguments.
With a numeric argument the regular expressions are included.
When called non interactively incl should be nil for non-inclusion and
non-nil for inclusion."
(interactive "sStart regexp: \nsEnd regexp: \nsReplace between %s
and %s with: \nP")
(save-excursion
(while (re-search-forward start-re nil t)
(let ((beg (if incl (match-beginning 0) (match-end 0)))
(end
(progn
(if (re-search-forward end-re nil t)
(if incl (match-end 0) (match-beginning 0))
nil))))
(if (not end)
(error "Unmatched \"%s\" sequence at position %d" start-re beg)
(delete-region beg end)
(when repl-str (insert repl-str)))))))
Do not use C-j than but C-x C-e
This is the documentation for C-j
Documentation:
Evaluate sexp before point; print value into current buffer.
So you will get nil after this run
C-x C-e runs
Documentation:
Evaluate sexp before point; print value in minibuffer.
With argument, print output into current buffer.
So the output will be put into the minibuffer.
>
> 4) The idea to solve my problem with a lisp function is neat and I
> guess there are many situations where one would like to add ones own
> special purpose functions to Emacs. My question is: where is the
> suitable place to put them? In my .emacs file or in a separate
> file. What are the conventions?
.emacs is change quite frequently. I don't think you self-written
functions will after development, therfor put them into an own file,
byte-compiler that file and load it from the .emacs file.
I've put all my stuff under .xemacs here and libraries I've installed
over time have found there way under ~/lib/elisp. It makes it quite
easy to update other systems such that XEmacs behaves like I want it
too.
It's IMHO nearly unavaiable that you customize your Emacs over time
and you feel like a fish on land while you are used to some things and
they are not there.
Regards
Friedrich
- Re: Newbie regexp question, (continued)
Re: Newbie regexp question, Barry Margolin, 2002/10/30
Re: Newbie regexp question, Stefan Monnier <address@hidden>, 2002/10/30
Re: Newbie regexp question, kgold, 2002/10/31
RE: Newbie regexp question, Bingham, Jay, 2002/10/30
RE: Newbie regexp question, Bingham, Jay, 2002/10/30
Re: Newbie regexp question, Paul Cohen, 2002/10/31
- Re: Newbie regexp question,
Friedrich Dominicus <=
RE: Newbie regexp question, Bingham, Jay, 2002/10/31