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

Discussion of Emacs Lisp

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

Postby acomber » Fri Aug 10, 2012 7:30 am

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?
acomber
 
Posts: 1
Joined: Fri Aug 10, 2012 7:18 am

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

Postby spacebat » Sat Aug 18, 2012 11:28 pm

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
Parenthetically speaking, that is.
spacebat
 
Posts: 3
Joined: Sun Jan 10, 2010 2:49 am
Location: Adelaide, South Australia


Return to Emacs Lisp

Who is online

Users browsing this forum: No registered users and 0 guests

cron