Update less from version 444 to 458 on the vendor branch
[dragonfly.git] / contrib / less / mark.c
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 /*
132  * Is a mark letter is invalid?
133  */
134         public int
135 badmark(c)
136         int c;
137 {
138         return (getmark(c) == NULL);
139 }
140
141 /*
142  * Set a user-defined mark.
143  */
144         public void
145 setmark(c)
146         int c;
147 {
148         register struct mark *m;
149         struct scrpos scrpos;
150
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;
157 }
158
159 /*
160  * Set lmark (the mark named by the apostrophe).
161  */
162         public void
163 lastmark()
164 {
165         struct scrpos scrpos;
166
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;
174 }
175
176 /*
177  * Go to a mark.
178  */
179         public void
180 gomark(c)
181         int c;
182 {
183         register struct mark *m;
184         struct scrpos scrpos;
185
186         m = getmark(c);
187         if (m == NULL)
188                 return;
189
190         /*
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.
194          */
195         if (m == &marks[LASTMARK] && m->m_scrpos.pos == NULL_POSITION)
196         {
197                 m->m_ifile = curr_ifile;
198                 m->m_scrpos.pos = ch_zero();
199                 m->m_scrpos.ln = jump_sline;
200         }
201
202         /*
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.)
206          */
207         scrpos = m->m_scrpos;
208         if (m->m_ifile != curr_ifile)
209         {
210                 /*
211                  * Not in the current file; edit the correct file.
212                  */
213                 if (edit_ifile(m->m_ifile))
214                         return;
215         }
216
217         jump_loc(scrpos.pos, scrpos.ln);
218 }
219
220 /*
221  * Return the position associated with a given mark letter.
222  *
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.
226  */
227         public POSITION
228 markpos(c)
229         int c;
230 {
231         register struct mark *m;
232
233         m = getmark(c);
234         if (m == NULL)
235                 return (NULL_POSITION);
236
237         if (m->m_ifile != curr_ifile)
238         {
239                 error("Mark not in current file", NULL_PARG);
240                 return (NULL_POSITION);
241         }
242         return (m->m_scrpos.pos);
243 }
244
245 /*
246  * Clear the marks associated with a specified ifile.
247  */
248         public void
249 unmark(ifile)
250         IFILE ifile;
251 {
252         int i;
253
254         for (i = 0;  i < NMARKS;  i++)
255                 if (marks[i].m_ifile == ifile)
256                         marks[i].m_scrpos.pos = NULL_POSITION;
257 }