File: mp_tags.c

package info (click to toggle)
mped 3.3.17-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,288 kB
  • ctags: 1,635
  • sloc: ansic: 13,150; sh: 393; makefile: 232; perl: 66
file content (362 lines) | stat: -rw-r--r-- 7,003 bytes parent folder | download
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*

    mp - Programmer Text Editor

    Tag management.

    Copyright (C) 1991-2005 Angel Ortega <angel@triptico.com>

    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
    of the License, 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 program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    http://www.triptico.com

*/

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "mp_core.h"
#include "mp_iface.h"
#include "mp_tags.h"
#include "mp_lang.h"
#include "mp_video.h"

/*******************
	Data
********************/

/* tag index */

static struct tag_index * _tag_index=NULL;
static int _tag_index_size=0;

/* the ctags command */
char _mpt_ctags_cmd[128]="ctags *";


/*******************
	Code
********************/

static int _tag_cmp(const void * s1, const void * s2)
/* bsearch/qsort function for tags */
{
	struct tag_index * t1;
	struct tag_index * t2;

	t1=(struct tag_index *)s1;
	t2=(struct tag_index *)s2;

	return(strcmp(t1->tag, t2->tag));
}


static void _reset_tags(void)
/* clears the tag index */
{
	int n;

	/* frees all tags */
	for(n=0;n < _tag_index_size;n++)
	{
		struct tag_index * ti=&_tag_index[n];

		if(ti->tag != NULL) free(ti->tag);
		if(ti->file != NULL) free(ti->file);
		if(ti->target != NULL) free(ti->target);
	}

	/* frees and reset the index itself */
	free(_tag_index);
	_tag_index=NULL;
	_tag_index_size=0;
}


static void _add_tag(char * tag, char * file, char * target)
/* adds a new tag to the database */
{
	struct tag_index * ti;

	/* expands */
	_tag_index_size++;

	_tag_index=realloc(_tag_index, _tag_index_size *
		sizeof(struct tag_index));

	/* stores */
	ti=&_tag_index[_tag_index_size - 1];

	ti->tag=strdup(tag);
	ti->file=strdup(file);
	ti->target=strdup(target);
}


static void _sort_tags(void)
/* sorts the tag database */
{
	if(_tag_index_size > 0)
		qsort(_tag_index, _tag_index_size,
		sizeof(struct tag_index), _tag_cmp);
}


static int _read_tags_file(int force)
/* reads and parses a tags file, filling the database */
{
	FILE * f;
	char line[4096];
	char * file;
	char * target;
	char * ptr;

	if((f=fopen("tags","r")) == NULL)
	{
		if(force)
		{
			/* no 'tags' file; try to create one */
			system(_mpt_ctags_cmd);

			f=fopen("tags","r");
		}
	}

	/* it tags does not exist, return */
	if(f == NULL)
		return(0);

	/* reset the index */
	_reset_tags();

	MP_SAVE_STATE();

	while(fgets(line,sizeof(line),f) != NULL)
	{
		/* ignore tagfile comments */
		if(line[0] == '!')
			continue;

		/* find first tab */
		if((ptr=strchr(line,'\t')) == NULL)
			continue;

		*ptr='\0';
		ptr++;
		file=ptr;

		if((ptr=strchr(file,'\t')) == NULL)
			continue;

		*ptr='\0';
		ptr++;
		target=ptr;

		/* if it's a regular expression, treat it as a string */
		if(*target == '/')
		{
			/* ignore / and ^ */
			target++; target++;

			/* ignore possible spaces */
			for(;*target==' ' || *target=='\t';target++);

			/* go on until a separator is found */
			for(ptr=target;*ptr!='$' && *ptr!='\t'
				&& *ptr!='/' && *ptr!='\\' && *ptr;ptr++);
			*ptr='\0';
		}
		else
		{
			/* it is a line number */
			for(ptr=target;*ptr>='0' && *ptr<='9';ptr++);
			*ptr='\0';
		}

		/* index the tag */
		_add_tag(line, file, target);
	}

	fclose(f);

	_sort_tags();

	MP_RESTORE_STATE();

	return(1);
}


