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;
42 string const token_from("$$i");
43 string const token_path("$$p");
47 bool operator<(Format const & a, Format const & b)
49 // use the compare_ascii_no_case instead of compare_no_case,
50 // because in turkish, 'i' is not the lowercase version of 'I',
51 // and thus turkish locale breaks parsing of tags.
53 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
56 Format::Format(string const & n, string const & e, string const & p,
57 string const & s, string const & v): name_(n),
65 bool Format::dummy() const
67 return extension().empty();
71 bool Format::isChildFormat() const
75 return isdigit(name_[name_.length() - 1]);
79 string const Format::parentFormat() const
81 return name_.substr(0, name_.length() - 1);
85 // This method should return a reference, and throw an exception
86 // if the format named name cannot be found (Lgb)
87 Format const * Formats::getFormat(string const & name) const
89 FormatList::const_iterator cit =
90 find_if(formatlist.begin(), formatlist.end(),
91 lyx::compare_memfun(&Format::name, name));
92 if (cit != formatlist.end())
99 int Formats::getNumber(string const & name) const
101 FormatList::const_iterator cit =
102 find_if(formatlist.begin(), formatlist.end(),
103 lyx::compare_memfun(&Format::name, name));
104 if (cit != formatlist.end())
105 return cit - formatlist.begin();
111 void Formats::add(string const & name)
113 if (!getFormat(name))
114 add(name, name, name, string());
118 void Formats::add(string const & name, string const & extension,
119 string const & prettyname, string const & shortcut)
121 FormatList::iterator it =
122 find_if(formatlist.begin(), formatlist.end(),
123 lyx::compare_memfun(&Format::name, name));
124 if (it == formatlist.end())
125 formatlist.push_back(Format(name, extension, prettyname,
128 string viewer = it->viewer();
129 *it = Format(name, extension, prettyname, shortcut, viewer);
134 void Formats::erase(string const & name)
136 FormatList::iterator it =
137 find_if(formatlist.begin(), formatlist.end(),
138 lyx::compare_memfun(&Format::name, name));
139 if (it != formatlist.end())
140 formatlist.erase(it);
146 std::sort(formatlist.begin(), formatlist.end());
150 void Formats::setViewer(string const & name, string const & command)
153 FormatList::iterator it =
154 find_if(formatlist.begin(), formatlist.end(),
155 lyx::compare_memfun(&Format::name, name));
156 if (it != formatlist.end())
157 it->setViewer(command);
161 bool Formats::view(Buffer const & buffer, string const & filename,
162 string const & format_name) const
164 if (filename.empty())
167 Format const * format = getFormat(format_name);
168 if (format && format->viewer().empty() &&
169 format->isChildFormat())
170 format = getFormat(format->parentFormat());
171 if (!format || format->viewer().empty()) {
172 // I believe this is the wrong place to show alerts, it should be done by
173 // the caller (this should be "utility" code
174 Alert::error(_("Cannot view file"),
175 bformat(_("No information for viewing %1$s"),
176 prettyName(format_name)));
180 string command = format->viewer();
182 if (format_name == "dvi" &&
183 !lyxrc.view_dvi_paper_option.empty()) {
184 command += ' ' + lyxrc.view_dvi_paper_option;
185 string paper_size = buffer.params().paperSizeName();
186 if (paper_size == "letter")
188 command += ' ' + paper_size;
189 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
193 if (!contains(command, token_from))
194 command += ' ' + token_from;
196 command = subst(command, token_from,
197 QuoteName(OnlyFilename(filename)));
198 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
200 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
201 buffer.message(_("Executing command: ") + command);
203 Path p(OnlyPath(filename));
205 int const res = one.startscript(Systemcall::DontWait, command);
208 Alert::error(_("Cannot view file"),
209 bformat(_("An error occurred whilst running %1$s"),
210 command.substr(0, 50)));
217 string const Formats::prettyName(string const & name) const
219 Format const * format = getFormat(name);
221 return format->prettyname();
227 string const Formats::extension(string const & name) const
229 Format const * format = getFormat(name);
231 return format->extension();
241 Formats system_formats;