]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Track changes in InsetCommands
[lyx.git] / src / insets / InsetCommand.cpp
1 /**
2  * \file InsetCommand.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetCommand.h"
15
16 #include "Buffer.h"
17 #include "BufferEncodings.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "Lexer.h"
25 #include "LyX.h"
26 #include "MetricsInfo.h"
27 #include "texstream.h"
28
29 #include "insets/InsetBox.h"
30 #include "insets/InsetBranch.h"
31 #include "insets/InsetCommand.h"
32 #include "insets/InsetERT.h"
33 #include "insets/InsetExternal.h"
34 #include "insets/InsetFloat.h"
35 #include "insets/InsetGraphics.h"
36 #include "insets/InsetIndex.h"
37 #include "insets/InsetLine.h"
38 #include "insets/InsetListings.h"
39 #include "insets/InsetNote.h"
40 #include "insets/InsetPhantom.h"
41 #include "insets/InsetSpace.h"
42 #include "insets/InsetTabular.h"
43 #include "insets/InsetVSpace.h"
44 #include "insets/InsetWrap.h"
45
46 #include "support/debug.h"
47 #include "support/gettext.h"
48 #include "support/lstrings.h"
49
50 #include "frontends/Application.h"
51
52 #include <sstream>
53
54 using namespace std;
55 using namespace lyx::support;
56
57
58 namespace lyx {
59
60 // FIXME Would it now be possible to use the InsetCode in
61 // place of the mailer name and recover that information?
62 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
63         : Inset(buf), p_(p)
64 {}
65
66
67 // The sole purpose of this copy constructor is to make sure
68 // that the mouse_hover_ map is not copied and remains empty.
69 InsetCommand::InsetCommand(InsetCommand const & rhs)
70         : Inset(rhs), p_(rhs.p_)
71 {}
72
73
74 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
75 {
76         if (&rhs == this)
77                 return *this;
78
79         Inset::operator=(rhs);
80         p_ = rhs.p_;
81         mouse_hover_.clear();
82         button_ = RenderButton();
83
84         return *this;
85 }
86
87
88 InsetCommand::~InsetCommand()
89 {
90         if (p_.code() != NO_CODE)
91                 hideDialogs(insetName(p_.code()), this);
92
93         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
94         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
95         for (; it != end; ++it)
96                 if (it->second)
97                         it->first->clearLastInset(this);
98 }
99
100
101 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
102 {
103         button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0),
104                        inheritFont());
105         button_.metrics(mi, dim);
106 }
107
108
109 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
110         const
111 {
112         mouse_hover_[bv] = mouse_hover;
113         return true;
114 }
115
116
117 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
118 {
119         button_.setRenderState(mouse_hover_[pi.base.bv]);
120         button_.draw(pi, x, y);
121 }
122
123
124 void InsetCommand::setParam(string const & name, docstring const & value)
125 {
126         p_[name] = value;
127 }
128
129
130 docstring const & InsetCommand::getParam(string const & name) const
131 {
132         return p_[name];
133 }
134
135
136 void InsetCommand::setParams(InsetCommandParams const & p)
137 {
138         p_ = p;
139         initView();
140 }
141
142
143 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
144 {
145         OutputParams runparams = runparams_in;
146         docstring command = getCommand(runparams);
147         if (buffer().params().use_minted
148             && prefixIs(command, from_ascii("\\lstlistoflistings")))
149                 command.erase(1, 3);
150         os << command;
151 }
152
153
154 int InsetCommand::plaintext(odocstringstream & os,
155         OutputParams const &, size_t) const
156 {
157         docstring const str = "[" + buffer().B_("LaTeX Command: ")
158                 + from_utf8(getCmdName()) + "]";
159         os << str;
160         return str.size();
161 }
162
163
164 int InsetCommand::docbook(odocstream &, OutputParams const &) const
165 {
166         return 0;
167 }
168
169
170 void InsetCommand::validate(LaTeXFeatures & features) const
171 {
172         if (params().info().hasParam("literal")
173             && params()["literal"] == "true")
174                 return;
175
176         ParamInfo::const_iterator it = params().info().begin();
177         ParamInfo::const_iterator end = params().info().end();
178         for (; it != end; ++it) {
179                 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
180                         docstring const text = params()[it->name()];
181                         // Validate the contents (if we LaTeXify, specific
182                         // macros might require packages)
183                         for (pos_type i = 0; i < int(text.size()) ; ++i)
184                                 BufferEncodings::validate(text[i], features);
185                 }
186         }
187 }
188
189
190 void InsetCommand::changeCmdName(string const & new_name)
191 {
192         string const old_name = getCmdName();
193         if (old_name == new_name)
194                 return;
195
196         if (buffer().masterParams().track_changes) {
197                 // With change tracking, we insert a new inset and
198                 // delete the old one
199                 InsetCommandParams p(p_.code());
200                 p = p_;
201                 p.setCmdName(new_name);
202                 string const data = InsetCommand::params2string(p);
203                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
204                 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
205         } else
206                 p_.setCmdName(new_name);
207 }
208
209
210 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
211 {
212         switch (cmd.action()) {
213         case LFUN_INSET_MODIFY: {
214                 if (cmd.getArg(0) == "changetype") {
215                         cur.recordUndo();
216                         changeCmdName(cmd.getArg(1));
217                         cur.forceBufferUpdate();
218                         initView();
219                         break;
220                 }
221                 InsetCommandParams p(p_.code());
222                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
223                 if (p.getCmdName().empty())
224                         cur.noScreenUpdate();
225                 else {
226                         cur.recordUndo();
227                         if (buffer().masterParams().track_changes) {
228                                 // With change tracking, we insert a new inset and
229                                 // delete the old one
230                                 string const data = InsetCommand::params2string(p);
231                                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
232                                 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
233                         } else
234                                 setParams(p);
235                 }
236                 // FIXME We might also want to check here if this one is in the TOC.
237                 // But I think most of those are labeled.
238                 if (isLabeled())
239                         cur.forceBufferUpdate();
240                 break;
241         }
242
243         case LFUN_INSET_DIALOG_UPDATE: {
244                 string const name = to_utf8(cmd.argument());
245                 cur.bv().updateDialog(name, params2string(params()));
246                 break;
247         }
248
249         default:
250                 Inset::doDispatch(cur, cmd);
251                 break;
252         }
253
254 }
255
256
257 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
258         FuncStatus & status) const
259 {
260         switch (cmd.action()) {
261         // suppress these
262         case LFUN_ERT_INSERT:
263                 status.setEnabled(false);
264                 return true;
265
266         // we handle these
267         case LFUN_INSET_MODIFY:
268                 if (cmd.getArg(0) == "changetype") {
269                         string const newtype = cmd.getArg(1);
270                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
271                         status.setOnOff(newtype == p_.getCmdName());
272                 }
273                 status.setEnabled(true);
274                 return true;
275
276         case LFUN_INSET_DIALOG_UPDATE:
277                 status.setEnabled(true);
278                 return true;
279
280         default:
281                 return Inset::getStatus(cur, cmd, status);
282         }
283 }
284
285
286 string InsetCommand::contextMenuName() const
287 {
288         return "context-" + insetName(p_.code());
289 }
290
291
292 bool InsetCommand::showInsetDialog(BufferView * bv) const
293 {
294         if (p_.code() != NO_CODE)
295                 bv->showDialog(insetName(p_.code()), params2string(p_),
296                         const_cast<InsetCommand *>(this));
297         return true;
298 }
299
300
301 bool InsetCommand::string2params(string const & data,
302         InsetCommandParams & params)
303 {
304         params.clear();
305         if (data.empty())
306                 return false;
307         // This happens when inset-insert is called without argument except for the
308         // inset type; ex:
309         // "inset-insert toc"
310         string const name = insetName(params.code());
311         if (data == name)
312                 return true;
313         istringstream dstream(data);
314         Lexer lex;
315         lex.setStream(dstream);
316         lex.setContext("InsetCommand::string2params");
317         lex >> name.c_str(); // check for name
318         lex >> "CommandInset";
319         params.read(lex);
320         return true;
321 }
322
323
324 string InsetCommand::params2string(InsetCommandParams const & params)
325 {
326         ostringstream data;
327         data << insetName(params.code()) << ' ';
328         params.write(data);
329         data << "\\end_inset\n";
330         return data.str();
331 }
332
333
334 bool decodeInsetParam(string const & name, string & data,
335         Buffer const & buffer)
336 {
337         InsetCode const code = insetCode(name);
338         switch (code) {
339         case BIBITEM_CODE:
340         case BIBTEX_CODE:
341         case INDEX_PRINT_CODE:
342         case LABEL_CODE:
343         case LINE_CODE:
344         case NOMENCL_CODE:
345         case NOMENCL_PRINT_CODE:
346         case REF_CODE:
347         case TOC_CODE:
348         case HYPERLINK_CODE: {
349                 InsetCommandParams p(code);
350                 data = InsetCommand::params2string(p);
351                 break;
352         }
353         case INCLUDE_CODE: {
354                 // data is the include type: one of "include",
355                 // "input", "verbatiminput" or "verbatiminput*"
356                 if (data.empty())
357                         // default type is requested
358                         data = "include";
359                 InsetCommandParams p(INCLUDE_CODE, data);
360                 data = InsetCommand::params2string(p);
361                 break;
362         }
363         case BOX_CODE: {
364                 // \c data == "Boxed" || "Frameless" etc
365                 InsetBoxParams p(data);
366                 data = InsetBox::params2string(p);
367                 break;
368         }
369         case BRANCH_CODE: {
370                 InsetBranchParams p;
371                 data = InsetBranch::params2string(p);
372                 break;
373         }
374         case CITE_CODE: {
375                 InsetCommandParams p(CITE_CODE);
376                 data = InsetCommand::params2string(p);
377                 break;
378         }
379         case ERT_CODE: {
380                 data = InsetERT::params2string(InsetCollapsible::Open);
381                 break;
382         }
383         case EXTERNAL_CODE: {
384                 InsetExternalParams p;
385                 data = InsetExternal::params2string(p, buffer);
386                 break;
387         }
388         case FLOAT_CODE:  {
389                 InsetFloatParams p;
390                 data = InsetFloat::params2string(p);
391                 break;
392         }
393         case INDEX_CODE: {
394                 InsetIndexParams p;
395                 data = InsetIndex::params2string(p);
396                 break;
397         }
398         case LISTINGS_CODE: {
399                 InsetListingsParams p;
400                 data = InsetListings::params2string(p);
401                 break;
402         }
403         case GRAPHICS_CODE: {
404                 InsetGraphicsParams p;
405                 data = InsetGraphics::params2string(p, buffer);
406                 break;
407         }
408         case MATH_SPACE_CODE: {
409                 InsetSpaceParams p(true);
410                 data = InsetSpace::params2string(p);
411                 break;
412         }
413         case NOTE_CODE: {
414                 InsetNoteParams p;
415                 data = InsetNote::params2string(p);
416                 break;
417         }
418         case PHANTOM_CODE: {
419                 InsetPhantomParams p;
420                 data = InsetPhantom::params2string(p);
421                 break;
422         }
423         case SPACE_CODE: {
424                 InsetSpaceParams p;
425                 data = InsetSpace::params2string(p);
426                 break;
427         }
428         case VSPACE_CODE: {
429                 VSpace space;
430                 data = InsetVSpace::params2string(space);
431                 break;
432         }
433         case WRAP_CODE: {
434                 InsetWrapParams p;
435                 data = InsetWrap::params2string(p);
436                 break;
437         }
438         default:
439                 return false;
440         } // end switch(code)
441         return true;
442 }
443
444 } // namespace lyx