xref: /openbsd-src/usr.bin/less/mark.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*
2  * Copyright (C) 1984-2012  Mark Nudelman
3  *
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.
6  *
7  * For more information, see the README file.
8  */
9 
10 
11 #include "less.h"
12 
13 extern IFILE curr_ifile;
14 extern int sc_height;
15 extern int jump_sline;
16 
17 /*
18  * A mark is an ifile (input file) plus a position within the file.
19  */
20 struct mark {
21 	IFILE m_ifile;
22 	struct scrpos m_scrpos;
23 };
24 
25 /*
26  * The table of marks.
27  * Each mark is identified by a lowercase or uppercase letter.
28  * The final one is lmark, for the "last mark"; addressed by the apostrophe.
29  */
30 #define	NMARKS		((2*26)+1)	/* a-z, A-Z, lastmark */
31 #define	LASTMARK	(NMARKS-1)
32 static struct mark marks[NMARKS];
33 
34 /*
35  * Initialize the mark table to show no marks are set.
36  */
37 	public void
38 init_mark()
39 {
40 	int i;
41 
42 	for (i = 0;  i < NMARKS;  i++)
43 		marks[i].m_scrpos.pos = NULL_POSITION;
44 }
45 
46 /*
47  * See if a mark letter is valid (between a and z).
48  */
49 	static struct mark *
50 getumark(c)
51 	int c;
52 {
53 	if (c >= 'a' && c <= 'z')
54 		return (&marks[c-'a']);
55 
56 	if (c >= 'A' && c <= 'Z')
57 		return (&marks[c-'A'+26]);
58 
59 	error("Invalid mark letter", NULL_PARG);
60 	return (NULL);
61 }
62 
63 /*
64  * Get the mark structure identified by a character.
65  * The mark struct may come either from the mark table
66  * or may be constructed on the fly for certain characters like ^, $.
67  */
68 	static struct mark *
69 getmark(c)
70 	int c;
71 {
72 	register struct mark *m;
73 	static struct mark sm;
74 
75 	switch (c)
76 	{
77 	case '^':
78 		/*
79 		 * Beginning of the current file.
80 		 */
81 		m = &sm;
82 		m->m_scrpos.pos = ch_zero();
83 		m->m_scrpos.ln = 0;
84 		m->m_ifile = curr_ifile;
85 		break;
86 	case '$':
87 		/*
88 		 * End of the current file.
89 		 */
90 		if (ch_end_seek())
91 		{
92 			error("Cannot seek to end of file", NULL_PARG);
93 			return (NULL);
94 		}
95 		m = &sm;
96 		m->m_scrpos.pos = ch_tell();
97 		m->m_scrpos.ln = sc_height-1;
98 		m->m_ifile = curr_ifile;
99 		break;
100 	case '.':
101 		/*
102 		 * Current position in the current file.
103 		 */
104 		m = &sm;
105 		get_scrpos(&m->m_scrpos);
106 		m->m_ifile = curr_ifile;
107 		break;
108 	case '\'':
109 		/*
110 		 * The "last mark".
111 		 */
112 		m = &marks[LASTMARK];
113 		break;
114 	default:
115 		/*
116 		 * Must be a user-defined mark.
117 		 */
118 		m = getumark(c);
119 		if (m == NULL)
120 			break;
121 		if (m->m_scrpos.pos == NULL_POSITION)
122 		{
123 			error("Mark not set", NULL_PARG);
124 			return (NULL);
125 		}
126 		break;
127 	}
128 	return (m);
129 }
130 
131 #if PIPEC
132 /*
133  * Is a mark letter is invalid?
134  */
135 	public int
136 badmark(c)
137 	int c;
138 {
139 	return (getmark(c) == NULL);
140 }
141 #endif /* PIPEC */
142 
143 /*
144  * Set a user-defined mark.
145  */
146 	public void
147 setmark(c)
148 	int c;
149 {
150 	register struct mark *m;
151 	struct scrpos scrpos;
152 
153 	m = getumark(c);
154 	if (m == NULL)
155 		return;
156 	get_scrpos(&scrpos);
157 	m->m_scrpos = scrpos;
158 	m->m_ifile = curr_ifile;
159 }
160 
161 /*
162  * Set lmark (the mark named by the apostrophe).
163  */
164 	public void
165 lastmark()
166 {
167 	struct scrpos scrpos;
168 
169 	if (ch_getflags() & CH_HELPFILE)
170 		return;
171 	get_scrpos(&scrpos);
172 	if (scrpos.pos == NULL_POSITION)
173 		return;
174 	marks[LASTMARK].m_scrpos = scrpos;
175 	marks[LASTMARK].m_ifile = curr_ifile;
176 }
177 
178 /*
179  * Go to a mark.
180  */
181 	public void
182 gomark(c)
183 	int c;
184 {
185 	register struct mark *m;
186 	struct scrpos scrpos;
187 
188 	m = getmark(c);
189 	if (m == NULL)
190 		return;
191 
192 	/*
193 	 * If we're trying to go to the lastmark and
194 	 * it has not been set to anything yet,
195 	 * set it to the beginning of the current file.
196 	 */
197 	if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
198 	{
199 		m->m_ifile = curr_ifile;
200 		m->m_scrpos.pos = ch_zero();
201 		m->m_scrpos.ln = jump_sline;
202 	}
203 
204 	/*
205 	 * If we're using lmark, we must save the screen position now,
206 	 * because if we call edit_ifile() below, lmark will change.
207 	 * (We save the screen position even if we're not using lmark.)
208 	 */
209 	scrpos = m->m_scrpos;
210 	if (m->m_ifile != curr_ifile)
211 	{
212 		/*
213 		 * Not in the current file; edit the correct file.
214 		 */
215 		if (edit_ifile(m->m_ifile))
216 			return;
217 	}
218 
219 	jump_loc(scrpos.pos, scrpos.ln);
220 }
221 
222 #if PIPEC
223 /*
224  * Return the position associated with a given mark letter.
225  *
226  * We don't return which screen line the position
227  * is associated with, but this doesn't matter much,
228  * because it's always the first non-blank line on the screen.
229  */
230 	public POSITION
231 markpos(c)
232 	int c;
233 {
234 	register struct mark *m;
235 
236 	m = getmark(c);
237 	if (m == NULL)
238 		return (NULL_POSITION);
239 
240 	if (m->m_ifile != curr_ifile)
241 	{
242 		error("Mark not in current file", NULL_PARG);
243 		return (NULL_POSITION);
244 	}
245 	return (m->m_scrpos.pos);
246 }
247 #endif /* PIPEC */
248 
249 /*
250  * Clear the marks associated with a specified ifile.
251  */
252 	public void
253 unmark(ifile)
254 	IFILE ifile;
255 {
256 	int i;
257 
258 	for (i = 0;  i < NMARKS;  i++)
259 		if (marks[i].m_ifile == ifile)
260 			marks[i].m_scrpos.pos = NULL_POSITION;
261 }
262