Emacs has a command check-parens to check for unbalanced parenthesis in a buffer. But I want to check for unbalanced parenthesis in a selected region (not the entire buffer) e.g. a region selected with the mouse. How do I do this?
You can use
check-parens on a region by narrowing to that region: select a region normally (either with the mouse or by marking one end with
C-<space> and moving to the other end) and then narrow to that region with
C-x n n. That makes the rest of the buffer invisible and
check-parens only operates on the visible portion of the buffer, so you can invoke it with
M-x check-parens to do its thing. You can then widen the buffer with
C-x n w. The doc string of
Check for unbalanced parentheses in the current buffer. More accurately, check the narrowed part of the buffer for unbalanced expressions ("sexps") in general. This is done according to the current syntax table and will find unbalanced brackets or quotes as appropriate. (See Info node ‘(emacs)Parentheses’.) If imbalance is found, an error is signaled and point is left at the first unbalanced character.
Another alternative is it use
sexp motion commands to skip over a balanced "sexp" (that stands for
S-expression, a construct in Lisp - including Emacs Lisp - where these commands are most often used, but they are available everywhere: they use the
syntax table of the buffer in question and they are often the best way to deal with matched pairs of delimiters. You can move up, down and across such matched groups of parens:
C-M-fMove forward over a balanced expression (
C-M-bMove backward over a balanced expression (
C-M-nMove forward over a parenthetical group (
C-M-pMove backward over a parenthetical group (
C-M-uMove up in parenthesis structure (
C-M-dMove down in parenthesis structure (
It's worth reading the whole
Commands for Editing with Parentheses that contains the subsections above. You can read it on the web or you can use Info (which I highly recommend you familiarize yourself with, if you are not familiar with it already):
C-h i g(emacs)m Parentheses or even
M-: (info "(emacs)Parentheses") RET.
 BTW, AFAICS what you are describing in the question is a matched set of parens - they just happen to be separated by thousands of lines - is that correct? If so, the motion commands above should work fine, as should
check-parens. The commands that blink a matching paren are more limited IIRC.