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 "lyx_cb.h" // for ShowMessage() ... to be removed?
18 #include "BoostFormat.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"
31 string const token_from("$$i");
32 string const token_path("$$p");
36 bool operator<(Format const & a, Format const & b)
38 // use the compare_ascii_no_case instead of compare_no_case,
39 // because in turkish, 'i' is not the lowercase version of 'I',
40 // and thus turkish locale breaks parsing of tags.
42 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
45 Format::Format(string const & n, string const & e, string const & p,
46 string const & s, string const & v): name_(n),
54 bool Format::dummy() const
56 return extension().empty();
60 bool Format::isChildFormat() const
64 return isdigit(name_[name_.length() - 1]);
68 string const Format::parentFormat() const
70 return name_.substr(0, name_.length() - 1);
74 // This method should return a reference, and throw an exception
75 // if the format named name cannot be found (Lgb)
76 Format const * Formats::getFormat(string const & name) const
78 FormatList::const_iterator cit =
79 find_if(formatlist.begin(), formatlist.end(),
80 lyx::compare_memfun(&Format::name, name));
81 if (cit != formatlist.end())
88 int Formats::getNumber(string const & name) const
90 FormatList::const_iterator cit =
91 find_if(formatlist.begin(), formatlist.end(),
92 lyx::compare_memfun(&Format::name, name));
93 if (cit != formatlist.end())
94 return cit - formatlist.begin();
100 void Formats::add(string const & name)
102 if (!getFormat(name))
103 add(name, name, name, string());
107 void Formats::add(string const & name, string const & extension,
108 string const & prettyname, string const & shortcut)
110 FormatList::iterator it =
111 find_if(formatlist.begin(), formatlist.end(),
112 lyx::compare_memfun(&Format::name, name));
113 if (it == formatlist.end())
114 formatlist.push_back(Format(name, extension, prettyname,
117 string viewer = it->viewer();
118 *it = Format(name, extension, prettyname, shortcut, viewer);
123 void Formats::erase(string const & name)
125 FormatList::iterator it =
126 find_if(formatlist.begin(), formatlist.end(),
127 lyx::compare_memfun(&Format::name, name));
128 if (it != formatlist.end())
129 formatlist.erase(it);
135 std::sort(formatlist.begin(), formatlist.end());
139 void Formats::setViewer(string const & name, string const & command)
142 FormatList::iterator it =
143 find_if(formatlist.begin(), formatlist.end(),
144 lyx::compare_memfun(&Format::name, name));
145 if (it != formatlist.end())
146 it->setViewer(command);
150 bool Formats::view(Buffer const * buffer, string const & filename,
151 string const & format_name) const
153 if (filename.empty())
156 Format const * format = getFormat(format_name);
157 if (format && format->viewer().empty() &&
158 format->isChildFormat())
159 format = getFormat(format->parentFormat());
160 if (!format || format->viewer().empty()) {
162 Alert::alert(_("Cannot view file"),
163 boost::io::str(boost::format(_("No information for viewing %1$s"))
164 % prettyName(format_name)));
166 Alert::alert(_("Cannot view file"),
167 _("No information for viewing ")
168 + prettyName(format_name));
173 string command = format->viewer();
175 if (format_name == "dvi" &&
176 !lyxrc.view_dvi_paper_option.empty()) {
177 command += ' ' + lyxrc.view_dvi_paper_option;
178 string paper_size = papersize(buffer);
179 if (paper_size == "letter")
181 command += ' ' + paper_size;
182 if (buffer->params.orientation
183 == BufferParams::ORIENTATION_LANDSCAPE)
187 if (!contains(command, token_from))
188 command += ' ' + token_from;
190 command = subst(command, token_from,
191 QuoteName(OnlyFilename(filename)));
192 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
194 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
195 ShowMessage(buffer, _("Executing command:"), command);
197 Path p(OnlyPath(filename));
199 int const res = one.startscript(Systemcall::DontWait, command);
202 Alert::alert(_("Cannot view file"),
203 _("Error while executing"),
204 command.substr(0, 50));
211 string const Formats::prettyName(string const & name) const
213 Format const * format = getFormat(name);
215 return format->prettyname();
221 string const Formats::extension(string const & name) const
223 Format const * format = getFormat(name);
225 return format->extension();
231 string const papersize(Buffer const * buffer)
233 char real_papersize = buffer->params.papersize;
234 if (real_papersize == BufferParams::PAPER_DEFAULT)
235 real_papersize = lyxrc.default_papersize;
237 switch (real_papersize) {
238 case BufferParams::PAPER_A3PAPER:
240 case BufferParams::PAPER_A4PAPER:
242 case BufferParams::PAPER_A5PAPER:
244 case BufferParams::PAPER_B5PAPER:
246 case BufferParams::PAPER_EXECUTIVEPAPER:
248 case BufferParams::PAPER_LEGALPAPER:
250 case BufferParams::PAPER_USLETTER:
259 Formats system_formats;