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