]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
888a83e6e4d6176fd5659029d2f5e8bb69200bcc
[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 #include "gettext.h"
17
18 #include "frontends/Alert.h"
19
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/lyxalgo.h"
23 #include "support/os.h"
24 #include "support/package.h"
25 #include "support/path.h"
26 #include "support/systemcall.h"
27
28 #include <boost/cregex.hpp>
29 #include <fstream>
30
31 using std::string;
32 using std::endl;
33
34 namespace lyx {
35
36 using support::bformat;
37 using support::contains;
38 using support::FileName;
39 using support::getExtension;
40 using support::getFileContents;
41 using support::getVectorFromString;
42 using support::libFileSearch;
43 using support::onlyFilename;
44 using support::package;
45 using support::Path;
46 using support::quoteName;
47 using support::split;
48 using support::Systemcall;
49 using support::token;
50
51 namespace frontend {
52
53 void rescanTexStyles()
54 {
55         // Run rescan in user lyx directory
56         Path p(package().user_support());
57         FileName const command = libFileSearch("scripts", "TeXFiles.py");
58         Systemcall one;
59         int const status = one.startscript(Systemcall::Wait,
60                         lyx::support::os::python() + ' ' +
61                         quoteName(command.toFilesystemEncoding()));
62         if (status == 0)
63                 return;
64         // FIXME UNICODE
65         Alert::error(_("Could not update TeX information"),
66                      bformat(_("The script `%s' failed."), lyx::from_utf8(command.absFilename())));
67 }
68
69
70 void texhash()
71 {
72         // Run texhash in user lyx directory
73         Path p(package().user_support());
74
75         //path to texhash through system
76         Systemcall one;
77         one.startscript(Systemcall::Wait,"texhash");
78 }
79
80
81 void getTexFileList(string const & filename, std::vector<string> & list)
82 {
83         list.clear();
84         FileName const file = libFileSearch("", filename);
85         if (file.empty())
86                 return;
87
88         list = getVectorFromString(getFileContents(file), "\n");
89
90         // Normalise paths like /foo//bar ==> /foo/bar
91         boost::RegEx regex("/{2,}");
92         std::vector<string>::iterator it  = list.begin();
93         std::vector<string>::iterator end = list.end();
94         for (; it != end; ++it) {
95                 *it = regex.Merge((*it), "/");
96         }
97
98         // remove empty items and duplicates
99         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
100         eliminate_duplicates(list);
101 }
102
103
104 string const getListOfOptions(string const & classname, string const & type)
105 {
106         string const filename = getTexFileFromList(classname,type);
107         string optionList = string();
108         std::ifstream is(filename.c_str());
109         while (is) {
110                 string s;
111                 is >> s;
112                 if (contains(s,"DeclareOption")) {
113                         s = s.substr(s.find("DeclareOption"));
114                         s = split(s,'{');               // cut front
115                         s = token(s,'}',0);             // cut end
116                         optionList += (s + '\n');
117                 }
118         }
119         return optionList;
120 }
121
122
123 string const getTexFileFromList(string const & file,
124                             string const & type)
125 {
126         string file_ = file;
127         // do we need to add the suffix?
128         if (!(getExtension(file) == type))
129                 file_ += '.' + type;
130
131         lyxerr << "Searching for file " << file_ << endl;
132
133         string lstfile;
134         if (type == "cls")
135                 lstfile = "clsFiles.lst";
136         else if (type == "sty")
137                 lstfile = "styFiles.lst";
138         else if (type == "bst")
139                 lstfile = "bstFiles.lst";
140         else if (type == "bib")
141                 lstfile = "bibFiles.lst";
142         FileName const abslstfile = libFileSearch(string(), lstfile);
143         if (abslstfile.empty()) {
144                 lyxerr << "File `'" << lstfile << "' not found." << endl;
145                 return string();
146         }
147         string const allClasses = getFileContents(abslstfile);
148         int entries = 0;
149         string classfile = token(allClasses, '\n', entries);
150         int count = 0;
151         while ((!contains(classfile, file) ||
152                 (onlyFilename(classfile) != file)) &&
153                 (++count < 1000)) {
154                 classfile = token(allClasses, '\n', ++entries);
155         }
156
157         // now we have filename with full path
158         lyxerr << "with full path: " << classfile << endl;
159
160         return classfile;
161 }
162
163 } // namespace frontend
164 } // namespace lyx