etc/protocols - sync with NetBSD-8
[minix.git] / external / bsd / less / dist / mark.c
blobbb603517d6466c925ac70ef37031bed15b79ad54
1 /* $NetBSD: mark.c,v 1.3 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
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 #include "less.h"
15 extern IFILE curr_ifile;
16 extern int sc_height;
17 extern int jump_sline;
20 * A mark is an ifile (input file) plus a position within the file.
22 struct mark {
23 IFILE m_ifile;
24 struct scrpos m_scrpos;
28 * The table of marks.
29 * Each mark is identified by a lowercase or uppercase letter.
30 * The final one is lmark, for the "last mark"; addressed by the apostrophe.
32 #define NMARKS ((2*26)+1) /* a-z, A-Z, lastmark */
33 #define LASTMARK (NMARKS-1)
34 static struct mark marks[NMARKS];
37 * Initialize the mark table to show no marks are set.
39 public void
40 init_mark()
42 int i;
44 for (i = 0; i < NMARKS; i++)
45 marks[i].m_scrpos.pos = NULL_POSITION;
49 * See if a mark letter is valid (between a and z).
51 static struct mark *
52 getumark(c)
53 int c;
55 if (c >= 'a' && c <= 'z')
56 return (&marks[c-'a']);
58 if (c >= 'A' && c <= 'Z')
59 return (&marks[c-'A'+26]);
61 error("Invalid mark letter", NULL_PARG);
62 return (NULL);
66 * Get the mark structure identified by a character.
67 * The mark struct may come either from the mark table
68 * or may be constructed on the fly for certain characters like ^, $.
70 static struct mark *
71 getmark(c)
72 int c;
74 register struct mark *m;
75 static struct mark sm;
77 switch (c)
79 case '^':
81 * Beginning of the current file.
83 m = &sm;
84 m->m_scrpos.pos = ch_zero();
85 m->m_scrpos.ln = 0;
86 m->m_ifile = curr_ifile;
87 break;
88 case '$':
90 * End of the current file.
92 if (ch_end_seek())
94 error("Cannot seek to end of file", NULL_PARG);
95 return (NULL);
97 m = &sm;
98 m->m_scrpos.pos = ch_tell();
99 m->m_scrpos.ln = sc_height-1;
100 m->m_ifile = curr_ifile;
101 break;
102 case '.':
104 * Current position in the current file.
106 m = &sm;
107 get_scrpos(&m->m_scrpos);
108 m->m_ifile = curr_ifile;
109 break;
110 case '\'':
112 * The "last mark".
114 m = &marks[LASTMARK];
115 break;
116 default:
118 * Must be a user-defined mark.
120 m = getumark(c);
121 if (m == NULL)
122 break;
123 if (m->m_scrpos.pos == NULL_POSITION)
125 error("Mark not set", NULL_PARG);
126 return (NULL);
128 break;
130 return (m);
134 * Is a mark letter is invalid?
136 public int
137 badmark(c)
138 int c;
140 return (getmark(c) == NULL);
144 * Set a user-defined mark.
146 public void
147 setmark(c)
148 int c;
150 register struct mark *m;
151 struct scrpos scrpos;
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;
162 * Set lmark (the mark named by the apostrophe).
164 public void
165 lastmark()
167 struct scrpos scrpos;
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;
179 * Go to a mark.
181 public void
182 gomark(c)
183 int c;
185 register struct mark *m;
186 struct scrpos scrpos;
188 m = getmark(c);
189 if (m == NULL)
190 return;
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.
197 if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
199 m->m_ifile = curr_ifile;
200 m->m_scrpos.pos = ch_zero();
201 m->m_scrpos.ln = jump_sline;
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.)
209 scrpos = m->m_scrpos;
210 if (m->m_ifile != curr_ifile)
213 * Not in the current file; edit the correct file.
215 if (edit_ifile(m->m_ifile))
216 return;
219 jump_loc(scrpos.pos, scrpos.ln);
223 * Return the position associated with a given mark letter.
225 * We don't return which screen line the position
226 * is associated with, but this doesn't matter much,
227 * because it's always the first non-blank line on the screen.
229 public POSITION
230 markpos(c)
231 int c;
233 register struct mark *m;
235 m = getmark(c);
236 if (m == NULL)
237 return (NULL_POSITION);
239 if (m->m_ifile != curr_ifile)
241 error("Mark not in current file", NULL_PARG);
242 return (NULL_POSITION);
244 return (m->m_scrpos.pos);
248 * Clear the marks associated with a specified ifile.
250 public void
251 unmark(ifile)
252 IFILE ifile;
254 int i;
256 for (i = 0; i < NMARKS; i++)
257 if (marks[i].m_ifile == ifile)
258 marks[i].m_scrpos.pos = NULL_POSITION;