2 * \file ControlSendto.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
14 #include "ControlSendto.h"
17 #include "converter.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/systemcall.h"
27 using namespace lyx::support;
32 ControlSendto::ControlSendto(LyXView & lv, Dialogs & d)
33 : ControlDialogBD(lv, d),
35 command_(lyxrc.custom_export_command)
39 vector<Format const *> const ControlSendto::allFormats() const
41 // What formats can we output natively?
42 vector<string> exports;
43 exports.push_back("lyx");
44 exports.push_back("text");
46 if (buffer()->isLatex())
47 exports.push_back("latex");
48 if (buffer()->isLinuxDoc())
49 exports.push_back("linuxdoc");
50 if (buffer()->isDocBook())
51 exports.push_back("docbook");
52 if (buffer()->isLiterate())
53 exports.push_back("literate");
55 // Loop over these native formats and ascertain what formats we
57 vector<Format const *> to;
59 vector<string>::const_iterator ex_it = exports.begin();
60 vector<string>::const_iterator ex_end = exports.end();
61 for (; ex_it != ex_end; ++ex_it) {
62 // Start off with the native export format.
63 // "formats" is LyX's list of recognised formats
64 to.push_back(formats.getFormat(*ex_it));
66 Formats::const_iterator fo_it = formats.begin();
67 Formats::const_iterator fo_end = formats.end();
68 for (; fo_it != fo_end; ++fo_it) {
69 // we need to hide the default graphic export formats
70 // from the external menu, because we need them only
71 // for the internal lyx-view and external latex run
72 string const name = fo_it->name();
73 if (name != "eps" && name != "xpm" && name != "png" &&
74 converters.isReachable(*ex_it, name))
75 to.push_back(&(*fo_it));
79 // Remove repeated formats.
80 std::sort(to.begin(), to.end());
82 vector<Format const *>::iterator to_begin = to.begin();
83 vector<Format const *>::iterator to_end = to.end();
84 vector<Format const *>::iterator to_it =
85 std::unique(to_begin, to_end);
87 to.erase(to_it, to_end);
93 void ControlSendto::setFormat(Format const * fmt)
99 void ControlSendto::setCommand(string const & cmd)
101 command_ = trim(cmd);
105 void ControlSendto::apply()
107 if (!bufferIsAvailable())
112 if (command_.empty() || !format_)
115 // The name of the file created by the conversion process
118 // Output to filename
119 if (format_->name() == "lyx") {
120 filename = ChangeExtension(buffer()->getLatexName(false),
121 format_->extension());
122 if (!buffer()->tmppath.empty())
123 filename = AddName(buffer()->tmppath, filename);
125 buffer()->writeFile(filename);
128 Exporter::Export(buffer(), format_->name(), true, filename);
131 // Substitute $$FName for filename
132 string command = command_;
133 if (!contains(command, "$$FName"))
134 command = "( " + command + " ) < $$FName";
135 command = subst(command, "$$FName", filename);
137 // Execute the command in the background
139 call.startscript(Systemcall::DontWait, command);