]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
DocBook: add support for DocBookArgumentAfterMainTag.
[lyx.git] / src / insets / InsetArgument.cpp
1 /**
2  * \file InsetArgument.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetArgument.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "Cursor.h"
19 #include "FuncStatus.h"
20 #include "FuncRequest.h"
21 #include "InsetLayout.h"
22 #include "InsetList.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "Lexer.h"
26 #include "ParIterator.h"
27 #include "TexRow.h"
28 #include "texstream.h"
29 #include "TocBackend.h"
30 #include "xml.h"
31
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"
37
38 using namespace std;
39
40 namespace lyx {
41
42
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())
49 {}
50
51
52 void InsetArgument::write(ostream & os) const
53 {
54         os << "Argument " << name_ << "\n";
55         InsetCollapsible::write(os);
56 }
57
58
59 void InsetArgument::read(Lexer & lex)
60 {
61         lex >> name_;
62         InsetCollapsible::read(lex);
63 }
64
65
66 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
67 {
68         bool const insetlayout = !it.paragraph().layout().hasArgs();
69         Layout::LaTeXArgMap const args = insetlayout ?
70                 it.inset().getLayout().args() : it.paragraph().layout().args();
71         pass_thru_context_ = insetlayout ?
72                 it.inset().getLayout().isPassThru() : it.paragraph().layout().pass_thru;
73         // Record PassThru status in order to act on changes.
74         bool const former_pass_thru = pass_thru_;
75
76         // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
77         // "999" is the conventional name given to those by lyx2lyx
78         if (name_ == "999") {
79                 int const req = insetlayout ? it.inset().getLayout().requiredArgs()
80                                       : it.paragraph().layout().requiredArgs();
81                 int const opts = insetlayout ? it.inset().getLayout().optArgs()
82                                       : it.paragraph().layout().optArgs();
83                 int nr = 0;
84                 int ours = 0;
85                 InsetList::const_iterator parit = it.paragraph().insetList().begin();
86                 InsetList::const_iterator parend = it.paragraph().insetList().end();
87                 for (; parit != parend; ++parit) {
88                         if (parit->inset->lyxCode() == ARG_CODE) {
89                                 ++nr;
90                                 if (parit->inset == this)
91                                         ours = nr;
92                         }
93                 }
94                 bool done = false;
95                 int realopts = 0;
96                 if (nr > req) {
97                         // We have optional arguments
98                         realopts = nr - req;
99                         if (ours <= realopts) {
100                                 name_ = convert<string>(ours);
101                                 done = true;
102                         }
103                 }
104                 if (!done) {
105                         // This is a mandatory argument. We have to consider
106                         // non-given optional arguments for the numbering
107                         int offset = opts - realopts;
108                         ours += offset;
109                         name_ = convert<string>(ours);
110                 }
111         }
112         Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
113         caption_of_toc_ = string();
114         if (lait != args.end()) {
115                 docstring label = translateIfPossible((*lait).second.labelstring);
116                 docstring striplabel;
117                 support::rsplit(label, striplabel, '|');
118                 labelstring_ = striplabel.empty() ? label: striplabel;
119                 tooltip_ = translateIfPossible((*lait).second.tooltip);
120                 font_ = (*lait).second.font;
121                 labelfont_ = (*lait).second.labelfont;
122                 decoration_ = (*lait).second.decoration;
123                 pass_thru_chars_ = (*lait).second.pass_thru_chars;
124                 newline_cmd_ = (*lait).second.newlinecmd;
125                 free_spacing_ = (*lait).second.free_spacing;
126                 docbooktag_ = (*lait).second.docbooktag;
127                 docbooktagtype_ = (*lait).second.docbooktagtype;
128                 docbookattr_ = (*lait).second.docbookattr;
129                 docbookargumentbeforemaintag_ = (*lait).second.docbookargumentbeforemaintag;
130                 docbookargumentaftermaintag_ = (*lait).second.docbookargumentaftermaintag;
131                 pass_thru_local_ = false;
132                 if (lait->second.is_toc_caption) {
133                         is_toc_caption_ = true;
134                         // empty if AddToToc is not set
135                         caption_of_toc_ = insetlayout
136                                 ? it.inset().getLayout().tocType()
137                                 : it.paragraph().layout().tocType();
138                 }
139
140                 switch ((*lait).second.passthru) {
141                         case PT_INHERITED:
142                                 pass_thru_ = pass_thru_context_;
143                                 break;
144                         case PT_TRUE:
145                                 pass_thru_ = true;
146                                 pass_thru_local_ = true;
147                                 break;
148                         case PT_FALSE:
149                                 pass_thru_ = false;
150                                 break;
151                 }
152         } else {
153                 labelstring_ = _("Unknown Argument");
154                 tooltip_ = _("Argument not known in this Layout. Will be suppressed in the output.");
155         }
156
157         if (former_pass_thru != pass_thru_) {
158                 // PassThru status changed. We might need to update
159                 // the language of the contents
160                 Language const * l  = insetlayout
161                         ? it.inset().buffer().language()
162                         : it.buffer()->language();
163                 fixParagraphLanguage(l);
164         }
165
166         setButtonLabel();
167         InsetCollapsible::updateBuffer(it, utype, deleted);
168 }
169
170
171 void InsetArgument::setButtonLabel()
172 {
173         setLabel(labelstring_);
174 }
175
176
177 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
178 {
179         if (isOpen(bv))
180                 return tooltip_;
181         return toolTipText(tooltip_ + from_ascii(":\n"));
182 }
183
184
185 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
186 {
187         switch (cmd.action()) {
188
189         case LFUN_INSET_MODIFY: {
190                 string const first_arg = cmd.getArg(0);
191                 bool const change_type = first_arg == "changetype";
192                 if (!change_type) {
193                         // not for us
194                         // this will not be handled higher up
195                         cur.undispatched();
196                         return;
197                 }
198                 cur.recordUndoInset(this);
199                 name_ = cmd.getArg(1);
200                 cur.forceBufferUpdate();
201                 break;
202         }
203
204         case LFUN_PASTE:
205         case LFUN_CLIPBOARD_PASTE:
206         case LFUN_SELECTION_PASTE:
207         case LFUN_PRIMARY_SELECTION_PASTE:
208         case LFUN_SELF_INSERT:
209                 // With (only) inherited pass_thru, call Text::dispatch()
210                 // directly to avoid call for fixParagraphsFont() and/or
211                 // forcing to latex_language in InsetText::dispatch(),
212                 // since this does not play nicely with inherited pass_thru
213                 // (see #8471).
214                 if (pass_thru_ && !pass_thru_local_) {
215                         text().dispatch(cur, cmd);
216                         // For the paste operations, check if we have
217                         // non-latex_language, and if so, fix.
218                         if (cmd.action() != LFUN_SELF_INSERT)
219                                 fixParagraphLanguage(buffer().params().language);
220                 }
221                 else
222                         InsetCollapsible::doDispatch(cur, cmd);
223                 break;
224
225         default:
226                 InsetCollapsible::doDispatch(cur, cmd);
227                 break;
228         }
229 }
230
231
232 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
233                 FuncStatus & flag) const
234 {
235         switch (cmd.action()) {
236
237         case LFUN_INSET_MODIFY: {
238                 string const first_arg = cmd.getArg(0);
239                 if (first_arg == "changetype") {
240                         string const type = cmd.getArg(1);
241                         flag.setOnOff(type == name_);
242                         if (type == name_) {
243                                 flag.setEnabled(true);
244                                 return true;
245                         }
246                         Layout::LaTeXArgMap args;
247                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
248                         if (insetlayout)
249                                 args = cur.inset().getLayout().latexargs();
250                         else
251                                 args = cur.paragraph().layout().latexargs();
252                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
253                         if (lait != args.end()) {
254                                 flag.setEnabled(true);
255                                 for (auto const & table : cur.paragraph().insetList())
256                                         if (InsetArgument const * ins = table.inset->asInsetArgument())
257                                                 if (ins->name() == type) {
258                                                         // we have this already
259                                                         flag.setEnabled(false);
260                                                         return true;
261                                                 }
262                         } else
263                                 flag.setEnabled(false);
264                         return true;
265                 }
266                 return InsetCollapsible::getStatus(cur, cmd, flag);
267         }
268
269         default:
270                 return InsetCollapsible::getStatus(cur, cmd, flag);
271         }
272 }
273
274
275 string InsetArgument::contextMenuName() const
276 {
277         if (decoration() == InsetDecoration::CONGLOMERATE)
278                 return "context-argument-conglomerate";
279         else
280                 return "context-argument";
281 }
282
283
284 FontInfo InsetArgument::getFont() const
285 {
286         if (font_ != inherit_font)
287                 return font_;
288         return InsetCollapsible::getFont();
289 }
290
291
292 FontInfo InsetArgument::getLabelfont() const
293 {
294         if (labelfont_ != inherit_font)
295                 return labelfont_;
296         return InsetCollapsible::getLabelfont();
297 }
298
299
300 ColorCode InsetArgument::labelColor() const {
301         if (labelfont_.color() != Color_inherit)
302                 return labelfont_.color();
303         return InsetCollapsible::labelColor();
304 }
305
306
307 InsetDecoration InsetArgument::decoration() const
308 {
309         InsetDecoration dec = getLayout().decoration();
310         if (!decoration_.empty())
311                 dec = translateDecoration(decoration_);
312         return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
313 }
314
315
316 void InsetArgument::docbook(XMLStream & xs, OutputParams const & rp) const {
317         // Ignore arguments that have already been output or are planned to be output elsewhere.
318         if (rp.docbook_prepended_arguments.find(this) != rp.docbook_prepended_arguments.end())
319                 return;
320         if (rp.docbook_appended_arguments.find(this) != rp.docbook_appended_arguments.end())
321                 return;
322
323         if (docbooktag_ != from_ascii("NONE") && docbooktag_ != from_ascii("IGNORE")) {
324                 // TODO: implement docbooktagtype_.
325                 xs << xml::StartTag(docbooktag_, docbookattr_);
326                 InsetText::docbook(xs, rp);
327                 xs << xml::EndTag(docbooktag_);
328                 xs << xml::CR();
329         }
330 }
331
332
333 void InsetArgument::latexArgument(otexstream & os,
334                 OutputParams const & runparams_in, docstring const & ldelim,
335                 docstring const & rdelim, docstring const & presetarg) const
336 {
337         otexstringstream ots;
338         OutputParams runparams = runparams_in;
339         if (!pass_thru_chars_.empty())
340                 runparams.pass_thru_chars += pass_thru_chars_;
341         if (!newline_cmd_.empty())
342                 runparams.newlinecmd = newline_cmd_;
343         runparams.pass_thru = isPassThru();
344         InsetText::latex(ots, runparams);
345         TexString ts = ots.release();
346         bool const add_braces = !ldelim.empty() && ldelim != "{"
347                         && support::contains(ts.str, rdelim);
348         os << ldelim;
349         if (add_braces)
350                 os << '{';
351         os << presetarg;
352         if (!presetarg.empty() && !ts.str.empty())
353                 os << ", ";
354         os << move(ts);
355         if (add_braces)
356                 os << '}';
357         os << rdelim;
358 }
359
360
361 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
362                              UpdateType utype, TocBackend & backend) const
363 {
364         if (!caption_of_toc_.empty()) {
365                 docstring str;
366                 text().forOutliner(str, TOC_ENTRY_LENGTH);
367                 backend.builder(caption_of_toc_).argumentItem(str);
368         }
369         // Proceed with the rest of the inset.
370         InsetText::addToToc(dit, output_active, utype, backend);
371 }
372
373
374 void InsetArgument::fixParagraphLanguage(Language const * l)
375 {
376         Font font(inherit_font, l);
377         if (pass_thru_)
378                 font.setLanguage(latex_language);
379         paragraphs().front().resetFonts(font);
380 }
381
382
383 } // namespace lyx