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