This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

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

2 posts · 5737 views

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?

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:
(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:
ELISP> (with-current-buffer "*scratch*" (goto-char (point-min)) (end-of-line))
nil
Parenthetically speaking, that is.