0

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?

4
  • Can you post an example of an index file (short example will do), so that people can try it out and see the problem for themselves? Otherwise, you are asking people to run makeindex (or whatever it is called) on some .tex file that they would have to write in order to produce an example that they can test in Emacs. Chances are that you are going to get an answer much faster that way. Put yourself in the place of a person who might be willing to answer if the work requires five minutes extra of her/his time, but if it requires half an hour, they might pass.
    – NickD
    May 26, 2023 at 14:31
  • 1
    I updated the question. To give some context: In certain latex documents, one purposely has only a single parenthesis '(' in a specific line followed by thousands of lines of text and then a closing parenthesis ') ' But the emacs command check-parens gets stuck at the first '('. If somehow I can run check-parens over a selected region (which omits such purposeful unmatched parenthesis) it would be very useful.
    – Vikram
    May 26, 2023 at 15:57
  • 1
    As the doc for the function says: Check for unbalanced parentheses in the current buffer. More accurately, check the narrowed part of the buffer for unbalanced expressions ("sexps") in general. So select a region normally, narrow the buffer to the region with C-x n n and then M-x check-parens. When you are done, widen the buffer: C-x n w.
    – NickD
    May 26, 2023 at 17:14
  • BTW, have you tried the commands C-M-f, bound to forward-sexp and C-M-b, bound to backward-sexp in "unmached-paren" situations? You might find them useful.
    – NickD
    May 26, 2023 at 17:22

1 Answer 1

0

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-parens says:

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[1]:

C-M-f Move forward over a balanced expression (forward-sexp).

C-M-b Move backward over a balanced expression (backward-sexp).

C-M-n Move forward over a parenthetical group (forward-list).

C-M-p Move backward over a parenthetical group (backward-list).

C-M-u Move up in parenthesis structure (backward-up-list).

C-M-d Move down in parenthesis structure (down-list).

See Expressions with balanced parentheses and Moving by parens in the Emacs manual.

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.


[1] 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.