File: gtags.pl

package info (click to toggle)
global 6.6.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,308 kB
  • sloc: ansic: 39,547; javascript: 4,891; sh: 4,674; perl: 811; lisp: 696; makefile: 332; python: 213; yacc: 122
file content (268 lines) | stat: -rw-r--r-- 6,728 bytes parent folder | download | duplicates (4)
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
#
# Gtags.pl --- Global facility for Nvi-1.81
#
# Copyright (c) 2001, 2002, 2008, 2010 Tama Communications Corporation
#
# This file is part of GNU GLOBAL.
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# usage:
#
#	nvi editor			command line
#	-----------------------------------------------------------
#	:perl tag qw(main)		global -x main
#	:perl tag qw(-T main)		global -Tx main
#	:perl tag qw(-r main)		global -rx main
#	:perl tag qw(-sl main)		global -slx main
#	:perl tag qw(-gi main)		global -gix main
#	:perl tag ('-go', 'int argc')	global -go 'int argc'
#	:perl tag qw(-I main)		global -Ix main
#	:perl tag qw(-P file)		global -Px file
#	:perl tag qw(-f %)		global -fx <current file>
#	:perl gozilla			gozilla +<current line> <current file>
#
# Suggested .nexrc: (If you have gtags.pl in ${HOME}/perl.)
#
# perl use lib "$ENV{HOME}/perl"
# perl require 'gtags.pl'
# map ^P :tagprev^M
# map ^N :tagnext^M
# map ^] :perl tag^M
# map ^G :perl gozilla^M
# ab gtag perl tag qw(
# ab gta perl tag qw(
# ab gt perl tag qw(
# ab gozill perl gozilla
# ab gozil perl gozilla
# ab gozi perl gozilla
# ab goz perl gozilla

