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>
43 using support::addName;
44 using support::bformat;
45 using support::FileFilterList;
46 using support::FileName;
47 using support::getExtension;
48 using support::getFileContents;
49 using support::getVectorFromString;
50 using support::libFileSearch;
51 using support::makeAbsPath;
52 using support::makeRelPath;
53 using support::onlyFilename;
54 using support::onlyPath;
55 using support::package;
56 using support::prefixIs;
57 using support::quoteName;
58 using support::removeExtension;
59 using support::Systemcall;
67 bool operator()(LanguagePair const & lhs, LanguagePair const & rhs) const {
68 return lhs.first < rhs.first;
76 vector<LanguagePair> const getLanguageData(bool character_dlg)
78 vector<LanguagePair>::size_type const size = character_dlg ?
79 languages.size() + 2 : languages.size();
81 vector<LanguagePair> langs(size);
84 langs[0].first = _("No change");
85 langs[0].second = "ignore";
86 langs[1].first = _("Reset");
87 langs[1].second = "reset";
90 vector<string>::size_type i = character_dlg ? 2 : 0;
91 for (Languages::const_iterator cit = languages.begin();
92 cit != languages.end(); ++cit) {
93 langs[i].first = _(cit->second.display());
94 langs[i].second = cit->second.lang();
98 // Don't sort "ignore" and "reset"
99 vector<LanguagePair>::iterator begin = character_dlg ?
100 langs.begin() + 2 : langs.begin();
102 std::sort(begin, langs.end(), Sorter());
108 docstring const browseFile(docstring const & filename,
109 docstring const & title,
110 FileFilterList const & filters,
112 pair<docstring,docstring> const & dir1,
113 pair<docstring,docstring> const & dir2)
115 docstring lastPath = from_ascii(".");
116 if (!filename.empty())
117 lastPath = from_utf8(onlyPath(to_utf8(filename)));
119 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
121 FileDialog::Result result;
124 result = fileDlg.save(lastPath, filters,
125 from_utf8(onlyFilename(to_utf8(filename))));
127 result = fileDlg.open(lastPath, filters,
128 from_utf8(onlyFilename(to_utf8(filename))));
130 return result.second;
134 docstring const browseRelFile(docstring const & filename,
135 docstring const & refpath,
136 docstring const & title,
137 FileFilterList const & filters,
139 pair<docstring,docstring> const & dir1,
140 pair<docstring,docstring> const & dir2)
142 docstring const fname = from_utf8(makeAbsPath(
143 to_utf8(filename), to_utf8(refpath)).absFilename());
145 docstring const outname = browseFile(fname, title, filters, save,
147 docstring const reloutname = makeRelPath(outname, refpath);
148 if (prefixIs(reloutname, from_ascii("../")))
155 docstring const browseLibFile(docstring const & dir,
156 docstring const & name,
157 docstring const & ext,
158 docstring const & title,
159 FileFilterList const & filters)
162 pair<docstring, docstring> const dir1(_("System files|#S#s"),
163 from_utf8(addName(package().system_support().absFilename(), to_utf8(dir))));
165 pair<docstring, docstring> const dir2(_("User files|#U#u"),
166 from_utf8(addName(package().user_support().absFilename(), to_utf8(dir))));
168 docstring const result = browseFile(from_utf8(
169 libFileSearch(to_utf8(dir), to_utf8(name), to_utf8(ext)).absFilename()),
170 title, filters, false, dir1, dir2);
172 // remove the extension if it is the default one
173 docstring noextresult;
174 if (from_utf8(getExtension(to_utf8(result))) == ext)
175 noextresult = from_utf8(removeExtension(to_utf8(result)));
177 noextresult = result;
179 // remove the directory, if it is the default one
180 docstring const file = from_utf8(onlyFilename(to_utf8(noextresult)));
181 if (from_utf8(libFileSearch(to_utf8(dir), to_utf8(file), to_utf8(ext)).absFilename()) == result)
188 docstring const browseDir(docstring const & pathname,
189 docstring const & title,
190 pair<docstring,docstring> const & dir1,
191 pair<docstring,docstring> const & dir2)
193 docstring lastPath = from_ascii(".");
194 if (!pathname.empty())
195 lastPath = from_utf8(onlyPath(to_utf8(pathname)));
197 FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
199 FileDialog::Result const result =
200 fileDlg.opendir(lastPath, from_utf8(onlyFilename(to_utf8(pathname))));
202 return result.second;
206 void rescanTexStyles()
208 // Run rescan in user lyx directory
209 support::Path p(package().user_support());
210 FileName const command = libFileSearch("scripts", "TeXFiles.py");
212 int const status = one.startscript(Systemcall::Wait,
213 lyx::support::os::python() + ' ' +
214 quoteName(command.toFilesystemEncoding()));
218 Alert::error(_("Could not update TeX information"),
219 bformat(_("The script `%s' failed."), from_utf8(command.absFilename())));
223 void getTexFileList(string const & filename, std::vector<string> & list)
226 FileName const file = libFileSearch("", filename);
230 list = getVectorFromString(getFileContents(file), "\n");
232 // Normalise paths like /foo//bar ==> /foo/bar
233 boost::RegEx regex("/{2,}");
234 std::vector<string>::iterator it = list.begin();
235 std::vector<string>::iterator end = list.end();
236 for (; it != end; ++it)
237 *it = regex.Merge((*it), "/");
239 // remove empty items and duplicates
240 list.erase(std::remove(list.begin(), list.end(), ""), list.end());
241 eliminate_duplicates(list);
244 } // namespace frontend