From: bolega
Subject: TERRIBLE ERROR :: Invalid search bound - wrong side of point
Date: 
Message-ID: <ebd783c0-345e-4f6d-a967-ad225dc0a9c2@y7g2000yqa.googlegroups.com>
I am doing a very simple and trivial thing that you often do in bash
using sed. Replace the end of the line by a token such as #.

I loath to use newline which is a break in emacs since it probably
does not accept \n.


I only want to replace this on one line such as the current line. I
could narrow to the line but is it really necessary ? Cant I just
specify the limits in replace-regexp ? Either there is something
seriously wrong with my understanding of emacs so I must pursue this
for the sake of learning.

I tried several variants:

(replace-regexp "$" "#" nil (line-beginning-position) (line-end-
position nil) )
(replace-regexp "$" "#" nil (line-beginning-position) (+ (line-end-
position nil) 1))


And why is a first nil needed ? Cant it be a t ?

Here is the error listing.

Debugger entered--Lisp error: (error "Invalid search bound (wrong side
of point)")
  re-search-forward("$" #<marker at 107360 in file.txt> t)
  perform-replace("$" "##" nil t nil nil nil 107360 107360)
  replace-regexp("$" "##" nil 107360 107360)
  eval((replace-regexp "$" "##" nil (point) (line-end-position nil)))
  eval-last-sexp-1(nil)
  eval-last-sexp(nil)
* call-interactively(eval-last-sexp)
  recursive-edit()

From: bolega
Subject: Re: TERRIBLE ERROR :: Invalid search bound - wrong side of point
Date: 
Message-ID: <c95ff269-6e12-4d7e-948d-b969b244d7a9@24g2000yqm.googlegroups.com>
To save trouble to myself, I searched the whole of google for the
error but no luck. One issue is the "$" which confuses any search
engine.

On Jul 2, 4:16 pm, bolega <·········@gmail.com> wrote:
> I am doing a very simple and trivial thing that you often do in bash
> using sed. Replace the end of the line by a token such as #.
>
> I loath to use newline which is a break in emacs since it probably
> does not accept \n.
>
> I only want to replace this on one line such as the current line. I
> could narrow to the line but is it really necessary ? Cant I just
> specify the limits in replace-regexp ? Either there is something
> seriously wrong with my understanding of emacs so I must pursue this
> for the sake of learning.
>
> I tried several variants:
>
> (replace-regexp "$" "#" nil (line-beginning-position) (line-end-
> position nil) )
> (replace-regexp "$" "#" nil (line-beginning-position) (+ (line-end-
> position nil) 1))
>
> And why is a first nil needed ? Cant it be a t ?
>
> Here is the error listing.
>
> Debugger entered--Lisp error: (error "Invalid search bound (wrong side
> of point)")
>   re-search-forward("$" #<marker at 107360 in file.txt> t)
>   perform-replace("$" "##" nil t nil nil nil 107360 107360)
>   replace-regexp("$" "##" nil 107360 107360)
>   eval((replace-regexp "$" "##" nil (point) (line-end-position nil)))
>   eval-last-sexp-1(nil)
>   eval-last-sexp(nil)
> * call-interactively(eval-last-sexp)
>   recursive-edit()
From: Pillsy
Subject: Re: TERRIBLE ERROR :: Invalid search bound - wrong side of point
Date: 
Message-ID: <30ad1c47-f7b3-4eaa-a374-47edff9a6831@26g2000yqk.googlegroups.com>
On Jul 2, 7:16 pm, bolega <·········@gmail.com> wrote:
[...]
> I only want to replace this on one line such as the current line. I
> could narrow to the line but is it really necessary ? Cant I just
> specify the limits in replace-regexp ? Either there is something
> seriously wrong with my understanding of emacs so I must pursue this
> for the sake of learning.

You should probably try comp.emacs or gnu.help.emacs, too, since this
group focuses almost entirely on the Common Lisp dialect of Lisp.
Nonetheless, I'll take a stab at answering your question.

> I tried several variants:

> (replace-regexp "$" "#" nil (line-beginning-position) (line-end-
> position nil) )
> (replace-regexp "$" "#" nil (line-beginning-position) (+ (line-end-
> position nil) 1))

Well, one thing that works is to capture the text of the line as a
group, like so:

(replace-regexp "^\\(.*\\)$" "\\1#"
		  nil
		  (line-beginning-position)
		  (line-end-position))

However, if you just want to stick a character onto the end of the
line, why not just move the point there and insert the character
instead of using regexps?

(save-excursion (end-of-line) (insert "#"))

HTH,
Pillsy
From: bolega
Subject: Re: TERRIBLE ERROR :: Invalid search bound - wrong side of point
Date: 
Message-ID: <025dee7e-208b-49c7-b0d9-641a75023a33@r33g2000yqn.googlegroups.com>
On Jul 2, 8:42 pm, Pillsy <·········@gmail.com> wrote:
> On Jul 2, 7:16 pm, bolega <·········@gmail.com> wrote:
> [...]
>
> > I only want to replace this on one line such as the current line. I
> > could narrow to the line but is it really necessary ? Cant I just
> > specify the limits in replace-regexp ? Either there is something
> > seriously wrong with my understanding of emacs so I must pursue this
> > for the sake of learning.
>
> You should probably try comp.emacs or gnu.help.emacs, too, since this
> group focuses almost entirely on the Common Lisp dialect of Lisp.
> Nonetheless, I'll take a stab at answering your question.
>
> > I tried several variants:
> > (replace-regexp "$" "#" nil (line-beginning-position) (line-end-
> > position nil) )
> > (replace-regexp "$" "#" nil (line-beginning-position) (+ (line-end-
> > position nil) 1))
>
> Well, one thing that works is to capture the text of the line as a
> group, like so:
>
> (replace-regexp "^\\(.*\\)$" "\\1#"
>                   nil
>                   (line-beginning-position)
>                   (line-end-position))
>
> However, if you just want to stick a character onto the end of the
> line, why not just move the point there and insert the character
> instead of using regexps?
>
> (save-excursion (end-of-line) (insert "#"))
>
> HTH,
> Pillsy

There is something really crazy going on.

When I try your first suggestion,

(replace-regexp "^\\(.*\\)$" "\\1#" nil (line-beginning-position)
(line-end-position))

I get this error:

Debugger entered--Lisp error: (error "Invalid search bound (wrong side
of point)")
  re-search-forward("^\\(.*\\)$" #<marker at 109804 in file.txt> t)
  perform-replace("^\\(.*\\)$" "\\1#" nil t nil nil nil 109703 109803)
  replace-regexp("^\\(.*\\)$" "\\1#" nil 109703 109803)
  eval((replace-regexp "^\\(.*\\)$" "\\1#" nil (line-beginning-
position) (line-end-position)))
  eval-last-sexp-1(nil)
  eval-last-sexp(nil)
* call-interactively(eval-last-sexp)


I want to understand what is going on so I learn to trace and debug an
error than be doing endless work-arounds. Predictive programming is
desired - which works the first time !!!
From: TomSW
Subject: Re: TERRIBLE ERROR :: Invalid search bound - wrong side of point
Date: 
Message-ID: <675ce6e5-7321-483a-bbc0-7a9d8211dfdd@h11g2000yqb.googlegroups.com>
On Jul 3, 1:16 am, bolega <·········@gmail.com> wrote:
> I am doing a very simple and trivial thing that you often do in bash
> using sed. Replace the end of the line by a token such as #.
>
> I loath to use newline which is a break in emacs since it probably
> does not accept \n.

It does.

> I only want to replace this on one line such as the current line. I
> could narrow to the line but is it really necessary ? Cant I just
> specify the limits in replace-regexp ? Either there is something
> seriously wrong with my understanding of emacs so I must pursue this
> for the sake of learning.

If you specify the limits of a replace operation you normally ensure
that the replacements don't add characters to the region being
changed. Otherwise there is a chance that the replacements will push
point beyond the end limit and you will get the error "Invalid search
bound". Narrowing means you don't need to worry about this.

Generally you should also avoid using replace-regexp in a program (as
explained in the documentation). Since you only want to do one
replacement, all you need is:

 (save-excursion
    (when (re-search-forward "$" nil :noerr)
      (replace-match "#")))

or even:

 (save-excursion
    (goto-char (line-end-position))
    (insert "#"))

regards,
Tom SW