2 * Copyright (C) 1984-2023 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.
26 public void match_brac(char obrac, char cbrac, int forwdir, int n)
33 extern int ch_forw_get(), ch_back_get();
36 * Seek to the line containing the open bracket.
37 * This is either the top or bottom line on the screen,
38 * depending on the type of bracket.
40 pos = position((forwdir) ? TOP : BOTTOM);
41 if (pos == NULL_POSITION || ch_seek(pos))
44 error("Nothing in top line", NULL_PARG);
46 error("Nothing in bottom line", NULL_PARG);
51 * Look thru the line to find the open bracket to match.
55 if ((c = ch_forw_get()) == '\n' || c == EOI)
58 error("No bracket in top line", NULL_PARG);
60 error("No bracket in bottom line", NULL_PARG);
63 } while (c != obrac || --n > 0);
66 * Position the file just "after" the open bracket
67 * (in the direction in which we will be searching).
68 * If searching forward, we are already after the bracket.
69 * If searching backward, skip back over the open bracket.
75 * Search the file for the matching bracket.
77 chget = (forwdir) ? ch_forw_get : ch_back_get;
79 while ((c = (*chget)()) != EOI)
87 else if (c == cbrac && --nest < 0)
90 * Found the matching bracket.
91 * If searching backward, put it on the top line.
92 * If searching forward, put it on the bottom line.
94 jump_line_loc(ch_tell(), forwdir ? -1 : 1);
98 error("No matching bracket", NULL_PARG);