I have the following problem:
I am in a comment or the empty lines after or within the comment. I want to get to the first line of the comment.
So no matter where I am in this commented block, I want to get to the beginning of line ;; blah blah
.
M-x butterfly
;; blah blah
;; spoon
;; blah blah blah
;; swordfish
I think I have solved it, albeit clumsily:
(defun beginning-of-comment ()
(interactive)
(while
(progn
(beginning-of-line)
(looking-at (rx-to-string (or
`(: line-start (zero-or-more whitespace) (one-or-more ,comment-start))
`(: line-start (zero-or-more whitespace) line-end))))))
(previous-line))
Problem is, I cannot use the solution above, because it makes my emacs crash (and would presumably make other's emacs' with the same OS/emacs version crash too if I were to PR it).
There must be a more idiomatic, less verbose way to get to the beginning of a comment from within it.
Ninja edit: I thought of one more, but this also crashes emacs (becomes totally unresponsive):
(defun testing ()
(interactive)
(while (comment-forward) (progn (previous-line) (previous-line)))
Ps. I want this to work in as many modes as possible, so please use comment-start
, do not hardcode the comment variable.
looking-at
can be very slow, especially if there are lookaheads.