Skip to content

Commit 445ddb1

Browse files
Add splash.gd
1 parent 50b3f01 commit 445ddb1

File tree

3 files changed

+133
-102
lines changed

3 files changed

+133
-102
lines changed

gap/splash.gd

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#############################################################################
2+
##
3+
## splash.gd
4+
## Copyright (C) 2024 Matthew Pancer
5+
##
6+
## Licensing information can be found in the README file of this package.
7+
##
8+
#############################################################################
9+
##
10+
11+
# This function is transplanted from the Digraphs package to here. The original
12+
# function was written by A. Egri-Nagy and Manuel Delgado, with some minor
13+
# modifications by J. Mitchell.
14+
15+
DeclareGlobalFunction("Splash");

gap/splash.gi

+117-102
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,133 @@
1-
# TODO file header
2-
# TODO rename file to remove "i" suffix
1+
#############################################################################
2+
##
3+
## splash.gi
4+
## Copyright (C) 2024 Matthew Pancer
5+
##
6+
## Licensing information can be found in the README file of this package.
7+
##
8+
#############################################################################
9+
##
310

4-
if not IsBound(Splash) then
5-
BindGlobal("VizViewers",
6-
["xpdf", "xdg-open", "open", "evince", "okular", "gv"]);
11+
# The contents of this file are transplanted from the Digraphs package to here.
12+
# The original function was written by A. Egri-Nagy and Manuel Delgado, with
13+
# some minor modifications by J. Mitchell.
714

