When writing a major mode, it's often useful to know 'is point in a string?' 'is point in a comment?'.
Most major modes seem to attempt to parse the programming language. For example:
c-in-comment-line-prefix-pmoves point around and calls
syntax-ppssbut also checks if it's on a comment delimeter
However, this doesn't work in some cases. In org-mode buffers, comments in source blocks are not correctly detected by these approaches.
It also seems pointless, since the buffer is already showing comments highlighted.
Instead, you could simply inspect the faces at point:
(defun wh--get-faces (pos) "Get all the font faces at POS." (remq nil (list (get-char-property pos 'read-face-name) (get-char-property pos 'face) (plist-get (text-properties-at pos) 'face)))) (defun wh-string-p (pos) "Return non-nil if POS is inside a string." (memq 'font-lock-string-face (wh--get-faces pos)))
Why don't major modes do this? The buffer is already fontified, so I would expect this to be faster, more robust, and require less code.