all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* elisp, replace-regexp and re-search-forward
@ 2007-10-01  7:56 Seweryn Kokot
  0 siblings, 0 replies; 7+ messages in thread
From: Seweryn Kokot @ 2007-10-01  7:56 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

I try to write a function that operate on a region and which inserts a newline
between each line, for example.
   from:
aaa
bbb
ccc
   to:
aaa

bbb

ccc
-------

My first attempt is the following function which does the job
(defun my-test (beg end)
  (interactive "r")
  (replace-regexp "\n" "\n\n" nil beg end))

however in Emacs help for replace-regexp I see that it is not good to use
replace-regexp function. Instead it is recommended to use re-search-forward and
replace-match. So I try with such a function, but it does not work.

(defun my-test-two (beg end)
  (interactive "r")
    (while (re-search-forward "\n" end t)
      (replace-match "\n\n" nil nil)))

Any idea what is wrong?

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

* RE: elisp, replace-regexp and re-search-forward
@ 2007-10-01  9:08 Bourgneuf Francois
  0 siblings, 0 replies; 7+ messages in thread
From: Bourgneuf Francois @ 2007-10-01  9:08 UTC (permalink / raw)
  To: help-gnu-emacs

 You don't need to use a regexp.
(query-replace "\n" "\n\n") will do the job
Bour9

> -----Message d'origine-----
> De : 
> help-gnu-emacs-bounces+francois.bourgneuf=groupe-mma.fr@gnu.or
> g 
> [mailto:help-gnu-emacs-bounces+francois.bourgneuf=groupe-mma.f
> r@gnu.org] De la part de Seweryn Kokot
> Envoyé : lundi 1 octobre 2007 09:57
> À : help-gnu-emacs@gnu.org
> Objet : elisp, replace-regexp and re-search-forward
> 
> Hello,
> 
> I try to write a function that operate on a region and which 
> inserts a newline
> between each line, for example.
>    from:
> aaa
> bbb
> ccc
>    to:
> aaa
> 
> bbb
> 
> ccc
> -------
> 
> My first attempt is the following function which does the job
> (defun my-test (beg end)
>   (interactive "r")
>   (replace-regexp "\n" "\n\n" nil beg end))
> 
> however in Emacs help for replace-regexp I see that it is not 
> good to use
> replace-regexp function. Instead it is recommended to use 
> re-search-forward and
> replace-match. So I try with such a function, but it does not work.
> 
> (defun my-test-two (beg end)
>   (interactive "r")
>     (while (re-search-forward "\n" end t)
>       (replace-match "\n\n" nil nil)))
> 
> Any idea what is wrong?
> 
> 
> 
> 
> _______________________________________________
> help-gnu-emacs mailing list
> help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs
> 
> 

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

* Re: elisp, replace-regexp and re-search-forward
       [not found] <mailman.1531.1191229744.18990.help-gnu-emacs@gnu.org>
@ 2007-10-01 11:36 ` weber
  2007-10-01 16:11   ` Seweryn Kokot
  0 siblings, 1 reply; 7+ messages in thread
From: weber @ 2007-10-01 11:36 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="us-ascii", Size: 1850 bytes --]

On Oct 1, 6:08 am, "Bourgneuf Francois" <francois.bourgn...@groupe-
mma.fr> wrote:
>  You don't need to use a regexp.
> (query-replace "\n" "\n\n") will do the job
> Bour9
>
> > -----Message d'origine-----
> > De :
> > help-gnu-emacs-bounces+francois.bourgneuf=groupe-mma...@gnu.or
> > g
> > [mailto:help-gnu-emacs-bounces+francois.bourgneuf=groupe-mma.f
> > r...@gnu.org] De la part de Seweryn Kokot
> > Envoyé : lundi 1 octobre 2007 09:57
> > À : help-gnu-em...@gnu.org
> > Objet : elisp, replace-regexp and re-search-forward
>
> > Hello,
>
> > I try to write a function that operate on a region and which
> > inserts a newline
> > between each line, for example.
> >    from:
> > aaa
> > bbb
> > ccc
> >    to:
> > aaa
>
> > bbb
>
> > ccc
> > -------
>
> > My first attempt is the following function which does the job
> > (defun my-test (beg end)
> >   (interactive "r")
> >   (replace-regexp "\n" "\n\n" nil beg end))
>
> > however in Emacs help for replace-regexp I see that it is not
> > good to use
> > replace-regexp function. Instead it is recommended to use
> > re-search-forward and
> > replace-match. So I try with such a function, but it does not work.
>
> > (defun my-test-two (beg end)
> >   (interactive "r")
> >     (while (re-search-forward "\n" end t)
> >       (replace-match "\n\n" nil nil)))
>
> > Any idea what is wrong?
>
> > _______________________________________________
> > help-gnu-emacs mailing list
> > help-gnu-em...@gnu.org
> >http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

Another version imitates what you would do if you were going to do it
manually (jump one line, press ret, jump one line..) :

(defun insert-lines (beg end)
  (interactive "r")
  (goto-char beg)
  (while (< (point) end)
	(insert "\n")
	(next-line 1)))

Regards,
weber

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

* Re: elisp, replace-regexp and re-search-forward
  2007-10-01 11:36 ` elisp, replace-regexp and re-search-forward weber
@ 2007-10-01 16:11   ` Seweryn Kokot
  2007-10-01 16:21     ` Bastien
       [not found]     ` <mailman.1550.1191255727.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 7+ messages in thread
From: Seweryn Kokot @ 2007-10-01 16:11 UTC (permalink / raw)
  To: help-gnu-emacs

weber <hugows@gmail.com> writes:

> Another version imitates what you would do if you were going to do it
> manually (jump one line, press ret, jump one line..) :
>
> (defun insert-lines (beg end)
>   (interactive "r")
>   (goto-char beg)
>   (while (< (point) end)
> 	(insert "\n")
> 	(next-line 1)))
>
> Regards,
> weber

thanks for another approach. However I needed to modify the function a
little. So now I have two working functions

; works
(defun insert-lines (beg end)
  (interactive "r")
  (save-excursion
	(let ((line 1)
		  (region-lines (count-lines beg end)))
	  (goto-char beg)
	  (while (< line region-lines)
		(end-of-line)
		(insert "\n")
		(forward-line)
		(setq line (+ line 1))))))

; works
(defun my-test (beg end)
  (interactive "r")
  (replace-regexp "\n" "\n\n" nil beg end))

and one which doesn't work

(defun insert-empty-lines (beg end)
  (interactive "r")
	(goto-char beg)
	(while (re-search-forward "\n" end t)
	  (replace-match "\n\n" nil nil)))

namely if I select the following region from 1 (mark at 1) to 7 (mark
after 7)

1
2
3
4
5
6
7

I get something like this

1

2

3

4

5
6
7

(note that there is no \n after 5 and 6). It seems that the replacement
doesn't reach the end bound which was after number 7.  Any ideas what is
wrong?

regards,
-- 
Seweryn Kokot

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

* Re: elisp, replace-regexp and re-search-forward
  2007-10-01 16:11   ` Seweryn Kokot
@ 2007-10-01 16:21     ` Bastien
       [not found]     ` <mailman.1550.1191255727.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 7+ messages in thread
From: Bastien @ 2007-10-01 16:21 UTC (permalink / raw)
  To: Seweryn Kokot; +Cc: help-gnu-emacs

Seweryn Kokot <s.kokot@po.opole.pl> writes:

> and one which doesn't work
>
> (defun insert-empty-lines (beg end)
>   (interactive "r")
> 	(goto-char beg)
> 	(while (re-search-forward "\n" end t)
> 	  (replace-match "\n\n" nil nil)))

Since your modifying the text within the region, the region end moves.
You should perhaps use markers to track these moves:

(defun insert-empty-lines (beg end)
  (interactive "r")
  (let* ((end-marker (make-marker))
	 (end-marker (set-marker endm end)))
    (save-excursion
      (goto-char beg)
      (while (re-search-forward "\n" end-marker t)
	(replace-match "\n\n" nil nil)))))

See (info "(elisp)Markers")

-- 
Bastien

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

* Re: elisp, replace-regexp and re-search-forward
       [not found]     ` <mailman.1550.1191255727.18990.help-gnu-emacs@gnu.org>
@ 2007-10-01 20:03       ` weber
  2007-10-01 20:06       ` weber
  1 sibling, 0 replies; 7+ messages in thread
From: weber @ 2007-10-01 20:03 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 1, 1:21 pm, Bastien <b...@altern.org> wrote:
> Seweryn Kokot <s.ko...@po.opole.pl> writes:
> > and one which doesn't work
>
> > (defun insert-empty-lines (beg end)
> >   (interactive "r")
> >    (goto-char beg)
> >    (while (re-search-forward "\n" end t)
> >      (replace-match "\n\n" nil nil)))
>
> Since your modifying the text within the region, the region end moves.
> You should perhaps use markers to track these moves:
>
> (defun insert-empty-lines (beg end)
>   (interactive "r")
>   (let* ((end-marker (make-marker))
>          (end-marker (set-marker endm end)))
>     (save-excursion
>       (goto-char beg)
>       (while (re-search-forward "\n" end-marker t)
>         (replace-match "\n\n" nil nil)))))
>
> See (info "(elisp)Markers")
>
> --
> Bastien

I had totally missed that. Please forgive me for posting the snippet
of malfunctioning code.
-weber

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

* Re: elisp, replace-regexp and re-search-forward
       [not found]     ` <mailman.1550.1191255727.18990.help-gnu-emacs@gnu.org>
  2007-10-01 20:03       ` weber
@ 2007-10-01 20:06       ` weber
  1 sibling, 0 replies; 7+ messages in thread
From: weber @ 2007-10-01 20:06 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 1, 1:21 pm, Bastien <b...@altern.org> wrote:
> Seweryn Kokot <s.ko...@po.opole.pl> writes:
> > and one which doesn't work
>
> > (defun insert-empty-lines (beg end)
> >   (interactive "r")
> >    (goto-char beg)
> >    (while (re-search-forward "\n" end t)
> >      (replace-match "\n\n" nil nil)))
>
> Since your modifying the text within the region, the region end moves.
> You should perhaps use markers to track these moves:
>
> (defun insert-empty-lines (beg end)
>   (interactive "r")
>   (let* ((end-marker (make-marker))
>          (end-marker (set-marker endm end)))
>     (save-excursion
>       (goto-char beg)
>       (while (re-search-forward "\n" end-marker t)
>         (replace-match "\n\n" nil nil)))))
>
> See (info "(elisp)Markers")
>
> --
> Bastien

I was testing that here and it looks like the first "end-marker"
should be "endm".
Cheers!
weber

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

end of thread, other threads:[~2007-10-01 20:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1531.1191229744.18990.help-gnu-emacs@gnu.org>
2007-10-01 11:36 ` elisp, replace-regexp and re-search-forward weber
2007-10-01 16:11   ` Seweryn Kokot
2007-10-01 16:21     ` Bastien
     [not found]     ` <mailman.1550.1191255727.18990.help-gnu-emacs@gnu.org>
2007-10-01 20:03       ` weber
2007-10-01 20:06       ` weber
2007-10-01  9:08 Bourgneuf Francois
  -- strict thread matches above, loose matches on Subject: below --
2007-10-01  7:56 Seweryn Kokot

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.