2 * \file InsetCommand.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "InsetCommand.h"
17 #include "BufferEncodings.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetIterator.h"
25 #include "MetricsInfo.h"
26 #include "texstream.h"
28 #include "insets/InsetBox.h"
29 #include "insets/InsetBranch.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetExternal.h"
32 #include "insets/InsetFloat.h"
33 #include "insets/InsetGraphics.h"
34 #include "insets/InsetIndex.h"
35 #include "insets/InsetListings.h"
36 #include "insets/InsetNote.h"
37 #include "insets/InsetPhantom.h"
38 #include "insets/InsetSpace.h"
39 #include "insets/InsetVSpace.h"
40 #include "insets/InsetWrap.h"
42 #include "support/debug.h"
43 #include "support/Lexer.h"
44 #include "support/lstrings.h"
46 #include "frontends/Application.h"
51 using namespace lyx::support;
56 // FIXME Would it now be possible to use the InsetCode in
57 // place of the mailer name and recover that information?
58 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
59 : Inset(buf), p_(p), broken_(false)
63 // The sole purpose of this copy constructor is to make sure
64 // that the mouse_hover_ map is not copied and remains empty.
65 InsetCommand::InsetCommand(InsetCommand const & rhs)
66 : Inset(rhs), p_(rhs.p_), broken_(false)
70 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
75 Inset::operator=(rhs);
78 button_ = RenderButton();
85 InsetCommand::~InsetCommand()
87 if (p_.code() != NO_CODE)
88 hideDialogs(insetName(p_.code()), this);
90 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
91 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
92 for (; it != end; ++it)
94 it->first->clearLastInset(this);
98 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
100 button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0),
101 inheritFont(), broken_);
102 button_.metrics(mi, dim);
106 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
109 mouse_hover_[bv] = mouse_hover;
114 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
116 button_.setRenderState(mouse_hover_[pi.base.bv]);
117 button_.draw(pi, x, y);
121 void InsetCommand::setParam(string const & name, docstring const & value)
127 docstring const & InsetCommand::getParam(string const & name) const
133 void InsetCommand::setParams(InsetCommandParams const & p)
140 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
142 OutputParams runparams = runparams_in;
143 docstring command = getCommand(runparams);
144 if (buffer().params().use_minted
145 && prefixIs(command, from_ascii("\\lstlistoflistings")))
151 int InsetCommand::plaintext(odocstringstream & os,
152 OutputParams const &, size_t) const
154 docstring const str = "[" + buffer().B_("LaTeX Command: ")
155 + from_utf8(getCmdName()) + "]";
161 void InsetCommand::docbook(XMLStream &, OutputParams const &) const
167 void InsetCommand::validate(LaTeXFeatures & features) const
169 if (params().info().hasParam("literal")
170 && params()["literal"] == "true")
173 ParamInfo::const_iterator it = params().info().begin();
174 ParamInfo::const_iterator end = params().info().end();
175 for (; it != end; ++it) {
176 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
177 docstring const text = params()[it->name()];
178 // Validate the contents (if we LaTeXify, specific
179 // macros might require packages)
180 for (pos_type i = 0; i < int(text.size()) ; ++i)
181 BufferEncodings::validate(text[i], features);
187 bool InsetCommand::isChangedByCurrentAuthor() const
189 InsetIterator it = begin(buffer().inset());
190 InsetIterator const itend = end(buffer().inset());
191 for (; it != itend; ++it) {
196 LYXERR0("Unable to find inset!");
197 // to be on the safe side.
200 Paragraph const & ourpara = it.paragraph();
201 pos_type const ourpos = it.pos();
202 Change const & change = ourpara.lookupChange(ourpos);
203 return change.currentAuthor();
207 void InsetCommand::changeCmdName(string const & new_name)
209 string const & old_name = getCmdName();
210 if (old_name == new_name)
213 if (buffer().masterParams().track_changes) {
214 // With change tracking, we insert a new inset and
215 // delete the old one.
216 // But we need to make sure that the inset isn't one
217 // that the current author inserted. Otherwise, we might
219 if (isChangedByCurrentAuthor()) {
220 p_.setCmdName(new_name);
224 // OK, so this is not an inset the current author inserted
225 InsetCommandParams p(p_.code());
227 p.setCmdName(new_name);
228 string const data = InsetCommand::params2string(p);
229 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
230 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
232 p_.setCmdName(new_name);
236 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
238 switch (cmd.action()) {
239 case LFUN_INSET_MODIFY: {
240 if (cmd.getArg(0) == "changetype") {
242 changeCmdName(cmd.getArg(1));
243 cur.forceBufferUpdate();
247 InsetCommandParams p(p_.code());
248 InsetCommand::string2params(to_utf8(cmd.argument()), p);
252 if (p.getCmdName().empty())
253 cur.noScreenUpdate();
256 if (buffer().masterParams().track_changes && !isChangedByCurrentAuthor()) {
257 // With change tracking, we insert a new inset and
258 // delete the old one
259 string const data = InsetCommand::params2string(p);
260 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
261 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
262 cur.forceBufferUpdate();
267 // FIXME We might also want to check here if this one is in the TOC.
268 // But I think most of those are labeled.
270 cur.forceBufferUpdate();
274 case LFUN_INSET_DIALOG_UPDATE: {
275 string const name = to_utf8(cmd.argument());
276 cur.bv().updateDialog(name, params2string(params()));
281 Inset::doDispatch(cur, cmd);
288 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
289 FuncStatus & status) const
291 switch (cmd.action()) {
293 case LFUN_ERT_INSERT:
294 status.setEnabled(false);
298 case LFUN_INSET_MODIFY:
299 if (cmd.getArg(0) == "changetype") {
300 string const newtype = cmd.getArg(1);
301 status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
302 status.setOnOff(newtype == p_.getCmdName());
304 status.setEnabled(true);
307 case LFUN_INSET_DIALOG_UPDATE:
308 status.setEnabled(true);
312 return Inset::getStatus(cur, cmd, status);
317 string InsetCommand::contextMenuName() const
319 return "context-" + insetName(p_.code());
323 bool InsetCommand::showInsetDialog(BufferView * bv) const
325 if (p_.code() != NO_CODE)
326 bv->showDialog(insetName(p_.code()), params2string(p_),
327 const_cast<InsetCommand *>(this));
332 bool InsetCommand::string2params(string const & data,
333 InsetCommandParams & params)
338 // This happens when inset-insert is called without argument except for the
340 // "inset-insert toc"
341 string const name = insetName(params.code());
344 istringstream dstream(data);
346 lex.setStream(dstream);
347 lex.setContext("InsetCommand::string2params");
348 lex >> name.c_str(); // check for name
349 lex >> "CommandInset";
355 string InsetCommand::params2string(InsetCommandParams const & params)
358 data << insetName(params.code()) << ' ';
360 data << "\\end_inset\n";
365 bool decodeInsetParam(string const & name, string & data,
366 Buffer const & buffer)
368 InsetCode const code = insetCode(name);
372 case INDEX_PRINT_CODE:
376 case NOMENCL_PRINT_CODE:
381 InsetCommandParams p(code);
382 data = InsetCommand::params2string(p);
386 // data is the include type: one of "include",
387 // "input", "verbatiminput" or "verbatiminput*"
389 // default type is requested
391 InsetCommandParams p(INCLUDE_CODE, data);
392 data = InsetCommand::params2string(p);
396 // \c data == "Boxed" || "Frameless" etc
397 InsetBoxParams p(data);
398 data = InsetBox::params2string(p);
403 data = InsetBranch::params2string(p);
407 InsetCommandParams p(CITE_CODE);
408 data = InsetCommand::params2string(p);
412 data = InsetERT::params2string(InsetCollapsible::Open);
415 case EXTERNAL_CODE: {
416 InsetExternalParams p;
417 data = InsetExternal::params2string(p, buffer);
422 data = InsetFloat::params2string(p);
427 data = InsetIndex::params2string(p);
430 case LISTINGS_CODE: {
431 InsetListingsParams p;
432 data = InsetListings::params2string(p);
435 case GRAPHICS_CODE: {
436 InsetGraphicsParams p;
437 data = InsetGraphics::params2string(p, buffer);
440 case MATH_SPACE_CODE: {
441 InsetSpaceParams p(true);
442 data = InsetSpace::params2string(p);
447 data = InsetNote::params2string(p);
451 InsetPhantomParams p;
452 data = InsetPhantom::params2string(p);
457 data = InsetSpace::params2string(p);
462 data = InsetVSpace::params2string(space);
467 data = InsetWrap::params2string(p);
472 } // end switch(code)