Page 1 of 1

What is the elisp idiom for do until end of buffer?

Posted: Fri Aug 10, 2012 7:30 am
by acomber
I have this function which fails with newline processing

(defun fmt-tbl ()
"format Word tables for wiki"
(interactive)
(goto-char (point-min))
(insert "{||-")
(while (search-forward "\t")
(delete-backward-char 1)
(insert "|")
)
(goto-char (point-min))
(while (search-forward "\n")
(insert "|-")
)
(goto-char (point-max))
(insert "|}")
)

I also tried this:

(while (end-of-line)
(insert "|-")
)

But that didn't work either.

It would be ok to repeatedly move to end of next line checking for end of buffer. How could I do that?

Re: What is the elisp idiom for do until end of buffer?

Posted: Sat Aug 18, 2012 11:28 pm
by spacebat
search-forward will throw an error by default if the string is not found. This will throw control out of your function which isn't catching errors. To use search-forward in a loop it is best to set the NOERROR argument to t:

Code: Select all

(while (search-forward "\t" nil t)
  ...)
The documentation for end-of-line says nothing about its return value, so its best to not use or rely on it, but it seems to always return nil:

Code: Select all

ELISP> (with-current-buffer "*scratch*" (goto-char (point-min)) (end-of-line))
nil