2 * \file frontend_helpers.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "frontend_helpers.h"
19 #include "frontends/FileDialog.h"
20 #include "frontends/alert.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/lyxalgo.h"
25 #include "support/os.h"
26 #include "support/Package.h"
27 #include "support/Path.h"
28 #include "support/Systemcall.h"
30 #include <boost/cregex.hpp>
42 using support::addName;
43 using support::bformat;
44 using support::FileFilterList;
45 using support::FileName;
46 using support::getExtension;
47 using support::getVectorFromString;
48 using support::libFileSearch;
49 using support::makeAbsPath;
50 using support::makeRelPath;
51 using support::onlyFilename;
52 using support::onlyPath;
53 using support::package;
54 using support::prefixIs;
55 using support::quoteName;
56 using support::removeExtension;
57 using support::Systemcall;
65 bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const {
66 return lhs.first < rhs.first;
74 vector<LanguagePair> const getLanguageData(bool character_dlg)
76 vector<LanguagePair>::size_type const size = character_dlg ?
77 languages.size() + 2 : languages.size();
79 vector<LanguagePair> langs(size);
82 langs[0].first = _("No change");
83 langs[0].second = "ignore";
84 langs[1].first = _("Reset");
85 langs[1].second = "reset";
88 vector<string>::size_type i = character_dlg ? 2 : 0;
89 for (Languages::const_iterator cit = languages.begin();
90 cit != languages.end(); ++cit) {
91 langs[i].first = _(cit->second.display());
92 langs[i].second = cit->second.lang();
96 // Don't sort "ignore" and "reset"
97 vector<LanguagePair>::iterator begin = character_dlg ?
98 langs.begin() + 2 : langs.begin();
100 std::sort(begin, langs.end(), Sorter());
106 docstring browseFile(docstring const & filename, docstring const & title,
107 FileFilterList const & filters, bool save,
108 docstring const & label1, docstring const & dir1,
109 docstring const & label2, docstring const & dir2)
111 docstring lastPath = from_ascii(".");
112 if (!filename.empty())
113 lastPath = from_utf8(onlyPath(to_utf8(filename)));
115 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
116 dlg.setButton1(label1, dir1);
117 dlg.setButton2(label2, dir2);
119 FileDialog::Result result;
122 result = dlg.save(lastPath, filters,
123 from_utf8(onlyFilename(to_utf8(filename))));
125 result = dlg.open(lastPath, filters,
126 from_utf8(onlyFilename(to_utf8(filename))));
128 return result.second;
132 docstring browseRelFile(docstring const & filename, docstring const & refpath,
133 docstring const & title, FileFilterList const & filters, bool save,
134 docstring const & label1, docstring const & dir1,
135 docstring const & label2, docstring const & dir2)
137 docstring const fname = from_utf8(makeAbsPath(
138 to_utf8(filename), to_utf8(refpath)).absFilename());
140 docstring const outname = browseFile(fname, title, filters, save,
141 label1, dir1, label2, dir2);
142 docstring const reloutname = makeRelPath(outname, refpath);
143 if (prefixIs(reloutname, from_ascii("../")))
150 docstring browseLibFile(docstring const & dir, docstring const & name,
151 docstring const & ext, docstring const & title,
152 FileFilterList const & filters)
155 docstring const label1 = _("System files|#S#s");
156 docstring const dir1 =
157 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir)));
159 docstring const label2 = _("User files|#U#u");
160 docstring const dir2 =
161 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir)));
163 docstring const result = browseFile(from_utf8(
164 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
165 title, filters, false, dir1, dir2);
167 // remove the extension if it is the default one
168 docstring noextresult;
169 if (from_utf8(getExtension(to_utf8(result))) == ext)
170 noextresult = from_utf8(removeExtension(to_utf8(result)));
172 noextresult = result;
174 // remove the directory, if it is the default one
175 docstring const file = from_utf8(onlyFilename(to_utf8(noextresult)));
176 if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
183 docstring browseDir(docstring const & pathname, docstring const & title,
184 docstring const & label1, docstring const & dir1,
185 docstring const & label2, docstring const & dir2)
187 docstring lastPath = from_ascii(".");
188 if (!pathname.empty())
189 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
191 FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
192 dlg.setButton1(label1, dir1);
193 dlg.setButton2(label2, dir2);
195 FileDialog::Result const result =
196 dlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
198 return result.second;
202 void rescanTexStyles()
204 // Run rescan in user lyx directory
205 support::Path p(package().user_support());
206 FileName const command = libFileSearch("scripts", "TeXFiles.py");
208 int const status = one.startscript(Systemcall::Wait,
209 lyx::support::os::python() + ' ' +
210 quoteName(command.toFilesystemEncoding()));
214 Alert::error(_("Could not update TeX information"),
215 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
219 void getTexFileList(string const & filename, std::vector<string> & list)
222 FileName const file = libFileSearch("", filename);
226 list = getVectorFromString(file.fileContents(), "\n");
228 // Normalise paths like /foo//bar ==> /foo/bar
229 boost::RegEx regex("/{2,}");
230 std::vector<string>::iterator it = list.begin();
231 std::vector<string>::iterator end = list.end();
232 for (; it != end; ++it)
233 *it = regex.Merge((*it), "/");
235 // remove empty items and duplicates
236 list.erase(std::remove(list.begin(), list.end(), ""), list.end());
237 eliminate_duplicates(list);
240 } // namespace frontend