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