/**
 * mpt_seek_tag - Seeks a tag
 * @tag: tag to be search
 *
 * Seeks a tag. Returns the structure defining the tag, or NULL if
 * the tag is not found.
 */
struct tag_index * mpt_seek_tag(char * tag)
{
	struct tag_index t;

	if(_tag_index_size == 0) return(NULL);

	t.tag=tag;

	return(bsearch(&t, _tag_index, _tag_index_size,
		sizeof(struct tag_index), _tag_cmp));
}


/**
 * mpt_select_tag - Selects from a list of tags given a partial string
 * @partial_tag: Partial tag string to search
 *
 * Makes a partial seek in the tag database and shows a list for
 * the user to select a tag. If no tag is found, an error message is
 * shown and NULL returned; if the user cancel on the list, NULL is
 * also returned. Otherwise, a struct tag_index is returned.
 */
struct tag_index * mpt_select_tag(char * partial_tag)
{
	int n;
	mp_txt * txt;
	mp_txt * itxt;
	char tmp[16];
	struct tag_index * ti;
	int matches=0;

	/* if 'tags' file cannot be read, it's done */
	if(! _read_tags_file(1))
		return(NULL);

	/* create the helping texts */
	txt=mp_create_sys_txt("<tags>");
	itxt=mp_create_sys_txt("<tags:i>");

	MP_SAVE_STATE();

	/* fills the text with tag and file */
	for(n=0;n < _tag_index_size;n++)
	{
		ti=&_tag_index[n];

		/* if tag is not contained, skip */
		if(strstr(ti->tag, partial_tag) == NULL)
			continue;

		/* store tag */
		mp_put_str(txt, ti->tag, 1);

		/* store file */
		mp_put_char(txt, '\t', 1);
		mp_put_str(txt, ti->file, 1);

		/* store target */
		mp_put_char(txt, '\t', 1);
		mp_put_str(txt, ti->target, 1);

		mp_put_char(txt, '\n', 1);

		/* store the tag subscript */
		snprintf(tmp, sizeof(tmp), "%d", n);
		mp_put_str(itxt, tmp, 1);
		mp_put_char(itxt, '\n', 1);

		matches++;
	}

	/* deletes the last \n */
	mp_move_left(txt);
	mp_delete_char(txt);

	ti=NULL;

	/* show the list */
	if(matches)
	{
		if((n=mpv_list(L("Tag list"), txt, 0)) != -1)
		{
			/* move to line and get tag index */
			mp_move_xy(itxt, 0, n);
			mp_get_word(itxt, tmp, sizeof(tmp));

			ti=&_tag_index[atoi(tmp)];
		}
	}
	else
		mpv_alert(L("Tag(s) not found."), partial_tag);

	mp_delete_sys_txt(txt);
	mp_delete_sys_txt(itxt);

	MP_RESTORE_STATE();

	return(ti);
}


/**
 * mpt_open_tag - Selects a tag from a list and opens its location
 * @partial_tag: Partial tag string to search
 *
 * Shows a list of tags matching @partial_tag and, if the user selects
 * one, it's opened.
 */
void mpt_open_tag(char * partial_tag)
{
	struct tag_index * ti;
	int n;

	if((ti=mpt_select_tag(partial_tag)) == NULL)
		return;

	/* open without reopening */
	mpi_open(ti->file, 0);

	/* is it a line number or a string to search? */
	if(*ti->target >= '0' && *ti->target <= '9')
	{
		n=atoi(ti->target) - 1;
			mp_move_xy(_mp_active, 0, n);
	}
	else
	{
		/* copies it into search buffer
		   to return to tag by pressing ^L */
		/* strncpy(_mpi_search_text, ti->target,
			sizeof(_mpi_search_text)); */

		mp_log("mpt_open_tag: %s\n", ti->target);

		mp_move_bof(_mp_active);
		mp_seek_plain(_mp_active, ti->target);
	}
}


void mpt_startup(void)
/* tag startup function */
{
	_read_tags_file(0);
}


void mpt_shutdown(void)
/* tag shutdown function */
{
}