2 * Copyright (C) 1984-2009 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines to perform bracket matching functions.
20 * Try to match the n-th open bracket
21 * which appears in the top displayed line (forwdir),
22 * or the n-th close bracket
23 * which appears in the bottom displayed line (!forwdir).
24 * The characters which serve as "open bracket" and
25 * "close bracket" are given.
28 match_brac(obrac
, cbrac
, forwdir
, n
)
39 extern int ch_forw_get(), ch_back_get();
42 * Seek to the line containing the open bracket.
43 * This is either the top or bottom line on the screen,
44 * depending on the type of bracket.
46 pos
= position((forwdir
) ? TOP
: BOTTOM
);
47 if (pos
== NULL_POSITION
|| ch_seek(pos
))
50 error("Nothing in top line", NULL_PARG
);
52 error("Nothing in bottom line", NULL_PARG
);
57 * Look thru the line to find the open bracket to match.
61 if ((c
= ch_forw_get()) == '\n' || c
== EOI
)
64 error("No bracket in top line", NULL_PARG
);
66 error("No bracket in bottom line", NULL_PARG
);
69 } while (c
!= obrac
|| --n
> 0);
72 * Position the file just "after" the open bracket
73 * (in the direction in which we will be searching).
74 * If searching forward, we are already after the bracket.
75 * If searching backward, skip back over the open bracket.
81 * Search the file for the matching bracket.
83 chget
= (forwdir
) ? ch_forw_get
: ch_back_get
;
85 while ((c
= (*chget
)()) != EOI
)
89 else if (c
== cbrac
&& --nest
< 0)
92 * Found the matching bracket.
93 * If searching backward, put it on the top line.
94 * If searching forward, put it on the bottom line.
96 jump_line_loc(ch_tell(), forwdir
? -1 : 1);
100 error("No matching bracket", NULL_PARG
);