]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
Merge Herbert's bibtex patch; it doesn't introduce any new bugs and
[lyx.git] / src / frontends / controllers / tex_helpers.C
1 /**
2  * \file tex_helpers.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Herbert Voss <voss@lyx.org>
7  */
8
9 #include <config.h>
10 #include <vector.h>
11 #include <fstream>
12 #include <algorithm>
13
14 #include "gettext.h"
15
16 #include "tex_helpers.h"
17 #include "support/filetools.h"
18 #include "debug.h"
19 #include "support/lstrings.h"
20 #include "support/systemcall.h"
21 #include "support/path.h"
22 #include "support/lyxalgo.h"
23
24 using std::vector;
25 using std::sort;
26 using std::unique;
27
28 extern string user_lyxdir; // home of *Files.lst
29
30 namespace {
31
32 vector<string> listWithoutPath(vector<string> & dbase)
33 {
34         vector<string>::iterator it = dbase.begin();
35         vector<string>::iterator end = dbase.end();
36         for (; it != end; ++it) 
37                 *it = OnlyFilename(*it);
38         return dbase;
39 }
40
41 }
42
43 // build filelists of all availabe bst/cls/sty-files. done through
44 // kpsewhich and an external script, saved in *Files.lst
45 void rescanTexStyles()
46 {
47         // Run rescan in user lyx directory
48         Path p(user_lyxdir);
49         Systemcall one;
50         one.startscript(Systemcall::Wait,
51                         LibFileSearch("scripts", "TeXFiles.sh"));
52         p.pop();
53 }
54
55
56 void texhash()
57 {
58         // Run texhash in user lyx directory
59         Path p(user_lyxdir);
60
61         //path to texhash through system
62         Systemcall one;
63         one.startscript(Systemcall::Wait,"texhash"); 
64         p.pop();
65 }
66
67 string const getTexFileList(string const & filename, bool withFullPath)
68 {
69         vector<string> dbase = getVectorFromString(
70                 GetFileContents(LibFileSearch(string(),filename)), "\n");
71         lyx::eliminate_duplicates(dbase); 
72         string const str_out = withFullPath ?
73                 getStringFromVector(dbase, "\n") :
74                 getStringFromVector(listWithoutPath(dbase), "\n");
75         // everything ok?
76         if (str_out.empty())
77                 return _("Missing filelist. try Rescan");
78         return str_out;
79 }
80
81
82 string const getListOfOptions(string const & classname,
83                             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 // FIXME: why is this commented out ?
96 //              s = s.substr(0, s.find('}')+1); // format entry
97                 optionList += (s + '\n');
98             }
99         }
100         return optionList;
101 }
102
103
104 string const getTexFileFromList(string const & file, 
105                             string const & type)
106 {
107         string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
108  
109         lyxerr << "Search for classfile " << file_ << endl;
110  
111         string const lstfile = (type == "cls") ? "clsFiles.lst" : "styFiles.lst";
112         string const allClasses = GetFileContents(LibFileSearch(string(), 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         return classfile;
125 }