8-
BindGlobal("Splash",
9-
function(arg...)
10-
local str, opt, path, dir, tdir, file, viewer, type, inn, filetype, out,
11-
engine;
15+
BindGlobal("VizViewers",
16+
["xpdf", "xdg-open", "open", "evince", "okular", "gv"]);
1217

13-
if IsEmpty(arg) then
14-
ErrorNoReturn("the must be at least 1 argument, found none");
15-
elif not IsString(arg[1]) and not IsGraphvizGraphOrDigraph(arg[1]) then
16-
ErrorFormatted("the 1st argument must be a string or ",
17-
"graphviz graph, found {}", TNAM_OBJ(arg[1]));
18-
elif IsGraphvizGraphOrDigraph(arg[1]) then
19-
arg[1] := String(arg[1]);
20-
fi;
21-
str := arg[1];
18+
InstallGlobalFunction(Splash,
19+
function(arg...)
20+
local file, str, opt, path, dir, tdir, viewer, type, inn, filetype, out,
21+
engine, i;
2222

23-
opt := rec();
24-
if IsBound(arg[2]) and IsRecord(arg[2]) then
25-
opt := arg[2];
26-
elif IsBound(arg[2]) then
27-
ErrorNoReturn("the 2nd argument must be a record,");
28-
fi;
23+
if IsEmpty(arg) then
24+
ErrorNoReturn("the must be at least 1 argument, found none");
25+
elif not IsString(arg[1]) and not IsGraphvizGraphOrDigraph(arg[1]) then
26+
ErrorFormatted("the 1st argument must be a string or ",
27+
"graphviz graph, found {}", TNAM_OBJ(arg[1]));
28+
elif IsGraphvizGraphOrDigraph(arg[1]) then
29+
file := GraphvizName(arg[1]);
30+
for i in [1 .. Length(file)] do
31+
if not IsAlphaChar(file[i]) and not IsDigitChar(file[i]) then
32+
file[i] := '_';
33+
fi;
34+
od;
35+
arg[1] := String(arg[1]);
36+
else
37+
file := "vizpicture";
38+
fi;
39+
str := arg[1];
2940

30-
path := UserHomeExpand("~/");
31-
if IsBound(opt.path) then
32-
path := opt.path;
33-
fi;
41+
opt := rec();
42+
if IsBound(arg[2]) and IsRecord(arg[2]) then
43+
opt := arg[2];
44+
elif IsBound(arg[2]) then
45+
ErrorNoReturn("the 2nd argument must be a record,");
46+
fi;
3447

35-
if IsBound(opt.directory) then
36-
if not opt.directory in DirectoryContents(path) then
37-
Exec(Concatenation("mkdir ", path, opt.directory));
38-
fi;
39-
dir := Concatenation(path, opt.directory, "/");
40-
elif IsBound(opt.path) then
41-
if not "tmp.viz" in DirectoryContents(path) then
42-
tdir := Directory(Concatenation(path, "/", "tmp.viz"));
43-
dir := Filename(tdir, "");
44-
fi;
45-
else
46-
tdir := DirectoryTemporary();
47-
dir := Filename(tdir, "");
48-
fi;
48+
path := UserHomeExpand("~/");
49+
if IsBound(opt.path) then
50+
path := opt.path;
51+
fi;
4952

50-
# TODO use the graphs name
51-
file := "vizpicture";
52-
if IsBound(opt.filename) then
53-
file := opt.filename;
53+
if IsBound(opt.directory) then
54+
if not opt.directory in DirectoryContents(path) then
55+
Exec(Concatenation("mkdir ", path, opt.directory));
5456
fi;
55-
56-
if IsBound(opt.viewer) then
57-
viewer := opt.viewer;
58-
if not IsString(viewer) then
59-
ErrorNoReturn("the option `viewer` must be a string, not an ",
60-
TNAM_OBJ(viewer));
61-
elif Filename(DirectoriesSystemPrograms(), viewer) = fail then
62-
ErrorNoReturn("the viewer \"", viewer, "\" specified in the option ",
63-
"`viewer` is not available");
64-
fi;
65-
else
66-
viewer := First(VizViewers, x ->
67-
Filename(DirectoriesSystemPrograms(), x) <> fail);
68-
if viewer = fail then
69-
ErrorNoReturn("none of the default viewers ", VizViewers,
70-
" is available, please specify an available viewer",
71-
" in the options record component `viewer`,");
72-
fi;
57+
dir := Concatenation(path, opt.directory, "/");
58+
elif IsBound(opt.path) then
59+
if not "tmp.viz" in DirectoryContents(path) then
60+
tdir := Directory(Concatenation(path, "/", "tmp.viz"));
61+
dir := Filename(tdir, "");
7362
fi;
63+
else
64+
tdir := DirectoryTemporary();
65+
dir := Filename(tdir, "");
66+
fi;
67+
68+
if IsBound(opt.filename) then
69+
file := opt.filename;
70+
fi;
7471

75-
if IsBound(opt.type) and (opt.type = "latex" or opt.type = "dot") then
76-
type := opt.type;
77-
elif Length(str) >= 6 and str{[1 .. 6]} = "%latex" then
78-
type := "latex";
79-
elif Length(str) >= 5 and str{[1 .. 5]} = "//dot" then
80-
type := "dot";
81-
else
82-
ErrorNoReturn("the component \"type\" of the 2nd argument <a record> ",
83-
" must be \"dot\" or \"latex\",");
72+
if IsBound(opt.viewer) then
73+
viewer := opt.viewer;
74+
if not IsString(viewer) then
75+
ErrorNoReturn("the option `viewer` must be a string, not an ",
76+
TNAM_OBJ(viewer));
77+
elif Filename(DirectoriesSystemPrograms(), viewer) = fail then
78+
ErrorNoReturn("the viewer \"", viewer, "\" specified in the option ",
79+
"`viewer` is not available");
8480
fi;
85-
if type = "latex" then
86-
inn := Concatenation(dir, file, ".tex");
87-
else
88-
inn := Concatenation(dir, file, ".dot");
81+
else
82+
viewer := First(VizViewers, x ->
83+
Filename(DirectoriesSystemPrograms(), x) <> fail);
84+
if viewer = fail then
85+
ErrorNoReturn("none of the default viewers ", VizViewers,
86+
" is available, please specify an available viewer",
87+
" in the options record component `viewer`,");
8988
fi;
89+
fi;
9090

91-
filetype := "pdf";
92-
if IsBound(opt.filetype) and IsString(opt.filetype) and type <> "latex" then
93-
filetype := opt.filetype;
94-
fi;
95-
out := Concatenation(dir, file, ".", filetype);
91+
if IsBound(opt.type) and (opt.type = "latex" or opt.type = "dot") then
92+
type := opt.type;
93+
elif Length(str) >= 6 and str{[1 .. 6]} = "%latex" then
94+
type := "latex";
95+
elif Length(str) >= 5 and str{[1 .. 5]} = "//dot" then
96+
type := "dot";
97+
else
98+
ErrorNoReturn("the component \"type\" of the 2nd argument <a record> ",
99+
" must be \"dot\" or \"latex\",");
100+
fi;
101+
if type = "latex" then
102+
inn := Concatenation(dir, file, ".tex");
103+
else
104+
inn := Concatenation(dir, file, ".dot");
105+
fi;
96106

97-
engine := "dot";
98-
if IsBound(opt.engine) then
99-
engine := opt.engine;
100-
if not engine in ["dot", "neato", "twopi", "circo",
101-
"fdp", "sfdp", "patchwork"] then
102-
ErrorNoReturn("the component \"engine\" of the 2nd argument ",
103-
"<a record> must be one of: \"dot\", \"neato\", ",
104-
"\"twopi\", \"circo\", \"fdp\", \"sfdp\", ",
105-
"or \"patchwork\"");
106-
fi;
107-
fi;
107+
filetype := "pdf";
108+
if IsBound(opt.filetype) and IsString(opt.filetype) and type <> "latex" then
109+
filetype := opt.filetype;
110+
fi;
111+
out := Concatenation(dir, file, ".", filetype);
108112

109-
FileString(inn, str);
110-
if type = "latex" then
111-
Exec(Concatenation("cd ", dir, ";",
112-
"pdflatex ", file, " 2>/dev/null 1>/dev/null"));
113-
else
114-
Exec(Concatenation(engine, " -T", filetype, " ", inn, " -o ", out));
113+
engine := "dot";
114+
if IsBound(opt.engine) then
115+
engine := opt.engine;
116+
if not engine in ["dot", "neato", "twopi", "circo",
117+
"fdp", "sfdp", "patchwork"] then
118+
ErrorNoReturn("the component \"engine\" of the 2nd argument ",
119+
"<a record> must be one of: \"dot\", \"neato\", ",
120+
"\"twopi\", \"circo\", \"fdp\", \"sfdp\", ",
121+
"or \"patchwork\"");
115122
fi;
116-
Exec(Concatenation(viewer, " ", out, " 2>/dev/null 1>/dev/null &"));
117-
end);
118-
fi;
123+
fi;
124+
125+
FileString(inn, str);
126+
if type = "latex" then
127+
Exec(Concatenation("cd ", dir, ";",
128+
"pdflatex ", file, " 2>/dev/null 1>/dev/null"));
129+
else
130+
Exec(Concatenation(engine, " -T", filetype, " ", inn, " -o ", out));
131+
fi;
132+
Exec(Concatenation(viewer, " ", out, " 2>/dev/null 1>/dev/null &"));
133+
end);

init.g

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
ReadPackage("graphviz", "gap/error.gd");
1212
ReadPackage("graphviz", "gap/dot.gd");
1313
ReadPackage("graphviz", "gap/gv.gd");
14+
ReadPackage("graphviz", "gap/splash.gd");

0 commit comments

Comments
 (0)