2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, 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(int obrac
, int cbrac
, int forwdir
, int n
)
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
== -1 || ch_seek(pos
)) {
43 error("Nothing in top line", NULL
);
45 error("Nothing in bottom line", NULL
);
50 * Look thru the line to find the open bracket to match.
53 if ((c
= ch_forw_get()) == '\n' || c
== EOI
) {
55 error("No bracket in top line", NULL
);
57 error("No bracket in bottom line", NULL
);
60 } while (c
!= obrac
|| --n
> 0);
63 * Position the file just "after" the open bracket
64 * (in the direction in which we will be searching).
65 * If searching forward, we are already after the bracket.
66 * If searching backward, skip back over the open bracket.
72 * Search the file for the matching bracket.
74 chget
= (forwdir
) ? ch_forw_get
: ch_back_get
;
76 while ((c
= (*chget
)()) != EOI
) {
79 } else if (c
== cbrac
&& --nest
< 0) {
81 * Found the matching bracket.
82 * If searching backward, put it on the top line.
83 * If searching forward, put it on the bottom line.
85 jump_line_loc(ch_tell(), forwdir
? -1 : 1);
89 error("No matching bracket", NULL
);