Correct math and limerick.
[dragonfly.git] / contrib / less / mark.c
blobc61ce034cb299b2116a78b60b4f06b86f2131fcb
1 /*
2 * Copyright (C) 1984-2012 Mark Nudelman
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.
7 * For more information, see the README file.
8 */
11 #include "less.h"
13 extern IFILE curr_ifile;
14 extern int sc_height;
15 extern int jump_sline;
18 * A mark is an ifile (input file) plus a position within the file.
20 struct mark {
21 IFILE m_ifile;
22 struct scrpos m_scrpos;
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.
30 #define NMARKS ((2*26)+1) /* a-z, A-Z, lastmark */
31 #define LASTMARK (NMARKS-1)
32 static struct mark marks[NMARKS];
35 * Initialize the mark table to show no marks are set.
37 public void
38 init_mark()
40 int i;
42 for (i = 0; i < NMARKS; i++)
43 marks[i].m_scrpos.pos = NULL_POSITION;
47 * See if a mark letter is valid (between a and z).
49 static struct mark *
50 getumark(c)
51 int c;
53 if (c >= 'a' && c <= 'z')
54 return (&marks[c-'a']);
56 if (c >= 'A' && c <= 'Z')
57 return (&marks[c-'A'+26]);
59 error("Invalid mark letter", NULL_PARG);
60 return (NULL);
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 ^, $.
68 static struct mark *
69 getmark(c)
70 int c;
72 register struct mark *m;
73 static struct mark sm;
75 switch (c)
77 case '^':
79 * Beginning of the current file.
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 '$':
88 * End of the current file.
90 if (ch_end_seek())
92 error("Cannot seek to end of file", NULL_PARG);
93 return (NULL);
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 '.':
102 * Current position in the current file.
104 m = &sm;
105 get_scrpos(&m->m_scrpos);
106 m->m_ifile = curr_ifile;
107 break;
108 case '\'':
110 * The "last mark".
112 m = &marks[LASTMARK];
113 break;
114 default:
116 * Must be a user-defined mark.
118 m = getumark(c);
119 if (m == NULL)
120 break;
121 if (m->m_scrpos.pos == NULL_POSITION)
123 error("Mark not set", NULL_PARG);
124 return (NULL);
126 break;
128 return (m);
132 * Is a mark letter is invalid?
134 public int
135 badmark(c)
136 int c;
138 return (getmark(c) == NULL);
142 * Set a user-defined mark.
144 public void
145 setmark(c)
146 int c;
148 register struct mark *m;
149 struct scrpos scrpos;
151 m = getumark(c);
152 if (m == NULL)
153 return;
154 get_scrpos(&scrpos);
155 m->m_scrpos = scrpos;
156 m->m_ifile = curr_ifile;
160 * Set lmark (the mark named by the apostrophe).
162 public void
163 lastmark()
165 struct scrpos scrpos;
167 if (ch_getflags() & CH_HELPFILE)
168 return;
169 get_scrpos(&scrpos);
170 if (scrpos.pos == NULL_POSITION)
171 return;
172 marks[LASTMARK].m_scrpos = scrpos;
173 marks[LASTMARK].m_ifile = curr_ifile;
177 * Go to a mark.
179 public void
180 gomark(c)
181 int c;
183 register struct mark *m;
184 struct scrpos scrpos;
186 m = getmark(c);
187 if (m == NULL)
188 return;
191 * If we're trying to go to the lastmark and
192 * it has not been set to anything yet,
193 * set it to the beginning of the current file.
195 if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
197 m->m_ifile = curr_ifile;
198 m->m_scrpos.pos = ch_zero();
199 m->m_scrpos.ln = jump_sline;
203 * If we're using lmark, we must save the screen position now,
204 * because if we call edit_ifile() below, lmark will change.
205 * (We save the screen position even if we're not using lmark.)
207 scrpos = m->m_scrpos;
208 if (m->m_ifile != curr_ifile)
211 * Not in the current file; edit the correct file.
213 if (edit_ifile(m->m_ifile))
214 return;
217 jump_loc(scrpos.pos, scrpos.ln);
221 * Return the position associated with a given mark letter.
223 * We don't return which screen line the position
224 * is associated with, but this doesn't matter much,
225 * because it's always the first non-blank line on the screen.
227 public POSITION
228 markpos(c)
229 int c;
231 register struct mark *m;
233 m = getmark(c);
234 if (m == NULL)
235 return (NULL_POSITION);
237 if (m->m_ifile != curr_ifile)
239 error("Mark not in current file", NULL_PARG);
240 return (NULL_POSITION);
242 return (m->m_scrpos.pos);
246 * Clear the marks associated with a specified ifile.
248 public void
249 unmark(ifile)
250 IFILE ifile;
252 int i;
254 for (i = 0; i < NMARKS; i++)
255 if (marks[i].m_ifile == ifile)
256 marks[i].m_scrpos.pos = NULL_POSITION;