Function: c-state-balance-parens-backwards

c-state-balance-parens-backwards is a byte-compiled function defined in cc-engine.el.gz.

Signature

(c-state-balance-parens-backwards HERE- HERE+ TOP)

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-engine.el.gz
(defun c-state-balance-parens-backwards (here- here+ top)
  ;; Return the position of the opening paren/brace/bracket before HERE- which
  ;; matches the outermost close p/b/b between HERE+ and TOP.  Except when
  ;; there's a macro, HERE- and HERE+ are the same.  Like this:
  ;;
  ;;	  ............................................
  ;;	  |				             |
  ;;	  (    [ ( .........#macro.. )      ( )  ]  )
  ;;	  ^		    ^	  ^			    ^
  ;;	  |		    |	  |			    |
  ;;   return		  HERE- HERE+			   TOP
  ;;
  ;; If there aren't enough opening paren/brace/brackets, return the position
  ;; of the outermost one found, or HERE- if there are none.  If there are no
  ;; closing p/b/bs between HERE+ and TOP, return HERE-.  HERE-/+ and TOP
  ;; must not be inside literals.  Only the accessible portion of the buffer
  ;; will be scanned.

  ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
  ;; `here'.  Go round the next loop each time we pass over such a ")".	 These
  ;; probably match "("s before `here-'.
  (let (pos pa ren+1 lonely-rens)
    (save-excursion
      (save-restriction
	(narrow-to-region (point-min) top) ; This can move point, sometimes.
	(setq pos here+)
	(c-safe
	  (while
	      (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
	    (setq lonely-rens (cons ren+1 lonely-rens)
		  pos ren+1)))))

      ;; PART 2: Scan back before `here-' searching for the "("s
      ;; matching/mismatching the ")"s found above. We only need to direct the
      ;; caller to scan when we've encountered unmatched right parens.
    (setq pos here-)
    (when lonely-rens
      (c-safe
	(while
	    (and lonely-rens		; actual values aren't used.
		 (setq pa (c-sc-scan-lists pos -1 1)))
	  (setq pos pa)
	  (setq lonely-rens (cdr lonely-rens)))))
    pos))