Is there a way to find out the type of the surrounding parenthesis (i.e. '(', '[' or '{') around point? For example (using |
to represent point)
{ abc, | df }
should return '{', and
{ abc[ | ], 123 }
should return '['. Ideally I would like it to handle quotation marks as well.
In case anyone is curious or needs more details: my aim is to set up smart automatic spacing around :
in python using electric-spacing (also known as smart-operator). The problem is that normally (in python) :
is either the slice operator or the start of a for/if/... statement, which shouldn't be surrounded by spaces. However, within a dictionary it is something like an assignment operator, and so it should be surrounded by spaces. So I a way need to check if point is inside a dict (i.e. inside {}
), but not inside a slice operation or string within that dict (i.e. not inside []
or ""
).
Edit:
Here's the helper function I wrote, based on abo-abo's answer:
(defun enclosing-paren ()
"Return the closing parenthesis of the enclosing parens, or nil if not inside any parens."
(ignore-errors
(save-excursion
(up-list)
(char-before))))
Then then the final predicate is:
(and (not (in-string-p))
(eq (enclosing-paren) ?\}))
Edit 2:
The above function turned out to be too slow (it often caused a noticeable lag when a :
was typed). I'm using Stefan's answer instead now, which seems to be much faster.