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