]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
Another 49 #includes bite the dust.
[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 Voss
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/systemcall.h"
20 #include "support/path.h"
21 #include "support/lyxalgo.h"
22 #include "support/path_defines.h"
23
24 #include <boost/cregex.hpp>
25 #include <fstream>
26
27 using namespace lyx::support;
28
29 using std::vector;
30 using std::endl;
31 using std::sort;
32 using std::unique;
33
34
35 // build filelists of all availabe bst/cls/sty-files. done through
36 // kpsewhich and an external script, saved in *Files.lst
37 void rescanTexStyles()
38 {
39         // Run rescan in user lyx directory
40         Path p(user_lyxdir());
41         Systemcall one;
42         one.startscript(Systemcall::Wait,
43                         LibFileSearch("scripts", "TeXFiles.sh"));
44 }
45
46
47 void texhash()
48 {
49         // Run texhash in user lyx directory
50         Path p(user_lyxdir());
51
52         //path to texhash through system
53         Systemcall one;
54         one.startscript(Systemcall::Wait,"texhash");
55 }
56
57
58 void getTexFileList(string const & filename, std::vector<string> & list)
59 {
60         list.clear();
61         string const file = LibFileSearch("", filename);
62         if (file.empty())
63                 return;
64
65         list = getVectorFromString(GetFileContents(file), "\n");
66
67         // Normalise paths like /foo//bar ==> /foo/bar
68         boost::RegEx regex("/{2,}");
69         std::vector<string>::iterator it  = list.begin();
70         std::vector<string>::iterator end = list.end();
71         for (; it != end; ++it) {
72                 *it = STRCONV(regex.Merge(STRCONV((*it)), "/"));
73         }
74
75         lyx::eliminate_duplicates(list);
76 }
77
78
79 string const getListOfOptions(string const & classname, string const & type)
80 {
81         string const filename = getTexFileFromList(classname,type);
82         string optionList = string();
83         std::ifstream is(filename.c_str());
84         while (is) {
85                 string s;
86                 is >> s;
87                 if (contains(s,"DeclareOption")) {
88                         s = s.substr(s.find("DeclareOption"));
89                         s = split(s,'{');               // cut front
90                         s = token(s,'}',0);             // cut end
91                         optionList += (s + '\n');
92                 }
93         }
94         return optionList;
95 }
96
97
98 string const getTexFileFromList(string const & file,
99                             string const & type)
100 {
101         string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
102
103         lyxerr << "Search for classfile " << file_ << endl;
104
105         string const lstfile =
106                 ((type == "cls") ? "clsFiles.lst" : "styFiles.lst");
107         string const allClasses = GetFileContents(LibFileSearch(string(),
108                                                                 lstfile));
109         int entries = 0;
110         string classfile = token(allClasses, '\n', entries);
111         int count = 0;
112         while ((!contains(classfile, file) ||
113                 (OnlyFilename(classfile) != file)) &&
114                 (++count < 1000)) {
115                 classfile = token(allClasses, '\n', ++entries);
116         }
117
118         // now we have filename with full path
119         lyxerr << "with full path: " << classfile << endl;
120
121         return classfile;
122 }