In addition to the things pointed out by @muffinmad, the code doesn't record the information found by the search.
The body of the loop is
(cons .... sec-lines), which creates a new list containing the search result and whatever is in the list
sec-lines. However, this new list is thrown away.
You can either write
(setq set-lines (cons ... sec-lines)) or, simpler,
(push ... sec-lines).
Some other things:
- The arguments to
re-search-forward are wrong, typically you pass
nil as bound and
t as noerror.
- You'll need to change the last line to
(message "%s" sec-lines).
- To avoid moving the point, you can wrap everything in a
All included, the following would work:
(defun bad-line-numbers ()
"Return list of lines containing `bad'."
(let ((sec-lines ()))
(while (re-search-forward "bad" nil t)
(push (thing-at-point 'line) sec-lines))
(message "%s" sec-lines)
(things-at-line 'line) returns the full line. To find the line number (which the title indicates) you can use