forked from imagej/ImageJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.txt
139 lines (131 loc) · 4.14 KB
/
Search.txt
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
// "Search"
// This macro searches for text in files contained in a directory.
// TF, 2011.02 Added support for scripts; Recordable.
str = "";
contents = true;
ignore = false;
search = "Macros";
firstLine = true;
arg = getArgument;
if (arg!="") {
args = split(arg, "|");
if (args.length==4) {
str = args[0];
contents = parseInt(args[1]);
ignore = parseInt(args[2]);
search = args[3];
}
}
extensions = newArray(".java", ".txt", ".ijm", ".js", ".py", ".rb", ".clj", ".bsh", ".html");
IJdir = getDirectory("imagej");
Dialog.create("Search");
Dialog.addString("_", str, 20);
items = newArray("Macros", "Scripts", "Java", "ImageJ folder", "Choose...");
Dialog.setInsets(2,20,0);
Dialog.addRadioButtonGroup("Search:", items, 5, 1, search);
Dialog.setInsets(0, 20, 0);
Dialog.addCheckbox("Search_contents", contents);
Dialog.addCheckbox("Ignore case", ignore);
Dialog.setInsets(10, 0, 0);
Dialog.addMessage("In the Log window, to open a file,\ndouble-click on its file path.");
Dialog.show();
str = Dialog.getString();
contents = Dialog.getCheckbox();
ignore = Dialog.getCheckbox();
search = Dialog.getRadioButton();
if (str=="")
exit("Search string is empty");
sourceExists = File.exists(IJdir+"source");
searchNames = false;
dir1=""; dir2=""; dir3="";
if (search=="Scripts") {
dir1 = getDirectory("macros");
dir2 = getDirectory("plugins");
dir3 = IJdir+"scripts/";
extensions = newArray(".js", ".py", ".rb", ".clj", ".bsh");
} else if (search=="Java") {
dir1 = getDirectory("plugins");
if (sourceExists)
dir2 = IJdir+"source"+"/";
extensions = newArray(".java");
} else if (search=="ImageJ folder") {
dir1 = getDirectory("imagej");
searchNames = true;
} else if (search=="Choose...") {
dir1 = getDirectory("Choose a Directory");
searchNames = true;
} else {
dir1 = getDirectory("macros");
dir2 = getDirectory("plugins");
extensions = newArray(".txt", ".ijm");
}
if (ignore)
str = toLowerCase(str);
count = 0;
if (dir1!="") find(dir1);
if (dir2!="") find(dir2);
if (dir3!="") find(dir3);
if (indexOf(str, "|")==-1)
return ""+str+"|"+contents+"|"+ignore+"|"+search;
exit;
function find(dir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
showProgress(i, list.length);
if (endsWith(list[i], "/"))
find(""+dir+list[i]);
else if (contents && valid(list[i])) {
s = File.openAsString(dir+list[i]);
s2 = s;
if (ignore)
s2 = toLowerCase(s);
if (indexOf(s2,str)!=-1) {
count++;
if (firstLine)
showMessageInHeader();
print("");
print(dir+list[i]);
lines = split(s, "\n");
n = 0;
for (j=0; j<lines.length; j++) {
line = lines[j];
line2 = line;
if (ignore) line2 = toLowerCase(line);
if (indexOf(line2,str)!=-1 && n<8) {
print((j+1)+": "+line);
n++;
}
} // for
} else
searchName(list[i]);
} else if (searchNames || valid(list[i]))
searchName(list[i]);
}
if (count==1)
showStatus("1 match");
else
showStatus(count+" matches");
}
function searchName(name) {
name2 = name;
if (ignore)
name2 = toLowerCase(name2);
if (indexOf(name2,str)!=-1) {
if (firstLine)
showMessageInHeader();
print("");
print(dir+name);
count++;
}
}
function valid(name) {
for (i=0; i<extensions.length; i++) {
if (endsWith(name, extensions[i]))
return true;
}
return false;
}
function showMessageInHeader() {
print("\\Heading: Double-click on a file name to open it");
firstLine = false;
}