2 * Copyright (C) 1984-2015 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * Routines to perform bracket matching functions.
19 * Try to match the n-th open bracket
20 * which appears in the top displayed line (forwdir),
21 * or the n-th close bracket
22 * which appears in the bottom displayed line (!forwdir).
23 * The characters which serve as "open bracket" and
24 * "close bracket" are given.
27 match_brac(int obrac, int cbrac, int forwdir, int n)
34 extern int ch_forw_get(void), ch_back_get(void);
37 * Seek to the line containing the open bracket.
38 * This is either the top or bottom line on the screen,
39 * depending on the type of bracket.
41 pos = position((forwdir) ? TOP : BOTTOM);
42 if (pos == NULL_POSITION || ch_seek(pos))
45 error("Nothing in top line", NULL_PARG);
47 error("Nothing in bottom line", NULL_PARG);
52 * Look thru the line to find the open bracket to match.
56 if ((c = ch_forw_get()) == '\n' || c == EOI)
59 error("No bracket in top line", NULL_PARG);
61 error("No bracket in bottom line", NULL_PARG);
64 } while (c != obrac || --n > 0);
67 * Position the file just "after" the open bracket
68 * (in the direction in which we will be searching).
69 * If searching forward, we are already after the bracket.
70 * If searching backward, skip back over the open bracket.
76 * Search the file for the matching bracket.
78 chget = (forwdir) ? ch_forw_get : ch_back_get;
80 while ((c = (*chget)()) != EOI)
84 else if (c == cbrac && --nest < 0)
87 * Found the matching bracket.
88 * If searching backward, put it on the top line.
89 * If searching forward, put it on the bottom line.
91 jump_line_loc(ch_tell(), forwdir ? -1 : 1);
95 error("No matching bracket", NULL_PARG);