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