]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
more unicode filenames
[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         FileName const filename(getTexFileFromList(classname, type));
107         if (filename.empty())
108                 return string();
109         string optionList = string();
110         std::ifstream is(filename.toFilesystemEncoding().c_str());
111         while (is) {
112                 string s;
113                 is >> s;
114                 if (contains(s,"DeclareOption")) {
115                         s = s.substr(s.find("DeclareOption"));
116                         s = split(s,'{');               // cut front
117                         s = token(s,'}',0);             // cut end
118                         optionList += (s + '\n');
119                 }
120         }
121         return optionList;
122 }
123
124
125 string const getTexFileFromList(string const & file,
126                             string const & type)
127 {
128         string file_ = file;
129         // do we need to add the suffix?
130         if (!(getExtension(file) == type))
131                 file_ += '.' + type;
132
133         lyxerr << "Searching for file " << file_ << endl;
134
135         string lstfile;
136         if (type == "cls")
137                 lstfile = "clsFiles.lst";
138         else if (type == "sty")
139                 lstfile = "styFiles.lst";
140         else if (type == "bst")
141                 lstfile = "bstFiles.lst";
142         else if (type == "bib")
143                 lstfile = "bibFiles.lst";
144         FileName const abslstfile = libFileSearch(string(), lstfile);
145         if (abslstfile.empty()) {
146                 lyxerr << "File `'" << lstfile << "' not found." << endl;
147                 return string();
148         }
149         string const allClasses = getFileContents(abslstfile);
150         int entries = 0;
151         string classfile = token(allClasses, '\n', entries);
152         int count = 0;
153         while ((!contains(classfile, file) ||
154                 (onlyFilename(classfile) != file)) &&
155                 (++count < 1000)) {
156                 classfile = token(allClasses, '\n', ++entries);
157         }
158
159         // now we have filename with full path
160         lyxerr << "with full path: " << classfile << endl;
161
162         return classfile;
163 }
164
165 } // namespace frontend
166 } // namespace lyx