[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Newbie regexp question
From: |
Bingham, Jay |
Subject: |
RE: Newbie regexp question |
Date: |
Thu, 31 Oct 2002 12:11:20 -0600 |
Friedrich,
Thanks for answering Paul's questions about the replace-between-regexp function.
I have one comment regarding the what appears to be a suggestion to change the
insert logic to (when repl-string (insert repl-string)).
I do not believe that it is necessary to enclose the insert construct in the
when construct. Without the when construct if repl-string is empty nothing
gets inserted into the buffer, so it serves no purpose, the nil that magically
appeared is totally a byproduct of using the C-j to invoke the function.
Regarding Paul's question about where to put the function I agree that it is
best to put functions into a separate file and load or auto-load them into
emacs in the .emacs file.
Here is the function in a file with instructions on how to load or auto-load it.
<<repbetre.el>>
-_
J_)
C_)ingham
. HP - NonStop Austin Software & Services - Software Quality Assurance
. Austin, TX
. Language is the apparel in which your thoughts parade in public.
. Never clothe them in vulgar and shoddy attire. -Dr. George W. Crane-
-----Original Message-----
From: Friedrich Dominicus [mailto:frido@q-software-solutions.com]
Sent: Thursday, October 31, 2002 8:41 AM
To: help-gnu-emacs@gnu.org
Subject: Re: Newbie regexp question
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
_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://mail.gnu.org/mailman/listinfo/help-gnu-emacs
repbetre.el
Description: repbetre.el
- 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,
Bingham, Jay <=