#
# You need this version of GLOBAL.
#
my($supported_version) = '5.7';
#
# Quote characters
#
my($sq) = "'";
my($dq) = '"';
my($single_quote_char) = $sq . $dq . $sq . $dq . $sq;
#
# command existent check.
#
$w32 = ($^O =~ /^(ms)?(dos|win(32|nt))/i) ? 1 : 0;
$pathsep = ($w32) ? ';' : ':';
$command = '';
foreach (split(/$pathsep/, $ENV{'PATH'})) {
    if (-x "$_/global") {
        $command = "$_/global";
        last;
    }
}
#
# version check of global(1).
#
$error = '';
if ($command) {
    open(TAGS, "$command --version |");
    $_ = <TAGS>;
    chop($_);
    $version = $_;
    close(TAGS);
    my $result = version_supported($version, $supported_version);
    if ($result < 0) {
        $error = 'Global(1) not found.';
    } elsif ($result == 0) {
        $error = 'Your global(1) seems to be older version.';
    }
}
if ($error) {
    $error .= " Required GLOBAL-${supported_version} or later.";
}
sub main::tag {
    my $tagq, $tag;
    my $flag = '';
    my $end = 0;

    if ($error) {
        $curscr->Msg($error);
	return;
    }
    while ($_[0] =~ /^-/ && $end == 0) {
	my($option) = $_[0];
	if ($option !~ /^--/) {			# ignore long option
	    my($length) = length($option);
	    my($offset) = 0;
	    while ($offset < $length) {
	        my($c) = substr($option, $offset, 1);
                if ($c !~ /[-cenpquv]/) {
                    $flag .= $c;
	        }
		if ($c eq 'e') {
		    $end = 1;
		}
		$offset++;
            }
	}
        shift;
    }
    if ($_[0]) {
        $tag = $_[0];
	#
	# replace '%' with current file name.
	#
	if ($tag =~ /%/) {
            $path = $curscr->GetFileName();
	    $tag =~ s/%/$path/;
	}
    } else {
        #
        # get current position.
        #
        my($lineno, $column) = $curscr->GetCursor();
        my($line) = $curscr->GetLine($lineno);
	my($length) = length($line);
        #
        # extract the first word as a tag.
        #
	my($offset) = $column;
	while ($offset > 0 && substr($line, $offset, 1) =~ /^\w/) {
	    $offset--;
	}
	while ($offset < $length && substr($line, $offset, 1) !~ /^\w/) {
	    $offset++;
	}
	my($subline) = substr($line, $offset);
        ($tag) = $subline =~ /(\w+)/;
        if (!$tag) {
            $curscr->Msg("tag not found in current position.");
            return;
        }
	#
	# make global(1) locate tags based on the context.
	#
        $flag = ' --from-here="' . $lineno . ':' . $curscr->GetFileName() . '"';
    }
    #
    # quote tag.
    #
    my $quoted_tag;
    $length = length($tag);
    $offset = 0;
    while ($offset < $length) {
        my($c) = substr($tag, $offset, 1);
        if ($c eq "'") {
    	$quoted_tag .= $single_quote_char;
        } else {
    	$quoted_tag .= $c;
        }
        $offset++;
    }
    if ($flag !~ /f/) {
	$flag .= 'e';
    }
    $cmd = "$command --encode-path=\" \t\" -xq$flag '$quoted_tag'";
    open(TAGS, "$cmd |");
    $tagq = undef;
    while(<TAGS>) {
        my ($name, $lno, $path, $rest);
        if ((($name, $lno, $path, $rest) = /^([^ \t]+)[ \t]+(\d+)[ \t]+([^ \t]+)(.*)$/) >= 2) {
	    if (!$tagq) {
		$tagq = $curscr->TagQ($tag);
	    }
	    $path =~ s/%([0-9a-f][0-9a-f])/pack("C", hex($1))/eg;
            $tagq->Add($path, $lno, '');
        }
    } 
    close(TAGS);
    $status = $?;
    $status = $status / 256;
    if ($status == 0) {
        if (!$tagq) {
	    if ($flag =~ /f/) {
		$curscr->Msg("Tag not found in $tag");
	    } elsif ($flag =~ /P/) {
		$curscr->Msg("Path which matches to $tag not found.");
	    } elsif ($flag =~ /g/) {
		$curscr->Msg("Line which matches to $tag not found.");
	    } else {
		$curscr->Msg("Tag which matches to $tag not found.");
	    }
        } else {
	    $tagq->Push();
        }
    } elsif ($status == 2) {
        $curscr->Msg("invalid arguments. command line: $cmd");
    } elsif ($status == 3) {
        $curscr->Msg("GTAGS not found.");
    } else {
        $curscr->Msg("Global(1) failed. command line: $cmd");
    }
}
sub main::gozilla {
    if ($error) {
        $curscr->Msg($error);
	return;
    }
    my($filename) = $curscr->GetFileName();
    my($lineno, $column) = $curscr->GetCursor();
    system("gozilla +$lineno $filename");
}
#
# parse_version: parse a version and return sub-strings as an array.
#
#	i)	version number
#	r)	number array
#
sub parse_version {
    my($v) = @_;
    my($v1, $v2, $v3);

    if ($v =~ /(\d+)\.(\d+)\.(\d+)$/) {
	$v1 = $1;
	$v2 = $2;
	$v3 = $3;
    } elsif ($v =~ /(\d+)\.(\d+)$/) {
	$v1 = $1;
	$v2 = $2;
	$v3 = 0;
    } elsif ($v =~ /(\d+)$/) {
	$v1 = $1;
	$v2 = 0;
	$v3 = 0;
    } else {
	return ();	
    }
    return ($v1, $v2, $v3);
}
#
# version_supported: check whether supported version or not.
#
#	i)	$v	version
#	i)	$sv	supported version
#	r)		-1: invalid
#			1: supported
#			0: not supported
#
sub version_supported {
    my($v, $sv) = @_;
    my(@v) = parse_version($v);
    my(@s) = parse_version($sv);

    return (@v < 3) ? -1 :
	($v[0] * 1000000 + $v[1] * 1000 + $v[2] >= $s[0] * 1000000 + $s[1] * 1000 + $s[2]) ? 1 : 0;
}

1;