Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created June 23, 2014 00:03
Show Gist options
  • Save mattdeboard/b39c7c57c46e833de88e to your computer and use it in GitHub Desktop.
Save mattdeboard/b39c7c57c46e833de88e to your computer and use it in GitHub Desktop.
(defun smie-indent-comment ()
"Compute indentation of a comment."
;; Don't do it for virtual indentations. We should normally never be "in
;; front of a comment" when doing virtual-indentation anyway. And if we are
;; (as can happen in octave-mode), moving forward can lead to inf-loops.
(and (smie-indent--bolp)
(let ((pos (point)))
(save-excursion
(beginning-of-line)
(and (re-search-forward comment-start-skip (line-end-position) t)
(eq pos (or (match-end 1) (match-beginning 0))))))
(save-excursion
(forward-comment (point-max))
(skip-chars-forward " \t\r\n")
(unless
;; Don't align with a closer, since the comment is "within" the
;; closed element. Don't align with EOB either.
(save-excursion
(let ((next (funcall smie-forward-token-function)))
(or (if (zerop (length next))
(or (eobp) (eq (car (syntax-after (point))) 5)))
(rassoc next smie-closer-alist))))
;; FIXME: We assume here that smie-indent-calculate will compute the
;; indentation of the next token based on text before the comment,
;; but this is not guaranteed, so maybe we should let
;; smie-indent-calculate return some info about which buffer
;; position was used as the "indentation base" and check that this
;; base is before `pos'.
(smie-indent-calculate)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment