3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "bufferparams.h"
19 #include "lyxsocket.h"
21 #include "frontends/Alert.h" //to be removed?
23 #include "support/filetools.h"
24 #include "support/path.h"
25 #include "support/systemcall.h"
27 using lyx::support::bformat;
28 using lyx::support::compare_ascii_no_case;
29 using lyx::support::contains;
30 using lyx::support::MakeDisplayPath;
31 using lyx::support::OnlyFilename;
32 using lyx::support::OnlyPath;
33 using lyx::support::Path;
34 using lyx::support::QuoteName;
35 using lyx::support::subst;
36 using lyx::support::Systemcall;
41 extern LyXServerSocket * lyxsocket;
45 string const token_from("$$i");
46 string const token_path("$$p");
47 string const token_socket("$$a");
50 class FormatNamesEqual : public std::unary_function<Format, bool> {
52 FormatNamesEqual(string const & name)
54 bool operator()(Format const & f) const
56 return f.name() == name_;
63 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
65 FormatExtensionsEqual(string const & extension)
66 : extension_(extension) {}
67 bool operator()(Format const & f) const
69 return f.extension() == extension_;
77 bool operator<(Format const & a, Format const & b)
79 // use the compare_ascii_no_case instead of compare_no_case,
80 // because in turkish, 'i' is not the lowercase version of 'I',
81 // and thus turkish locale breaks parsing of tags.
83 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
86 Format::Format(string const & n, string const & e, string const & p,
87 string const & s, string const & v, string const & ed)
88 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v), editor_(ed)
92 bool Format::dummy() const
94 return extension().empty();
98 bool Format::isChildFormat() const
102 return isdigit(name_[name_.length() - 1]);
106 string const Format::parentFormat() const
108 return name_.substr(0, name_.length() - 1);
112 // This method should return a reference, and throw an exception
113 // if the format named name cannot be found (Lgb)
114 Format const * Formats::getFormat(string const & name) const
116 FormatList::const_iterator cit =
117 find_if(formatlist.begin(), formatlist.end(),
118 FormatNamesEqual(name));
119 if (cit != formatlist.end())
126 string Formats::getFormatFromFile(string const & filename) const
128 if (filename.empty())
131 string const format = lyx::support::getFormatFromContents(filename);
135 // try to find a format from the file extension.
136 string const ext(lyx::support::GetExtension(filename));
138 // this is ambigous if two formats have the same extension,
139 // but better than nothing
140 Formats::const_iterator cit =
141 find_if(formatlist.begin(), formatlist.end(),
142 FormatExtensionsEqual(ext));
143 if (cit != formats.end()) {
144 lyxerr[Debug::GRAPHICS]
145 << "\twill guess format from file extension: "
146 << ext << " -> " << cit->name() << std::endl;
154 int Formats::getNumber(string const & name) const
156 FormatList::const_iterator cit =
157 find_if(formatlist.begin(), formatlist.end(),
158 FormatNamesEqual(name));
159 if (cit != formatlist.end())
160 return distance(formatlist.begin(), cit);
166 void Formats::add(string const & name)
168 if (!getFormat(name))
169 add(name, name, name, string(), string(), string());
173 void Formats::add(string const & name, string const & extension,
174 string const & prettyname, string const & shortcut,
175 string const & viewer, string const & editor)
177 FormatList::iterator it =
178 find_if(formatlist.begin(), formatlist.end(),
179 FormatNamesEqual(name));
180 if (it == formatlist.end())
181 formatlist.push_back(Format(name, extension, prettyname,
182 shortcut, viewer, editor));
184 *it = Format(name, extension, prettyname, shortcut, viewer, editor);
188 void Formats::erase(string const & name)
190 FormatList::iterator it =
191 find_if(formatlist.begin(), formatlist.end(),
192 FormatNamesEqual(name));
193 if (it != formatlist.end())
194 formatlist.erase(it);
200 std::sort(formatlist.begin(), formatlist.end());
204 void Formats::setViewer(string const & name, string const & command)
207 FormatList::iterator it =
208 find_if(formatlist.begin(), formatlist.end(),
209 FormatNamesEqual(name));
210 if (it != formatlist.end())
211 it->setViewer(command);
215 bool Formats::view(Buffer const & buffer, string const & filename,
216 string const & format_name) const
218 if (filename.empty())
221 Format const * format = getFormat(format_name);
222 if (format && format->viewer().empty() &&
223 format->isChildFormat())
224 format = getFormat(format->parentFormat());
225 if (!format || format->viewer().empty()) {
226 // I believe this is the wrong place to show alerts, it should be done by
227 // the caller (this should be "utility" code)
228 Alert::error(_("Cannot view file"),
229 bformat(_("No information for viewing %1$s"),
230 prettyName(format_name)));
234 string command = format->viewer();
236 if (format_name == "dvi" &&
237 !lyxrc.view_dvi_paper_option.empty()) {
238 command += ' ' + lyxrc.view_dvi_paper_option;
239 string paper_size = buffer.params().paperSizeName();
240 if (paper_size == "letter")
242 command += ' ' + paper_size;
243 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
247 if (!contains(command, token_from))
248 command += ' ' + token_from;
250 command = subst(command, token_from,
251 QuoteName(OnlyFilename(filename)));
252 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
253 command = subst(command, token_socket, QuoteName(lyxsocket->address()));
254 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
255 buffer.message(_("Executing command: ") + command);
257 Path p(OnlyPath(filename));
259 int const res = one.startscript(Systemcall::DontWait, command);
262 Alert::error(_("Cannot view file"),
263 bformat(_("An error occurred whilst running %1$s"),
264 MakeDisplayPath(command, 50)));
271 bool Formats::edit(Buffer const & buffer, string const & filename,
272 string const & format_name) const
274 if (filename.empty())
277 Format const * format = getFormat(format_name);
278 if (format && format->editor().empty() &&
279 format->isChildFormat())
280 format = getFormat(format->parentFormat());
281 if (!format || format->editor().empty()) {
282 // I believe this is the wrong place to show alerts, it should be done by
283 // the caller (this should be "utility" code)
284 Alert::error(_("Cannot edit file"),
285 bformat(_("No information for editing %1$s"),
286 prettyName(format_name)));
290 string command = format->editor();
292 if (!contains(command, token_from))
293 command += ' ' + token_from;
295 command = subst(command, token_from,
296 QuoteName(OnlyFilename(filename)));
297 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
298 command = subst(command, token_socket, QuoteName(lyxsocket->address()));
299 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
300 buffer.message(_("Executing command: ") + command);
302 Path p(OnlyPath(filename));
304 int const res = one.startscript(Systemcall::DontWait, command);
307 Alert::error(_("Cannot edit file"),
308 bformat(_("An error occurred whilst running %1$s"),
309 MakeDisplayPath(command, 50)));
316 string const Formats::prettyName(string const & name) const
318 Format const * format = getFormat(name);
320 return format->prettyname();
326 string const Formats::extension(string const & name) const
328 Format const * format = getFormat(name);
330 return format->extension();
340 Formats system_formats;