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