summaryrefslogtreecommitdiff
path: root/usr.bin/less/brac.c
blob: 0540e41de5777bcd38675824c26216e93ab24059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (C) 1984-2012  Mark Nudelman
 * Modified for use with illumos by Garrett D'Amore.
 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Less License, as specified in the README file.
 *
 * For more information, see the README file.
 */

/*
 * Routines to perform bracket matching functions.
 */

#include "less.h"
#include "position.h"

/*
 * Try to match the n-th open bracket
 *  which appears in the top displayed line (forwdir),
 * or the n-th close bracket
 *  which appears in the bottom displayed line (!forwdir).
 * The characters which serve as "open bracket" and
 * "close bracket" are given.
 */
void
match_brac(int obrac, int cbrac, int forwdir, int n)
{
	int c;
	int nest;
	off_t pos;
	int (*chget)(void);

	/*
	 * Seek to the line containing the open bracket.
	 * This is either the top or bottom line on the screen,
	 * depending on the type of bracket.
	 */
	pos = position((forwdir) ? TOP : BOTTOM);
	if (pos == -1 || ch_seek(pos)) {
		if (forwdir)
			error("Nothing in top line", NULL);
		else
			error("Nothing in bottom line", NULL);
		return;
	}

	/*
	 * Look thru the line to find the open bracket to match.
	 */
	do {
		if ((c = ch_forw_get()) == '\n' || c == EOI) {
			if (forwdir)
				error("No bracket in top line", NULL);
			else
				error("No bracket in bottom line", NULL);
			return;
		}
	} while (c != obrac || --n > 0);

	/*
	 * Position the file just "after" the open bracket
	 * (in the direction in which we will be searching).
	 * If searching forward, we are already after the bracket.
	 * If searching backward, skip back over the open bracket.
	 */
	if (!forwdir)
		(void) ch_back_get();

	/*
	 * Search the file for the matching bracket.
	 */
	chget = (forwdir) ? ch_forw_get : ch_back_get;
	nest = 0;
	while ((c = (*chget)()) != EOI) {
		if (c == obrac) {
			if (nest == INT_MAX)
				break;
			nest++;
		} else if (c == cbrac && --nest < 0) {
			/*
			 * Found the matching bracket.
			 * If searching backward, put it on the top line.
			 * If searching forward, put it on the bottom line.
			 */
			jump_line_loc(ch_tell(), forwdir ? -1 : 1);
			return;
		}
	}
	error("No matching bracket", NULL);
}