2 * \file InsetArgument.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Martin Vermeer
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetArgument.h"
17 #include "BufferParams.h"
19 #include "FuncStatus.h"
20 #include "FuncRequest.h"
21 #include "InsetLayout.h"
22 #include "InsetList.h"
26 #include "ParIterator.h"
28 #include "texstream.h"
29 #include "TocBackend.h"
32 #include "support/convert.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
43 InsetArgument::InsetArgument(Buffer * buf, string const & name)
44 : InsetCollapsible(buf), name_(name), labelstring_(docstring()),
45 font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
46 pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
47 free_spacing_(false), pass_thru_chars_(docstring()), is_toc_caption_(false),
48 newline_cmd_(string())
52 void InsetArgument::write(ostream & os) const
54 os << "Argument " << name_ << "\n";
55 InsetCollapsible::write(os);
59 void InsetArgument::read(Lexer & lex)
62 InsetCollapsible::read(lex);
66 void InsetArgument::init(Paragraph const & par)
68 Inset const & ininset = par.inInset();
69 bool const insetlayout = !par.layout().hasArgs();
70 Layout::LaTeXArgMap const args = insetlayout ?
71 ininset.getLayout().args() : par.layout().args();
72 pass_thru_context_ = insetlayout ?
73 ininset.getLayout().isPassThru() : par.layout().pass_thru;
74 // Record PassThru status in order to act on changes.
75 bool const former_pass_thru = pass_thru_;
77 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
78 // "999" is the conventional name given to those by lyx2lyx
80 int const req = insetlayout ? ininset.getLayout().requiredArgs()
81 : par.layout().requiredArgs();
82 int const opts = insetlayout ? ininset.getLayout().optArgs()
83 : par.layout().optArgs();
86 for (InsetList::Element const & elt : par.insetList()) {
87 if (elt.inset->lyxCode() == ARG_CODE) {
89 if (elt.inset == this)
96 // We have optional arguments
98 if (ours <= realopts) {
99 name_ = convert<string>(ours);
104 // This is a mandatory argument. We have to consider
105 // non-given optional arguments for the numbering
106 int offset = opts - realopts;
108 name_ = convert<string>(ours);
111 Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
112 caption_of_toc_ = string();
113 if (lait != args.end()) {
114 docstring label = translateIfPossible((*lait).second.labelstring);
115 docstring striplabel;
116 support::rsplit(label, striplabel, '|');
117 labelstring_ = striplabel.empty() ? label: striplabel;
118 tooltip_ = translateIfPossible((*lait).second.tooltip);
119 font_ = (*lait).second.font;
120 labelfont_ = (*lait).second.labelfont;
121 decoration_ = (*lait).second.decoration;
122 pass_thru_chars_ = (*lait).second.pass_thru_chars;
123 newline_cmd_ = (*lait).second.newlinecmd;
124 free_spacing_ = (*lait).second.free_spacing;
125 docbooktag_ = (*lait).second.docbooktag;
126 docbooktagtype_ = (*lait).second.docbooktagtype;
127 docbookattr_ = (*lait).second.docbookattr;
128 docbookargumentbeforemaintag_ = (*lait).second.docbookargumentbeforemaintag;
129 docbookargumentaftermaintag_ = (*lait).second.docbookargumentaftermaintag;
130 pass_thru_local_ = false;
131 if (lait->second.is_toc_caption) {
132 is_toc_caption_ = true;
133 // empty if AddToToc is not set
134 caption_of_toc_ = insetlayout
135 ? ininset.getLayout().tocType()
136 : par.layout().tocType();
139 switch ((*lait).second.passthru) {
141 pass_thru_ = pass_thru_context_;
145 pass_thru_local_ = true;
152 labelstring_ = _("Unknown Argument");
153 tooltip_ = _("Argument not known in this Layout. Will be suppressed in the output.");
156 if (former_pass_thru != pass_thru_) {
157 // PassThru status changed. We might need to update
158 // the language of the contents
159 // Language const * l = insetlayout
160 // ? it.inset().buffer().language()
161 // : it.buffer()->language();
162 Language const * l = ininset.buffer().language();
163 fixParagraphLanguage(l);
169 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
171 init(it.paragraph());
172 InsetCollapsible::updateBuffer(it, utype, deleted);
176 void InsetArgument::setButtonLabel()
178 setLabel(labelstring_);
182 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
186 return toolTipText(tooltip_ + from_ascii(":\n"));
190 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
192 switch (cmd.action()) {
194 case LFUN_INSET_MODIFY: {
195 string const first_arg = cmd.getArg(0);
196 bool const change_type = first_arg == "changetype";
199 // this will not be handled higher up
203 cur.recordUndoInset(this);
204 name_ = cmd.getArg(1);
205 cur.forceBufferUpdate();
210 case LFUN_CLIPBOARD_PASTE:
211 case LFUN_SELECTION_PASTE:
212 case LFUN_PRIMARY_SELECTION_PASTE:
213 case LFUN_SELF_INSERT:
214 // With (only) inherited pass_thru, call Text::dispatch()
215 // directly to avoid call for fixParagraphsFont() and/or
216 // forcing to latex_language in InsetText::dispatch(),
217 // since this does not play nicely with inherited pass_thru
219 if (pass_thru_ && !pass_thru_local_) {
220 text().dispatch(cur, cmd);
221 // For the paste operations, check if we have
222 // non-latex_language, and if so, fix.
223 if (cmd.action() != LFUN_SELF_INSERT)
224 fixParagraphLanguage(buffer().params().language);
227 InsetCollapsible::doDispatch(cur, cmd);
231 InsetCollapsible::doDispatch(cur, cmd);
237 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
238 FuncStatus & flag) const
240 switch (cmd.action()) {
242 case LFUN_INSET_MODIFY: {
243 string const first_arg = cmd.getArg(0);
244 if (first_arg == "changetype") {
245 string const type = cmd.getArg(1);
246 flag.setOnOff(type == name_);
248 flag.setEnabled(true);
251 Layout::LaTeXArgMap args;
252 bool const insetlayout = cur.paragraph().layout().latexargs().empty();
254 args = cur.inset().getLayout().latexargs();
256 args = cur.paragraph().layout().latexargs();
257 Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
258 if (lait != args.end()) {
259 flag.setEnabled(true);
260 for (auto const & table : cur.paragraph().insetList())
261 if (InsetArgument const * ins = table.inset->asInsetArgument())
262 if (ins->name() == type) {
263 // we have this already
264 flag.setEnabled(false);
268 flag.setEnabled(false);
271 return InsetCollapsible::getStatus(cur, cmd, flag);
275 return InsetCollapsible::getStatus(cur, cmd, flag);
280 string InsetArgument::contextMenuName() const
282 if (decoration() == InsetDecoration::CONGLOMERATE)
283 return "context-argument-conglomerate";
285 return "context-argument";
289 FontInfo InsetArgument::getFont() const
291 if (font_ != inherit_font)
293 return InsetCollapsible::getFont();
297 FontInfo InsetArgument::getLabelfont() const
299 if (labelfont_ != inherit_font)
301 return InsetCollapsible::getLabelfont();
305 ColorCode InsetArgument::labelColor() const {
306 if (labelfont_.color() != Color_inherit)
307 return labelfont_.color();
308 return InsetCollapsible::labelColor();
312 InsetDecoration InsetArgument::decoration() const
314 InsetDecoration dec = getLayout().decoration();
315 if (!decoration_.empty())
316 dec = translateDecoration(decoration_);
317 return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
321 void InsetArgument::docbook(XMLStream & xs, OutputParams const & rp) const {
322 // Ignore arguments that have already been output or are planned to be output elsewhere.
323 if (rp.docbook_prepended_arguments.find(this) != rp.docbook_prepended_arguments.end())
325 if (rp.docbook_appended_arguments.find(this) != rp.docbook_appended_arguments.end())
328 if (docbooktag_ != from_ascii("NONE") && docbooktag_ != from_ascii("IGNORE")) {
329 // TODO: implement docbooktagtype_.
330 xs << xml::StartTag(docbooktag_, docbookattr_);
331 InsetText::docbook(xs, rp);
332 xs << xml::EndTag(docbooktag_);
338 void InsetArgument::latexArgument(otexstream & os,
339 OutputParams const & runparams_in, docstring const & ldelim,
340 docstring const & rdelim, docstring const & presetarg) const
342 otexstringstream ots;
343 OutputParams runparams = runparams_in;
344 if (!pass_thru_chars_.empty())
345 runparams.pass_thru_chars += pass_thru_chars_;
346 if (!newline_cmd_.empty())
347 runparams.newlinecmd = newline_cmd_;
348 runparams.pass_thru = isPassThru();
349 InsetText::latex(ots, runparams);
350 TexString ts = ots.release();
351 bool const add_braces = !ldelim.empty() && ldelim != "{"
352 && support::contains(ts.str, rdelim);
357 if (!presetarg.empty() && !ts.str.empty())
366 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
367 UpdateType utype, TocBackend & backend) const
369 if (!caption_of_toc_.empty()) {
371 text().forOutliner(str, TOC_ENTRY_LENGTH);
372 backend.builder(caption_of_toc_).argumentItem(str);
374 // Proceed with the rest of the inset.
375 InsetText::addToToc(dit, output_active, utype, backend);
379 void InsetArgument::fixParagraphLanguage(Language const * l)
381 Font font(inherit_font, l);
383 font.setLanguage(latex_language);
384 paragraphs().front().resetFonts(font);