6

When, while executing an emacs-lisp function, point is at the beginning of a line, how can I check whether that line is part of a block comment, inside it, or starting it?

The stupid approach, that I am doing now, is to go to the first non-whitespace character and see if its face is font-lock-comment-face or some such:

(save-excursion
  (skip-syntax-forward "\\s-")
  (let ((face (get-char-property (point) 'face)))
    (or (eq face 'font-lock-comment-face)
        (eq face 'font-lock-comment-delimiter-face)
        (eq face 'font-lock-doc-face))))

But this fails sometimes, because the function that invokes this might be run before fontification is performed.

Is there some standard way of doing this? (Please include, if you can, a link to the emacs lisp manual section that I've missed.)

1

1 Answer 1

8

As far as I know the standard way of checking if a point is within a comment is by calling syntax-ppss. See Parser state for the meaning of its return value.

In particular,

(nth 4 (syntax-ppss))

is not nil when the point is in a comment.

To determine if this is a block comment, try checking if the text at the beginning of the comment (position (nth 8 (syntax-ppss))) matches a regular expression (for example by using looking-at).

3
  • 1
    Actually, (nth 4 (syntax-ppss)) can evaluate to other values than t when inside a comment (e.g. an integer). But it will always be a non-nil value.
    – Stefan
    Dec 10, 2014 at 13:57
  • Thanks for the correction, @Stefan! I updated the answer. Dec 10, 2014 at 15:26
  • Awesome! This inspired to fix a function I use to pull up the next line to the current line: emacs.stackexchange.com/q/7519/115 Jan 18, 2015 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.