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"
20 #include "frontends/Alert.h" //to be removed?
22 #include "support/filetools.h"
23 #include "support/path.h"
24 #include "support/systemcall.h"
25 #include "support/lyxfunctional.h"
27 using lyx::support::bformat;
28 using lyx::support::compare_ascii_no_case;
29 using lyx::support::contains;
30 using lyx::support::OnlyFilename;
31 using lyx::support::OnlyPath;
32 using lyx::support::Path;
33 using lyx::support::QuoteName;
34 using lyx::support::subst;
35 using lyx::support::Systemcall;
40 string const token_from("$$i");
41 string const token_path("$$p");
45 bool operator<(Format const & a, Format const & b)
47 // use the compare_ascii_no_case instead of compare_no_case,
48 // because in turkish, 'i' is not the lowercase version of 'I',
49 // and thus turkish locale breaks parsing of tags.
51 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
54 Format::Format(string const & n, string const & e, string const & p,
55 string const & s, string const & v): name_(n),
63 bool Format::dummy() const
65 return extension().empty();
69 bool Format::isChildFormat() const
73 return isdigit(name_[name_.length() - 1]);
77 string const Format::parentFormat() const
79 return name_.substr(0, name_.length() - 1);
83 // This method should return a reference, and throw an exception
84 // if the format named name cannot be found (Lgb)
85 Format const * Formats::getFormat(string const & name) const
87 FormatList::const_iterator cit =
88 find_if(formatlist.begin(), formatlist.end(),
89 lyx::compare_memfun(&Format::name, name));
90 if (cit != formatlist.end())
97 int Formats::getNumber(string const & name) const
99 FormatList::const_iterator cit =
100 find_if(formatlist.begin(), formatlist.end(),
101 lyx::compare_memfun(&Format::name, name));
102 if (cit != formatlist.end())
103 return cit - formatlist.begin();
109 void Formats::add(string const & name)
111 if (!getFormat(name))
112 add(name, name, name, string());
116 void Formats::add(string const & name, string const & extension,
117 string const & prettyname, string const & shortcut)
119 FormatList::iterator it =
120 find_if(formatlist.begin(), formatlist.end(),
121 lyx::compare_memfun(&Format::name, name));
122 if (it == formatlist.end())
123 formatlist.push_back(Format(name, extension, prettyname,
126 string viewer = it->viewer();
127 *it = Format(name, extension, prettyname, shortcut, viewer);
132 void Formats::erase(string const & name)
134 FormatList::iterator it =
135 find_if(formatlist.begin(), formatlist.end(),
136 lyx::compare_memfun(&Format::name, name));
137 if (it != formatlist.end())
138 formatlist.erase(it);
144 std::sort(formatlist.begin(), formatlist.end());
148 void Formats::setViewer(string const & name, string const & command)
151 FormatList::iterator it =
152 find_if(formatlist.begin(), formatlist.end(),
153 lyx::compare_memfun(&Format::name, name));
154 if (it != formatlist.end())
155 it->setViewer(command);
159 bool Formats::view(Buffer const & buffer, string const & filename,
160 string const & format_name) const
162 if (filename.empty())
165 Format const * format = getFormat(format_name);
166 if (format && format->viewer().empty() &&
167 format->isChildFormat())
168 format = getFormat(format->parentFormat());
169 if (!format || format->viewer().empty()) {
170 // I believe this is the wrong place to show alerts, it should be done by
171 // the caller (this should be "utility" code
172 Alert::error(_("Cannot view file"),
173 bformat(_("No information for viewing %1$s"),
174 prettyName(format_name)));
178 string command = format->viewer();
180 if (format_name == "dvi" &&
181 !lyxrc.view_dvi_paper_option.empty()) {
182 command += ' ' + lyxrc.view_dvi_paper_option;
183 string paper_size = buffer.params().paperSizeName();
184 if (paper_size == "letter")
186 command += ' ' + paper_size;
187 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
191 if (!contains(command, token_from))
192 command += ' ' + token_from;
194 command = subst(command, token_from,
195 QuoteName(OnlyFilename(filename)));
196 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
198 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
199 buffer.message(_("Executing command: ") + command);
201 Path p(OnlyPath(filename));
203 int const res = one.startscript(Systemcall::DontWait, command);
206 Alert::error(_("Cannot view file"),
207 bformat(_("An error occurred whilst running %1$s"),
208 command.substr(0, 50)));
215 string const Formats::prettyName(string const & name) const
217 Format const * format = getFormat(name);
219 return format->prettyname();
225 string const Formats::extension(string const & name) const
227 Format const * format = getFormat(name);
229 return format->extension();
239 Formats system_formats;