]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
Generate python code in GraphicsConverter.C, replace convertDefault.sh by convertDefa...
[lyx.git] / src / frontends / controllers / tex_helpers.C
1 /**
2  * \file tex_helpers.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Herbert Voß
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "tex_helpers.h"
14
15 #include "debug.h"
16
17 #include "support/filetools.h"
18 #include "support/lstrings.h"
19 #include "support/lyxalgo.h"
20 #include "support/package.h"
21 #include "support/path.h"
22 #include "support/systemcall.h"
23
24 #include <boost/cregex.hpp>
25 #include <fstream>
26
27 using std::string;
28 using std::endl;
29
30 namespace lyx {
31
32 using support::contains;
33 using support::getExtension;
34 using support::getFileContents;
35 using support::getVectorFromString;
36 using support::libFileSearch;
37 using support::onlyFilename;
38 using support::package;
39 using support::Path;
40 using support::quoteName;
41 using support::split;
42 using support::Systemcall;
43 using support::token;
44
45 namespace frontend {
46
47 // build filelists of all availabe bst/cls/sty-files. done through
48 // kpsewhich and an external script, saved in *Files.lst
49 void rescanTexStyles()
50 {
51         // Run rescan in user lyx directory
52         Path p(package().user_support());
53         Systemcall one;
54         one.startscript(Systemcall::Wait,
55                         "sh " +
56                         quoteName(libFileSearch("scripts", "TeXFiles.sh")));
57         // To be changed to
58         //              "python " +
59         //              quoteName(libFileSearch("scripts", "TeXFiles.py")));
60 }
61
62
63 void texhash()
64 {
65         // Run texhash in user lyx directory
66         Path p(package().user_support());
67
68         //path to texhash through system
69         Systemcall one;
70         one.startscript(Systemcall::Wait,"texhash");
71 }
72
73
74 void getTexFileList(string const & filename, std::vector<string> & list)
75 {
76         list.clear();
77         string const file = libFileSearch("", filename);
78         if (file.empty())
79                 return;
80
81         list = getVectorFromString(getFileContents(file), "\n");
82
83         // Normalise paths like /foo//bar ==> /foo/bar
84         boost::RegEx regex("/{2,}");
85         std::vector<string>::iterator it  = list.begin();
86         std::vector<string>::iterator end = list.end();
87         for (; it != end; ++it) {
88                 *it = regex.Merge((*it), "/");
89         }
90
91         // remove empty items and duplicates
92         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
93         eliminate_duplicates(list);
94 }
95
96
97 string const getListOfOptions(string const & classname, string const & type)
98 {
99         string const filename = getTexFileFromList(classname,type);
100         string optionList = string();
101         std::ifstream is(filename.c_str());
102         while (is) {
103                 string s;
104                 is >> s;
105                 if (contains(s,"DeclareOption")) {
106                         s = s.substr(s.find("DeclareOption"));
107                         s = split(s,'{');               // cut front
108                         s = token(s,'}',0);             // cut end
109                         optionList += (s + '\n');
110                 }
111         }
112         return optionList;
113 }
114
115
116 string const getTexFileFromList(string const & file,
117                             string const & type)
118 {
119         string file_ = file;
120         // do we need to add the suffix?
121         if (!(getExtension(file) == type))
122                 file_ += '.' + type;
123
124         lyxerr << "Searching for file " << file_ << endl;
125
126         string lstfile;
127         if (type == "cls")
128                 lstfile = "clsFiles.lst";
129         else if (type == "sty")
130                 lstfile = "styFiles.lst";
131         else if (type == "bst")
132                 lstfile = "bstFiles.lst";
133         else if (type == "bib")
134                 lstfile = "bibFiles.lst";
135         string const allClasses = getFileContents(libFileSearch(string(),
136                                                                 lstfile));
137         int entries = 0;
138         string classfile = token(allClasses, '\n', entries);
139         int count = 0;
140         while ((!contains(classfile, file) ||
141                 (onlyFilename(classfile) != file)) &&
142                 (++count < 1000)) {
143                 classfile = token(allClasses, '\n', ++entries);
144         }
145
146         // now we have filename with full path
147         lyxerr << "with full path: " << classfile << endl;
148
149         return classfile;
150 }
151
152 } // namespace frontend
153 } // namespace lyx