add a CVS snapshot, to thoroughly test on the Debian side
[alioth/jupp.git] / utag.c
1 /* $MirOS: contrib/code/jupp/utag.c,v 1.2 2008/05/13 13:08:30 tg Exp $ */
2 /*
3  *      tags file symbol lookup
4  *      Copyright
5  *              (C) 1992 Joseph H. Allen
6  *
7  *      This file is part of JOE (Joe's Own Editor)
8  */
9 #include "config.h"
10 #include "types.h"
11
12 #include <stdio.h>
13
14 #include "b.h"
15 #include "bw.h"
16 #include "main.h"
17 #include "pw.h"
18 #include "tab.h"
19 #include "ufile.h"
20 #include "usearch.h"
21 #include "utils.h"
22 #include "vs.h"
23 #include "utf8.h"
24 #include "charmap.h"
25 #include "w.h"
26
27 static int dotag(BW *bw, unsigned char *s, void *obj, int *notify)
28 {
29         unsigned char buf[512];
30         FILE *f;
31         unsigned char *t = NULL;
32
33         if (notify) {
34                 *notify = 1;
35         }
36         if (bw->b->name) {
37                 t = vsncpy(t, 0, sz(bw->b->name));
38                 t = vsncpy(sv(t), sc(":"));
39                 t = vsncpy(sv(t), sv(s));
40         }
41         f = fopen("tags", "r");
42         if (!f) {
43                 msgnw(bw->parent, US "Couldn't open tags file");
44                 vsrm(s);
45                 vsrm(t);
46                 return -1;
47         }
48         while (fgets((char *)buf, 512, f)) {
49                 int x, y, c;
50
51                 for (x = 0; buf[x] && buf[x] != ' ' && buf[x] != '\t'; ++x) ;
52                 c = buf[x];
53                 buf[x] = 0;
54                 if (!strcmp(s, buf) || (t && !strcmp(t, buf))) {
55                         buf[x] = c;
56                         while (buf[x] == ' ' || buf[x] == '\t') {
57                                 ++x;
58                         }
59                         for (y = x; buf[y] && buf[y] != ' ' && buf[y] != '\t' && buf[y] != '\n'; ++y) ;
60                         if (x != y) {
61                                 c = buf[y];
62                                 buf[y] = 0;
63                                 if (doedit(bw, vsncpy(NULL, 0, sz(buf + x)), NULL, NULL)) {
64                                         vsrm(s);
65                                         vsrm(t);
66                                         fclose(f);
67                                         return -1;
68                                 }
69                                 bw = (BW *) maint->curwin->object;
70                                 buf[y] = c;
71                                 while (buf[y] == ' ' || buf[y] == '\t') {
72                                         ++y;
73                                 }
74                                 for (x = y; buf[x] && buf[x] != '\n'; ++x) ;
75                                 buf[x] = 0;
76                                 if (x != y) {
77                                         long line = 0;
78
79                                         if (buf[y] >= '0' && buf[y] <= '9') {
80                                                 sscanf((char *)(buf + y), "%ld", &line);
81                                                 if (line >= 1) {
82                                                         int omid = mid;
83
84                                                         mid = 1;
85                                                         pline(bw->cursor, line - 1), bw->cursor->xcol = piscol(bw->cursor);
86                                                         dofollows();
87                                                         mid = omid;
88                                                 } else {
89                                                         msgnw(bw->parent, US "Invalid line number");
90                                                 }
91                                         } else {
92                                                 if (buf[y] == '/' || buf[y] == '?') {
93                                                         ++y;
94                                                         if (buf[y] == '^')
95                                                                 buf[--y] = '\\';
96                                                         for (x = y+1; buf[x] && buf[x] != '\n' && buf[x-1] != '/'; ++x);
97                                                 }
98                                                 if (buf[x - 1] == '/' || buf[x - 1] == '?') {
99                                                         --x;
100                                                         buf[x] = 0;
101                                                         if (buf[x - 1] == '$') {
102                                                                 buf[x - 1] = '\\';
103                                                                 buf[x] = '$';
104                                                                 ++x;
105                                                                 buf[x] = 0;
106                                                         }
107                                                 }
108                                                 if (x != y) {
109                                                         vsrm(s);
110                                                         vsrm(t);
111                                                         fclose(f);
112                                                         return dopfnext(bw, mksrch(vsncpy(NULL, 0, sz(buf + y)), NULL, 0, 0, -1, 0, 0), NULL);
113                                                 }
114                                         }
115                                 }
116                                 vsrm(s);
117                                 vsrm(t);
118                                 fclose(f);
119                                 return 0;
120                         }
121                 }
122         }
123         msgnw(bw->parent, US "Not found");
124         vsrm(s);
125         vsrm(t);
126         fclose(f);
127         return -1;
128 }
129
130 static B *taghist = NULL;
131
132 int utag(BW *bw)
133 {
134         BW *pbw;
135
136         pbw = wmkpw(bw->parent, US "Tag search: ", &taghist, dotag, NULL, NULL, cmplt, NULL, NULL, locale_map);
137         if (pbw && joe_isalnum_(bw->b->o.charmap,brch(bw->cursor))) {
138                 P *p = pdup(bw->cursor);
139                 P *q = pdup(p);
140                 int c;
141
142                 while (joe_isalnum_(bw->b->o.charmap,(c = prgetc(p))))
143                         /* do nothing */;
144                 if (c != NO_MORE_DATA) {
145                         pgetc(p);
146                 }
147                 pset(q, p);
148                 while (joe_isalnum_(bw->b->o.charmap,(c = pgetc(q))))
149                         /* do nothing */;
150                 if (c != NO_MORE_DATA) {
151                         prgetc(q);
152                 }
153                 binsb(pbw->cursor, bcpy(p, q));
154                 pset(pbw->cursor, pbw->b->eof);
155                 pbw->cursor->xcol = piscol(pbw->cursor);
156                 prm(p);
157                 prm(q);
158         }
159         if (pbw) {
160                 return 0;
161         } else {
162                 return -1;
163         }
164 }