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