]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
Amend 6c3447c8: FindAdv: sometimes a space is added on some math symbols
[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 "ColorSet.h"
21 #include "Cursor.h"
22 #include "Exporter.h"
23 #include "FontInfo.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "LaTeXFeatures.h"
28 #include "LyXRC.h"
29 #include "output_docbook.h"
30 #include "output_latex.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/Lexer.h"
36 #include "support/lstrings.h"
37 #include "support/Translator.h"
38
39 #include "frontends/Application.h"
40
41 #include <algorithm>
42 #include <sstream>
43
44 using namespace std;
45
46 namespace lyx {
47
48 using support::Lexer;
49
50 namespace {
51
52 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
53
54 NoteTranslator const init_notetranslator()
55 {
56         NoteTranslator translator("Note", InsetNoteParams::Note);
57         translator.addPair("Comment", InsetNoteParams::Comment);
58         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
59         return translator;
60 }
61
62
63 NoteTranslator const & notetranslator()
64 {
65         static NoteTranslator const translator = init_notetranslator();
66         return translator;
67 }
68
69
70 } // namespace
71
72
73 InsetNoteParams::InsetNoteParams()
74         : type(Note)
75 {}
76
77
78 void InsetNoteParams::write(ostream & os) const
79 {
80         string const label = notetranslator().find(type);
81         os << "Note " << label << "\n";
82 }
83
84
85 void InsetNoteParams::read(Lexer & lex)
86 {
87         string label;
88         lex >> label;
89         if (lex)
90                 type = notetranslator().find(label);
91 }
92
93
94 /////////////////////////////////////////////////////////////////////
95 //
96 // InsetNote
97 //
98 /////////////////////////////////////////////////////////////////////
99
100 InsetNote::InsetNote(Buffer * buf, string const & label)
101         : InsetCollapsible(buf)
102 {
103         params_.type = notetranslator().find(label);
104 }
105
106
107 InsetNote::~InsetNote()
108 {
109         hideDialogs("note", this);
110 }
111
112
113 docstring InsetNote::layoutName() const
114 {
115         return from_ascii("Note:" + notetranslator().find(params_.type));
116 }
117
118
119 void InsetNote::write(ostream & os) const
120 {
121         params_.write(os);
122         InsetCollapsible::write(os);
123 }
124
125
126 void InsetNote::read(Lexer & lex)
127 {
128         params_.read(lex);
129         InsetCollapsible::read(lex);
130 }
131
132
133 bool InsetNote::showInsetDialog(BufferView * bv) const
134 {
135         bv->showDialog("note", params2string(params()),
136                 const_cast<InsetNote *>(this));
137         return true;
138 }
139
140
141 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
142 {
143         switch (cmd.action()) {
144
145         case LFUN_INSET_MODIFY: {
146                 if (cmd.getArg(0) != "note") {
147                         // not for us; might be handled higher up
148                         cur.undispatched();
149                         return;
150                 }
151                 // Do not do anything if converting to the same type of Note.
152                 // A quick break here is done instead of disabling the LFUN
153                 // because disabling the LFUN would lead to a greyed out
154                 // entry, which might confuse users.
155                 // In the future, we might want to have a radio button for
156                 // switching between notes.
157                 InsetNoteParams params;
158                 string2params(to_utf8(cmd.argument()), params);
159                 if (params_.type == params.type)
160                         break;
161
162                 cur.recordUndoInset(this);
163                 string2params(to_utf8(cmd.argument()), params_);
164                 setButtonLabel();
165                 // what we really want here is a TOC update, but that means
166                 // a full buffer update
167                 cur.forceBufferUpdate();
168                 break;
169         }
170
171         case LFUN_INSET_DIALOG_UPDATE:
172                 cur.bv().updateDialog("note", params2string(params()));
173                 break;
174
175         default:
176                 InsetCollapsible::doDispatch(cur, cmd);
177                 break;
178         }
179 }
180
181
182 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
183                 FuncStatus & flag) const
184 {
185         switch (cmd.action()) {
186
187         case LFUN_INSET_MODIFY:
188                 if (cmd.getArg(0) == "note") {
189                         InsetNoteParams params;
190                         string2params(to_utf8(cmd.argument()), params);
191                         flag.setOnOff(params_.type == params.type);
192                 }
193                 return true;
194
195         case LFUN_INSET_DIALOG_UPDATE:
196                 flag.setEnabled(true);
197                 return true;
198
199         default:
200                 return InsetCollapsible::getStatus(cur, cmd, flag);
201         }
202 }
203
204
205 bool InsetNote::isMacroScope() const
206 {
207         // LyX note has no latex output
208         if (params_.type == InsetNoteParams::Note)
209                 return true;
210
211         return InsetCollapsible::isMacroScope();
212 }
213
214
215 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
216 {
217         if (params_.type != InsetNoteParams::Greyedout
218             && runparams_in.find_effective()
219             && !runparams_in.find_with_non_output())
220                 return;
221
222         if (params_.type == InsetNoteParams::Note) {
223                 if (runparams_in.find_with_non_output()) {
224                         OutputParams runparams(runparams_in);
225                         InsetCollapsible::latex(os, runparams);
226                         runparams_in.encoding = runparams.encoding;
227                 }
228                 return;
229         }
230
231         OutputParams runparams(runparams_in);
232         if (params_.type == InsetNoteParams::Comment) {
233                 if (runparams_in.inComment) {
234                         // Nested comments should just output the contents.
235                         latexParagraphs(buffer(), text(), os, runparams);
236                         return;
237                 }
238
239                 runparams.inComment = true;
240                 // Ignore files that are exported inside a comment
241                 runparams.exportdata.reset(new ExportData);
242         }
243
244         // the space after the comment in 'a[comment] b' will be eaten by the
245         // comment environment since the space before b is ignored with the
246         // following latex output:
247         //
248         // a%
249         // \begin{comment}
250         // comment
251         // \end{comment}
252         //  b
253         //
254         // Adding {} before ' b' fixes this.
255         // The {} will be automatically added, but only if needed, for all
256         // insets whose InsetLayout Display tag is false. This is achieved
257         // by telling otexstream to protect an immediately following space
258         // and is done for both comment and greyedout insets.
259         InsetCollapsible::latex(os, runparams);
260
261         runparams_in.encoding = runparams.encoding;
262 }
263
264
265 int InsetNote::plaintext(odocstringstream & os,
266                          OutputParams const & runparams_in, size_t max_length) const
267 {
268         if (!runparams_in.find_with_non_output()) {
269                 if (params_.type == InsetNoteParams::Note)
270                         return 0;
271                 else if (params_.type == InsetNoteParams::Comment
272                     && runparams_in.find_effective())
273                         return 0;
274         }
275
276         OutputParams runparams(runparams_in);
277         if (params_.type != InsetNoteParams::Greyedout) {
278                 runparams.inComment = true;
279                 // Ignore files that are exported inside a comment
280                 runparams.exportdata.reset(new ExportData);
281         }
282         if (!runparams_in.find_with_non_output())
283                 os << '[' << buffer().B_("note") << ":\n";
284         InsetText::plaintext(os, runparams, max_length);
285         if (!runparams_in.find_with_non_output())
286                 os << "\n]";
287
288         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
289 }
290
291
292 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
293 {
294         if (params_.type == InsetNoteParams::Note)
295                 return;
296
297         OutputParams runparams(runparams_in);
298         if (params_.type == InsetNoteParams::Comment) {
299                 xs << xml::StartTag("remark");
300                 xs << xml::CR();
301                 runparams.inComment = true;
302                 // Ignore files that are exported inside a comment
303                 runparams.exportdata.reset(new ExportData);
304         }
305         // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
306
307         InsetText::docbook(xs, runparams);
308
309         if (params_.type == InsetNoteParams::Comment) {
310                 xs << xml::CR();
311                 xs << xml::EndTag("remark");
312                 xs << xml::CR();
313         }
314 }
315
316
317 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
318 {
319         if (params_.type == InsetNoteParams::Note)
320                 return docstring();
321
322         return InsetCollapsible::xhtml(xs, rp);
323 }
324
325
326 void InsetNote::validate(LaTeXFeatures & features) const
327 {
328         switch (params_.type) {
329         case InsetNoteParams::Comment:
330                 if (features.runparams().flavor == Flavor::Html)
331                         // we do output this but set display to "none" by default,
332                         // but people might want to use it.
333                         InsetCollapsible::validate(features);
334                 else
335                         // Only do the requires
336                         features.useInsetLayout(getLayout());
337                 break;
338         case InsetNoteParams::Greyedout:
339                 if (features.hasRTLLanguage())
340                         features.require("environ");
341                 InsetCollapsible::validate(features);
342                 break;
343         case InsetNoteParams::Note:
344                 // Showing previews in this inset may require stuff
345                 if (features.runparams().for_preview)
346                         InsetCollapsible::validate(features);
347                 break;
348         }
349 }
350
351
352 string InsetNote::contextMenuName() const
353 {
354         return "context-note";
355 }
356
357 bool InsetNote::allowSpellCheck() const
358 {
359         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
360 }
361
362 FontInfo InsetNote::getFont() const
363 {
364         FontInfo font = getLayout().font();
365         if (params_.type == InsetNoteParams::Greyedout
366             && buffer().params().isnotefontcolor) {
367                 ColorCode c = lcolor.getFromLyXName("notefontcolor");
368                 if (c != Color_none)
369                         font.setColor(c);
370                 // This is the local color (not overridden by other documents)
371                 ColorCode lc = lcolor.getFromLyXName("notefontcolor@" + buffer().fileName().absFileName());
372                 if (lc != Color_none)
373                         font.setPaintColor(lc);
374         }
375         return font;
376 }
377
378
379 string InsetNote::params2string(InsetNoteParams const & params)
380 {
381         ostringstream data;
382         data << "note" << ' ';
383         params.write(data);
384         return data.str();
385 }
386
387
388 void InsetNote::string2params(string const & in, InsetNoteParams & params)
389 {
390         params = InsetNoteParams();
391
392         if (in.empty())
393                 return;
394
395         istringstream data(in);
396         Lexer lex;
397         lex.setStream(data);
398         lex.setContext("InsetNote::string2params");
399         lex >> "note";
400         // There are cases, such as when we are called via getStatus() from
401         // Dialog::canApply(), where we are just called with "note" rather
402         // than a full "note Note TYPE".
403         if (!lex.isOK())
404                 return;
405         lex >> "Note";
406
407         params.read(lex);
408 }
409
410
411 } // namespace lyx