-
Notifications
You must be signed in to change notification settings - Fork 1
/
tags.c
210 lines (172 loc) · 4.11 KB
/
tags.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* tags.c - tag file commands
* Copyright (C) 1988-2018 Sean MacLennan
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "z.h"
/** @addtogroup zedit
* @{
*/
static char Tagfile[PATHMAX];
/* SAM Currently works for functions only */
/* Leaves the point at the char offset */
#define TAG_REGEX "[^a-zA-Z0-9_]%s(.*[0-9][0-9]*,"
#define BOOKMARKS 16 /* number of book marks */
static struct mark *Bookmrks[BOOKMARKS]; /* stack of book marks */
static int Bookmark = -1; /* current book mark */
static int Lastbook = -1; /* last bookmark */
static int set_bookmark(void)
{
Bookmark = (Bookmark + 1) & (BOOKMARKS - 1);
if (Bookmark > Lastbook) {
if (!(Bookmrks[Bookmark] = bcremark(Bbuff)))
return -1;
Lastbook = Bookmark;
} else
bmrktopnt(Bbuff, Bookmrks[Bookmark]);
return Bookmark;
}
static int get_tagfile(void)
{
if (Argp) {
Arg = 0;
return getfname("Tagfile: ", Tagfile);
}
if (access(Tagfile, F_OK) == 0)
return 0;
if (Curbuff->fname) {
char *p;
strlcpy(Tagfile, Curbuff->fname, sizeof(Tagfile) - 4);
p = strrchr(Tagfile, '/');
if (p) {
++p;
strcpy(p, "TAGS");
if (access(Tagfile, F_OK) == 0)
return 0;
}
}
strconcat(Tagfile, sizeof(Tagfile), zgetcwd(Tagfile, sizeof(Tagfile)), "TAGS", NULL);
if (access(Tagfile, F_OK) == 0)
return 0;
*Tagfile = '\0';
return getfname("Tagfile: ", Tagfile);
}
static bool tagfile_modified(zbuff_t *buff)
{
struct stat sbuf;
if (strcmp(buff->fname, Tagfile))
return true;
if (stat(Tagfile, &sbuf) || sbuf.st_mtime > buff->mtime)
return true;
return false;
}
/* May change Curbuff */
static zbuff_t *read_tagfile(void)
{
zbuff_t *buff;
if (get_tagfile())
return NULL;
/* Check if we can reuse an existing tag buffer */
buff = cfindbuff(TAGBUFF);
if (buff && tagfile_modified(buff) == false)
return buff;
buff = cmakebuff(TAGBUFF, Tagfile);
if (!buff)
return NULL;
if (zreadfile(Tagfile))
return NULL;
return buff;
}
static bool find_tag(char *word)
{
char path[PATHMAX], regstr[STRMAX], *p;
regexp_t re;
zbuff_t *buff, *save = Curbuff;
int offset;
buff = read_tagfile();
zswitchto(save);
if (!buff)
return false;
strfmt(regstr, sizeof(regstr), TAG_REGEX, word);
if (re_compile(&re, regstr, 0)) {
putpaw("regcomp failed");
return false;
}
zswitchto(buff);
btostart(Bbuff);
if (!re_step(Bbuff, &re, NULL))
goto failed;
offset = batoi();
if (!bcrsearch(Bbuff, 014)) /* C-L */
goto failed;
bmove(Bbuff, 2);
strcpy(path, Tagfile);
p = strrchr(path, '/');
if (p)
++p;
else
p = path + strlen(path);
getbword(p, sizeof(path), bistoken);
zswitchto(save);
set_bookmark();
if (findfile(path)) {
boffset(Bbuff, offset);
redisplay();
re_free(&re);
return true;
}
failed:
putpaw("No match.");
zswitchto(save);
re_free(&re);
return false;
}
void Ztag(void)
{
char tag[STRMAX];
*tag = '\0';
if (getarg("Tag: ", tag, sizeof(tag)) == 0)
find_tag(tag);
}
void Ztag_word(void)
{
char tag[STRMAX];
getbword(tag, sizeof(tag), bisword);
find_tag(tag);
}
void Zset_bookmark(void)
{
Arg = 0;
set_bookmark();
putpaw("Book Mark %d Set", Bookmark + 1);
}
void Znext_bookmark(void)
{
if (Bookmark < 0) {
putpaw("No bookmarks set.");
return;
}
if (Bookmrks[Bookmark]->mbuff != Bbuff) {
set_last_bufname(Curbuff);
wgoto(Bookmrks[Bookmark]->mbuff);
}
bpnttomrk(Bbuff, Bookmrks[Bookmark]);
Curwdo->modeflags = INVALID;
putpaw("Book Mark %d", Bookmark + 1);
if (--Bookmark < 0)
Bookmark = Lastbook;
}
/* @} */