Function: lua-find-matching-token-word
lua-find-matching-token-word is a byte-compiled function defined in
lua-mode.el.gz.
Signature
(lua-find-matching-token-word TOKEN &optional DIRECTION)
Documentation
Find matching open- or close-token for TOKEN in DIRECTION.
Point has to be exactly at the beginning of TOKEN, e.g. with | being point
{{ }|} -- (lua-find-matching-token-word "}" 'backward) will return
-- the first {
{{ |}} -- (lua-find-matching-token-word "}" 'backward) will find
-- the second {.
DIRECTION has to be either 'forward or 'backward.
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/lua-mode.el.gz
(defun lua-find-matching-token-word (token &optional direction)
"Find matching open- or close-token for TOKEN in DIRECTION.
Point has to be exactly at the beginning of TOKEN, e.g. with | being
point
{{ }|} -- (lua-find-matching-token-word \"}\" \\='backward) will return
-- the first {
{{ |}} -- (lua-find-matching-token-word \"}\" \\='backward) will find
-- the second {.
DIRECTION has to be either \\='forward or \\='backward."
(let* ((token-info (lua-get-block-token-info token))
(match-type (lua-get-token-type token-info))
;; If we are on a middle token, go backwards. If it is a
;; middle or open, go forwards
(search-direction (or direction
(if (or (eq match-type 'open)
(eq match-type 'middle-or-open))
'forward
'backward)
'backward))
(match (lua-get-token-match-re token-info search-direction))
maybe-found-pos)
;; If we are searching forward from the token at the current point
;; (i.e. for a closing token), need to step one character forward
;; first, or the regexp will match the opening token.
(when (eq search-direction 'forward) (forward-char 1))
(catch 'found
;; If we are attempting to find a matching token for a terminating
;; token (i.e. a token that starts a statement when searching
;; back, or a token that ends a statement when searching forward),
;; then we don't need to look any further.
(when (or (and (eq search-direction 'forward)
(eq match-type 'close))
(and (eq search-direction 'backward)
(eq match-type 'open)))
(throw 'found nil))
(while (lua-find-regexp search-direction lua-indentation-modifier-regexp)
;; Have we found a valid matching token?
(let* ((found-token (match-string 0))
(found-pos (match-beginning 0))
(found-type (lua-get-token-type
(lua-get-block-token-info found-token))))
(if (not (and match (string-match match found-token)))
;; No - then there is a nested block. If we were looking
;; for a block begin token, found-token must be a block
;; end token; likewise, if we were looking for a block end
;; token, found-token must be a block begin token,
;; otherwise there is a grammatical error in the code.
(unless (and (or (eq match-type 'middle)
(eq found-type 'middle)
(eq match-type 'middle-or-open)
(eq found-type 'middle-or-open)
(eq match-type found-type))
(progn
(goto-char found-pos)
(lua-find-matching-token-word
found-token search-direction)))
(when maybe-found-pos
(goto-char maybe-found-pos)
(throw 'found maybe-found-pos)))
;; Yes.
;; If it is a not a middle kind, report the location
(unless (or (eq found-type 'middle)
(eq found-type 'middle-or-open))
(throw 'found found-pos))
;; If it is a middle-or-open type, record location, but keep
;; searching. If we fail to complete the search, we'll
;; report the location
(when (eq found-type 'middle-or-open)
(setq maybe-found-pos found-pos))
;; Cannot use tail recursion. Too much nesting on long
;; chains of if/elseif. Will reset variables instead.
(setq token found-token)
(setq token-info (lua-get-block-token-info token))
(setq match (lua-get-token-match-re token-info search-direction))
(setq match-type (lua-get-token-type token-info)))))
maybe-found-pos)))