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(obrac
, cbrac
, forwdir
, n
)
38 extern int ch_forw_get(), ch_back_get();
41 * Seek to the line containing the open bracket.
42 * This is either the top or bottom line on the screen,
43 * depending on the type of bracket.
45 pos
= position((forwdir
) ? TOP
: BOTTOM
);
46 if (pos
== NULL_POSITION
|| ch_seek(pos
))
49 error("Nothing in top line", NULL_PARG
);
51 error("Nothing in bottom line", NULL_PARG
);
56 * Look thru the line to find the open bracket to match.
60 if ((c
= ch_forw_get()) == '\n' || c
== EOI
)
63 error("No bracket in top line", NULL_PARG
);
65 error("No bracket in bottom line", NULL_PARG
);
68 } while (c
!= obrac
|| --n
> 0);
71 * Position the file just "after" the open bracket
72 * (in the direction in which we will be searching).
73 * If searching forward, we are already after the bracket.
74 * If searching backward, skip back over the open bracket.
80 * Search the file for the matching bracket.
82 chget
= (forwdir
) ? ch_forw_get
: ch_back_get
;
84 while ((c
= (*chget
)()) != EOI
)
88 else if (c
== cbrac
&& --nest
< 0)
91 * Found the matching bracket.
92 * If searching backward, put it on the top line.
93 * If searching forward, put it on the bottom line.
95 jump_line_loc(ch_tell(), forwdir
? -1 : 1);
99 error("No matching bracket", NULL_PARG
);