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