]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
Restore XHTML output for InsetListings.
[lyx.git] / src / insets / InsetNote.cpp
1 /**
2  * \file InsetNote.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 Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetNote.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "BufferParams.h"
21 #include "Counters.h"
22 #include "Cursor.h"
23 #include "DispatchResult.h"
24 #include "Exporter.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "InsetIterator.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "MetricsInfo.h"
31 #include "OutputParams.h"
32 #include "ParIterator.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
35
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/Translator.h"
40
41 #include "frontends/Application.h"
42
43 #include <algorithm>
44 #include <sstream>
45
46 using namespace std;
47
48 namespace lyx {
49
50 namespace {
51
52 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
53 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
54
55 NoteTranslator const init_notetranslator()
56 {
57         NoteTranslator translator("Note", InsetNoteParams::Note);
58         translator.addPair("Comment", InsetNoteParams::Comment);
59         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
60         return translator;
61 }
62
63
64 NoteTranslatorLoc const init_notetranslator_loc()
65 {
66         NoteTranslatorLoc translator(_("Note[[InsetNote]]"), InsetNoteParams::Note);
67         translator.addPair(_("Comment"), InsetNoteParams::Comment);
68         translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
69         return translator;
70 }
71
72
73 NoteTranslator const & notetranslator()
74 {
75         static NoteTranslator translator = init_notetranslator();
76         return translator;
77 }
78
79
80 NoteTranslatorLoc const & notetranslator_loc()
81 {
82         static NoteTranslatorLoc translator = init_notetranslator_loc();
83         return translator;
84 }
85
86 } // anon
87
88
89
90
91 InsetNoteParams::InsetNoteParams()
92         : type(Note)
93 {}
94
95
96 void InsetNoteParams::write(ostream & os) const
97 {
98         string const label = notetranslator().find(type);
99         os << "Note " << label << "\n";
100 }
101
102
103 void InsetNoteParams::read(Lexer & lex)
104 {
105         string label;
106         lex >> label;
107         if (lex)
108                 type = notetranslator().find(label);
109 }
110
111
112 /////////////////////////////////////////////////////////////////////
113 //
114 // InsetNote
115 //
116 /////////////////////////////////////////////////////////////////////
117
118 InsetNote::InsetNote(Buffer * buf, string const & label)
119         : InsetCollapsable(buf)
120 {
121         params_.type = notetranslator().find(label);
122 }
123
124
125 InsetNote::~InsetNote()
126 {
127         hideDialogs("note", this);
128 }
129
130
131 docstring InsetNote::name() const 
132 {
133         return from_ascii("Note:" + notetranslator().find(params_.type));
134 }
135
136
137 Inset::DisplayType InsetNote::display() const
138 {
139         return Inline;
140 }
141
142
143 void InsetNote::write(ostream & os) const
144 {
145         params_.write(os);
146         InsetCollapsable::write(os);
147 }
148
149
150 void InsetNote::read(Lexer & lex)
151 {
152         params_.read(lex);
153         InsetCollapsable::read(lex);
154 }
155
156
157 void InsetNote::setButtonLabel()
158 {
159         docstring const label = notetranslator_loc().find(params_.type);
160         setLabel(label);
161 }
162
163
164 bool InsetNote::showInsetDialog(BufferView * bv) const
165 {
166         bv->showDialog("note", params2string(params()),
167                 const_cast<InsetNote *>(this));
168         return true;
169 }
170
171
172 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
173 {
174         switch (cmd.action) {
175
176         case LFUN_INSET_MODIFY:
177                 string2params(to_utf8(cmd.argument()), params_);
178                 setButtonLabel();
179                 break;
180
181         case LFUN_INSET_DIALOG_UPDATE:
182                 cur.bv().updateDialog("note", params2string(params()));
183                 break;
184
185         default:
186                 InsetCollapsable::doDispatch(cur, cmd);
187                 break;
188         }
189 }
190
191
192 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
193                 FuncStatus & flag) const
194 {
195         switch (cmd.action) {
196
197         case LFUN_INSET_MODIFY:
198                 // disallow comment and greyed out in commands
199                 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
200                                 cmd.getArg(2) == "Note");
201                 if (cmd.getArg(0) == "note") {
202                         InsetNoteParams params;
203                         string2params(to_utf8(cmd.argument()), params);
204                         flag.setOnOff(params_.type == params.type);
205                 }
206                 return true;
207
208         case LFUN_INSET_DIALOG_UPDATE:
209                 flag.setEnabled(true);
210                 return true;
211
212         default:
213                 return InsetCollapsable::getStatus(cur, cmd, flag);
214         }
215 }
216
217
218 void InsetNote::addToToc(DocIterator const & cpit)
219 {
220         DocIterator pit = cpit;
221         pit.push_back(CursorSlice(*this));
222
223         Toc & toc = buffer().tocBackend().toc("note");
224         docstring str;
225         str = notetranslator_loc().find(params_.type) + from_ascii(": ")
226                 + getNewLabel(str);
227         toc.push_back(TocItem(pit, 0, str));
228         // Proceed with the rest of the inset.
229         InsetCollapsable::addToToc(cpit);
230 }
231
232
233 bool InsetNote::isMacroScope() const
234 {
235         // LyX note has no latex output
236         if (params_.type == InsetNoteParams::Note)
237                 return true;
238
239         return InsetCollapsable::isMacroScope();
240 }
241
242
243 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
244 {
245         if (params_.type == InsetNoteParams::Note)
246                 return 0;
247
248         OutputParams runparams(runparams_in);
249         if (params_.type == InsetNoteParams::Comment) {
250                 runparams.inComment = true;
251                 // Ignore files that are exported inside a comment
252                 runparams.exportdata.reset(new ExportData);
253         } 
254
255         odocstringstream ss;
256         InsetCollapsable::latex(ss, runparams);
257         // the space after the comment in 'a[comment] b' will be eaten by the
258         // comment environment since the space before b is ignored with the
259         // following latex output:
260         //
261         // a%
262         // \begin{comment}
263         // comment
264         // \end{comment}
265         //  b
266         //
267         // Adding {} before ' b' fixes this.
268         if (params_.type == InsetNoteParams::Comment)
269                 ss << "{}";
270
271         docstring const str = ss.str();
272         os << str;
273         runparams_in.encoding = runparams.encoding;
274         // Return how many newlines we issued.
275         return int(count(str.begin(), str.end(), '\n'));
276 }
277
278
279 int InsetNote::plaintext(odocstream & os,
280                          OutputParams const & runparams_in) const
281 {
282         if (params_.type == InsetNoteParams::Note)
283                 return 0;
284
285         OutputParams runparams(runparams_in);
286         if (params_.type == InsetNoteParams::Comment) {
287                 runparams.inComment = true;
288                 // Ignore files that are exported inside a comment
289                 runparams.exportdata.reset(new ExportData);
290         }
291         os << '[' << buffer().B_("note") << ":\n";
292         InsetText::plaintext(os, runparams);
293         os << "\n]";
294
295         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
296 }
297
298
299 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
300 {
301         if (params_.type == InsetNoteParams::Note)
302                 return 0;
303
304         OutputParams runparams(runparams_in);
305         if (params_.type == InsetNoteParams::Comment) {
306                 os << "<remark>\n";
307                 runparams.inComment = true;
308                 // Ignore files that are exported inside a comment
309                 runparams.exportdata.reset(new ExportData);
310         }
311
312         int const n = InsetText::docbook(os, runparams);
313
314         if (params_.type == InsetNoteParams::Comment)
315                 os << "\n</remark>\n";
316
317         // Return how many newlines we issued.
318         //return int(count(str.begin(), str.end(), '\n'));
319         return n + 1 + 2;
320 }
321
322
323 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
324 {
325         if (params_.type == InsetNoteParams::Note)
326                 return docstring();
327
328         return InsetCollapsable::xhtml(xs, rp);
329 }
330
331
332 void InsetNote::validate(LaTeXFeatures & features) const
333 {
334         switch (params_.type) {
335         case InsetNoteParams::Comment:
336                 features.require("verbatim");
337                 break;
338         case InsetNoteParams::Greyedout:
339                 features.require("color");
340                 features.require("lyxgreyedout");
341                 InsetCollapsable::validate(features);
342                 break;
343         case InsetNoteParams::Note:
344                 break;
345         }
346 }
347
348
349 docstring InsetNote::contextMenu(BufferView const &, int, int) const
350 {
351         return from_ascii("context-note");
352 }
353
354
355 string InsetNote::params2string(InsetNoteParams const & params)
356 {
357         ostringstream data;
358         data << "note" << ' ';
359         params.write(data);
360         return data.str();
361 }
362
363
364 void InsetNote::string2params(string const & in, InsetNoteParams & params)
365 {
366         params = InsetNoteParams();
367
368         if (in.empty())
369                 return;
370
371         istringstream data(in);
372         Lexer lex;
373         lex.setStream(data);
374         lex.setContext("InsetNote::string2params");
375         lex >> "note";
376         // There are cases, such as when we are called via getStatus() from
377         // Dialog::canApply(), where we are just called with "note" rather
378         // than a full "note Note TYPE".
379         if (!lex.isOK())
380                 return;
381         lex >> "Note";
382
383         params.read(lex);
384 }
385
386
387 bool mutateNotes(Cursor & cur, string const & source, string const & target)
388 {
389         InsetNoteParams::Type typeSrc = notetranslator().find(source);
390         InsetNoteParams::Type typeTrt = notetranslator().find(target);
391         // syntax check of arguments
392         string src = notetranslator().find(typeSrc);
393         string trt = notetranslator().find(typeTrt);
394         if (src != source || trt != target)
395                 return false;
396
397         // did we found some conforming inset?
398         bool ret = false;
399
400         cur.beginUndoGroup();
401         Inset & inset = cur.buffer()->inset();
402         InsetIterator it  = inset_iterator_begin(inset);
403         InsetIterator const end = inset_iterator_end(inset);
404         for (; it != end; ++it) {
405                 if (it->lyxCode() == NOTE_CODE) {
406                         InsetNote & ins = static_cast<InsetNote &>(*it);
407                         if (ins.params().type == typeSrc) {
408                                 cur.buffer()->undo().recordUndo(it);
409                                 FuncRequest fr(LFUN_INSET_MODIFY, "note Note " + target);
410                                 ins.dispatch(cur, fr);
411                                 ret = true;
412                         }
413                 }
414         }
415         cur.endUndoGroup();
416
417         return ret;
418 }
419
420 } // namespace lyx