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