Elisp: Find Matching Bracket Character

By Xah Lee. Date: . Last updated: .

Given a bracket character, how to find the character that matches it?

For example:

〔►see Unicode: Brackets, Quotes «»「」【】《》


(defun xah-get-matching-bracket (@bracket-char-string)
  "Returns a char in string form matching *BRACKET-CHAR-STRING.
 For example, if input is \"[\" returns \"]\".
This works with any unicode bracket, such as 「」【】〈〉etc.
This function uses the current syntax table to determine what's brackets and the matching char.
If the input is not a bracket, returns nil.
Version 2017-01-16"
  (let (($syntableValue (aref (syntax-table) (string-to-char @bracket-char-string))))
    (if (or
         (eq (car $syntableValue ) 4) ; syntax table, code 4 is open bracket
         (eq (car $syntableValue ) 5) ; syntax table, code 5 is close bracket
        (char-to-string (cdr $syntableValue))

;; test
(xah-get-matching-bracket "(" ) ; ")"
(xah-get-matching-bracket ")" ) ; "("
(xah-get-matching-bracket "[" ) ; "]"
(xah-get-matching-bracket "]" ) ; "["
(xah-get-matching-bracket "「" ) ; "」"
(xah-get-matching-bracket "」" ) ; "「"
(xah-get-matching-bracket "【" ) ; "】"
(xah-get-matching-bracket "】" ) ; "【"

How does it work?

For bracket characters, the matching character is stored in emacs syntax table. So, we just need to extract that info.

〔►see Elisp: Syntax Table Tutorial

Syntax table is implemented as a character table, which is implemented as a elisp vector type, but instead of integer for index, it uses character for index. (in elisp, character type is just integer too, of the character's Unicode codepoint in decimal.)

〔►see Elisp: Vector

Each element of the syntax table is a cons pair (syntax-code . matching-char).

The syntax-code is a integer that represents the syntax class. The matching-char, if not nil, is the matching character.

(info "(elisp) Syntax Table Internals")

Writing Major Mode

  1. How to Write a Emacs Major Mode for Syntax Coloring
  2. Elisp: html6-mode
  3. Elisp: Font Lock Mode Basics
  4. Elisp: How to Define Face
  5. Elisp: How to Color Comment in Major Mode
  6. Elisp: How to Write Comment Command in Major Mode
  7. Elisp: How to Write Your Own Comment Command from Scratch
  8. Elisp: How to Write Keyword Completion Command
  9. Elisp: How to Create Keymap for Major Mode
  10. Elisp: Create Abbrev and Templates for Major Mode
  11. Elisp: Text Properties
  12. Elisp: Overlay Highlighting
  13. Emacs: Lookup Google, Dictionary, Documentation
  14. Elisp: Syntax Table Tutorial

  1. Elisp: How to Name Your Major Mode
  2. Elisp: What's “feature”?
  3. Elisp: require, load, load-file, autoload, feature, Explained

Syntax Table

  1. Elisp: Syntax Table Tutorial
  2. Elisp: How to Find Syntax of a Character?
  3. Elisp: How to Modify Syntax Table Temporarily
  4. Elisp: How to Determine If Cursor is Inside String or Comment
  5. Elisp: Regex Patterns and Syntax Table
  6. Elisp: Find Matching Bracket Character
Liket it? Put $5 at patreon. Or Buy Xah Emacs Tutorial. Thanks.