1

What is a reliable way to find the matching balanced bracket (eg: {}, [], () in C or Python), which doesn't get confused by brackets in strings, or escaped brackets for e.g.

Example:

["Foo]" ?\] 0 [1 2]]
^                  ^
Input point.      Return point.

Something like how show-paren-mode works, except as an elisp-function where you can pass in a point and get a matching point or nil when not found.

Drew
  • 75,699
  • 9
  • 109
  • 225
ideasman42
  • 8,375
  • 1
  • 28
  • 105

1 Answers1

1

Generally forward-sexp should know what to do in any given mode, so you could perhaps base it on this:

(save-excursion (ignore-errors (forward-sexp) (1- (point))))

Alternatively...

Something like how show-paren-mode works

You could use that exact same code. Look at M-x find-function RET show-paren-function and how it uses the results of (funcall show-paren-data-function), and adapt to your purposes.

show-paren-data-function is a variable defined in `paren.el'.
Its value is `show-paren--default'

  This variable may be risky if used as a file-local variable.

Documentation:
Function to find the opener/closer "near" point and its match.
The function is called with no argument and should return either nil
if there's no opener/closer near point, or a list of the form
(HERE-BEG HERE-END THERE-BEG THERE-END MISMATCH)
Where HERE-BEG..HERE-END is expected to be near point.
phils
  • 48,657
  • 3
  • 76
  • 115