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()
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 !!!
Predictive programming is
desired - which works the first time !!!
I take this to mean you find it important to construct
a proper model of the situation before you implement.
Am i mistaken?
thi
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