]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
Simplify ERT by using verbatim
[features.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
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 "InsetERT.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "debug.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "gettext.h"
25 #include "Language.h"
26 #include "Layout.h"
27 #include "Color.h"
28 #include "LyXAction.h"
29 #include "Lexer.h"
30 #include "TextClass.h"
31 #include "MetricsInfo.h"
32 #include "ParagraphParameters.h"
33 #include "Paragraph.h"
34
35 #include "frontends/alert.h"
36
37 #include <sstream>
38
39
40 namespace lyx {
41
42 using support::token;
43
44 using std::endl;
45 using std::min;
46
47 using std::istringstream;
48 using std::ostream;
49 using std::ostringstream;
50 using std::string;
51
52
53 void InsetERT::init()
54 {
55         setButtonLabel();
56         setLabelFont(layout_.labelfont);
57         // FIXME: what to do with those?
58         //text_.current_font.setLanguage(latex_language);
59         //text_.real_current_font.setLanguage(latex_language);
60 }
61
62
63 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
64         : InsetCollapsable(bp, status)
65 {
66         setLayout(bp);
67         init();
68 }
69
70
71 InsetERT::InsetERT(InsetERT const & in)
72         : InsetCollapsable(in)
73 {
74         init();
75 }
76
77
78 Inset * InsetERT::clone() const
79 {
80         return new InsetERT(*this);
81 }
82
83
84 #if 0
85 InsetERT::InsetERT(BufferParams const & bp,
86                    Language const *, string const & contents, CollapseStatus status)
87         : InsetCollapsable(bp, status)
88 {
89         Font font(Font::ALL_INHERIT, latex_language);
90         paragraphs().begin()->insert(0, contents, font);
91
92         // the init has to be after the initialization of the paragraph
93         // because of the label settings (draw_label for ert insets).
94         init();
95 }
96 #endif
97
98
99 InsetERT::~InsetERT()
100 {
101         InsetERTMailer(*this).hideDialog();
102 }
103
104
105 void InsetERT::write(Buffer const & buf, ostream & os) const
106 {
107         os << "ERT" << "\n";
108         InsetCollapsable::write(buf, os);
109 }
110
111
112 void InsetERT::read(Buffer const & buf, Lexer & lex)
113 {
114         InsetCollapsable::read(buf, lex);
115
116         // Force default font
117         // This avoids paragraphs in buffer language that would have a
118         // foreign language after a document langauge change, and it ensures
119         // that all new text in ERT gets the "latex" language, since new text
120         // inherits the language from the last position of the existing text.
121         // As a side effect this makes us also robust against bugs in LyX
122         // that might lead to font changes in ERT in .lyx files.
123         Font font(Font::ALL_INHERIT, latex_language);
124         ParagraphList::iterator par = paragraphs().begin();
125         ParagraphList::iterator const end = paragraphs().end();
126         while (par != end) {
127                 pos_type siz = par->size();
128                 for (pos_type i = 0; i <= siz; ++i) {
129                         par->setFont(i, font);
130                 }
131                 ++par;
132         }
133 }
134
135
136 docstring const InsetERT::editMessage() const
137 {
138         return _("Opened ERT Inset");
139 }
140
141
142 int InsetERT::latex(Buffer const & buf, odocstream & os,
143                     OutputParams const & op) const
144 {
145         return InsetCollapsable::latex(buf, os, op);
146 }
147
148
149 int InsetERT::plaintext(Buffer const &, odocstream &,
150                         OutputParams const &) const
151 {
152         return 0; // do not output TeX code
153 }
154
155
156 int InsetERT::docbook(Buffer const &, odocstream & os,
157                       OutputParams const &) const
158 {
159         // FIXME can we do the same thing here as for LaTeX?
160         ParagraphList::const_iterator par = paragraphs().begin();
161         ParagraphList::const_iterator end = paragraphs().end();
162
163         int lines = 0;
164         while (par != end) {
165                 pos_type siz = par->size();
166                 for (pos_type i = 0; i < siz; ++i)
167                         os.put(par->getChar(i));
168                 ++par;
169                 if (par != end) {
170                         os << "\n";
171                         ++lines;
172                 }
173         }
174
175         return lines;
176 }
177
178
179 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
180 {
181         BufferParams const & bp = cur.buffer().params();
182         LayoutPtr const layout =
183                         bp.getTextClass().defaultLayout();
184         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
185         switch (cmd.action) {
186
187         case LFUN_MOUSE_PRESS:
188                 if (cmd.button() != mouse_button::button3)
189                         InsetCollapsable::doDispatch(cur, cmd);
190                 else
191                         // This makes the cursor leave the
192                         // inset when it collapses on mouse-3
193                         cur.undispatched();
194                 break;
195
196         case LFUN_QUOTE_INSERT: {
197                 // We need to bypass the fancy quotes in Text
198                 FuncRequest f(LFUN_SELF_INSERT, "\"");
199                 dispatch(cur, f);
200                 break;
201         }
202         case LFUN_INSET_MODIFY: {
203                 InsetCollapsable::CollapseStatus st;
204                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
205                 setStatus(cur, st);
206                 break;
207         }
208         case LFUN_PASTE:
209         case LFUN_CLIPBOARD_PASTE:
210         case LFUN_PRIMARY_SELECTION_PASTE: {
211                 InsetCollapsable::doDispatch(cur, cmd);
212
213                 // Since we can only store plain text, we must reset all
214                 // attributes.
215                 // FIXME: Change only the pasted paragraphs
216
217                 Font font = layout->font;
218                 // ERT contents has always latex_language
219                 font.setLanguage(latex_language);
220                 ParagraphList::iterator const end = paragraphs().end();
221                 for (ParagraphList::iterator par = paragraphs().begin();
222                      par != end; ++par) {
223                         // in case par had a manual label
224                         par->setBeginOfBody();
225                         pos_type const siz = par->size();
226                         for (pos_type i = 0; i < siz; ++i) {
227                                 par->setFont(i, font);
228                         }
229                         par->params().clear();
230                 }
231                 break;
232         }
233         default:
234                 // Force any new text to latex_language
235                 // FIXME: This should only be necessary in init(), but
236                 // new paragraphs that are created by pressing enter at the
237                 // start of an existing paragraph get the buffer language
238                 // and not latex_language, so we take this brute force
239                 // approach.
240                 cur.current_font = layout->font;
241                 cur.real_current_font = layout->font;
242                 cur.current_font.setLanguage(latex_language);
243                 cur.real_current_font.setLanguage(latex_language);
244                 InsetCollapsable::doDispatch(cur, cmd);
245                 break;
246         }
247 }
248
249
250 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
251         FuncStatus & status) const
252 {
253         switch (cmd.action) {
254                 // suppress these
255                 case LFUN_ACCENT_ACUTE:
256                 case LFUN_ACCENT_BREVE:
257                 case LFUN_ACCENT_CARON:
258                 case LFUN_ACCENT_CEDILLA:
259                 case LFUN_ACCENT_CIRCLE:
260                 case LFUN_ACCENT_CIRCUMFLEX:
261                 case LFUN_ACCENT_DOT:
262                 case LFUN_ACCENT_GRAVE:
263                 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
264                 case LFUN_ACCENT_MACRON:
265                 case LFUN_ACCENT_OGONEK:
266                 case LFUN_ACCENT_SPECIAL_CARON:
267                 case LFUN_ACCENT_TIE:
268                 case LFUN_ACCENT_TILDE:
269                 case LFUN_ACCENT_UMLAUT:
270                 case LFUN_ACCENT_UNDERBAR:
271                 case LFUN_ACCENT_UNDERDOT:
272                 case LFUN_APPENDIX:
273                 case LFUN_BIBITEM_INSERT:
274                 case LFUN_BOX_INSERT:
275                 case LFUN_BRANCH_INSERT:
276                 case LFUN_BREAK_LINE:
277                 case LFUN_CAPTION_INSERT:
278                 case LFUN_CLEARPAGE_INSERT:
279                 case LFUN_CLEARDOUBLEPAGE_INSERT:
280                 case LFUN_DEPTH_DECREMENT:
281                 case LFUN_DEPTH_INCREMENT:
282                 case LFUN_DOTS_INSERT:
283                 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
284                 case LFUN_ENVIRONMENT_INSERT:
285                 case LFUN_ERT_INSERT:
286                 case LFUN_FILE_INSERT:
287                 case LFUN_FLEX_INSERT:
288                 case LFUN_FLOAT_INSERT:
289                 case LFUN_FLOAT_LIST:
290                 case LFUN_FLOAT_WIDE_INSERT:
291                 case LFUN_FONT_BOLD:
292                 case LFUN_FONT_TYPEWRITER:
293                 case LFUN_FONT_DEFAULT:
294                 case LFUN_FONT_EMPH:
295                 case LFUN_FONT_FREE_APPLY:
296                 case LFUN_FONT_FREE_UPDATE:
297                 case LFUN_FONT_NOUN:
298                 case LFUN_FONT_ROMAN:
299                 case LFUN_FONT_SANS:
300                 case LFUN_FONT_FRAK:
301                 case LFUN_FONT_ITAL:
302                 case LFUN_FONT_SIZE:
303                 case LFUN_FONT_STATE:
304                 case LFUN_FONT_UNDERLINE:
305                 case LFUN_FOOTNOTE_INSERT:
306                 case LFUN_HFILL_INSERT:
307                 case LFUN_HYPERLINK_INSERT:
308                 case LFUN_HYPHENATION_POINT_INSERT:
309                 case LFUN_INDEX_INSERT:
310                 case LFUN_INDEX_PRINT:
311                 case LFUN_INSET_INSERT:
312                 case LFUN_LABEL_GOTO:
313                 case LFUN_LABEL_INSERT:
314                 case LFUN_LIGATURE_BREAK_INSERT:
315                 case LFUN_LINE_INSERT:
316                 case LFUN_PAGEBREAK_INSERT:
317                 case LFUN_LANGUAGE:
318                 case LFUN_LAYOUT:
319                 case LFUN_LAYOUT_PARAGRAPH:
320                 case LFUN_LAYOUT_TABULAR:
321                 case LFUN_MARGINALNOTE_INSERT:
322                 case LFUN_MATH_DISPLAY:
323                 case LFUN_MATH_INSERT:
324                 case LFUN_MATH_MATRIX:
325                 case LFUN_MATH_MODE:
326                 case LFUN_MENU_OPEN:
327                 case LFUN_MENU_SEPARATOR_INSERT:
328                 case LFUN_NOACTION:
329                 case LFUN_NOMENCL_INSERT:
330                 case LFUN_NOMENCL_PRINT:
331                 case LFUN_NOTE_INSERT:
332                 case LFUN_NOTE_NEXT:
333                 case LFUN_OPTIONAL_INSERT:
334                 case LFUN_PARAGRAPH_PARAMS:
335                 case LFUN_PARAGRAPH_PARAMS_APPLY:
336                 case LFUN_PARAGRAPH_SPACING:
337                 case LFUN_PARAGRAPH_UPDATE:
338                 case LFUN_REFERENCE_NEXT:
339                 case LFUN_SERVER_GOTO_FILE_ROW:
340                 case LFUN_SERVER_NOTIFY:
341                 case LFUN_SERVER_SET_XY:
342                 case LFUN_SPACE_INSERT:
343                 case LFUN_TABULAR_INSERT:
344                 case LFUN_TOC_INSERT:
345                 case LFUN_WRAP_INSERT:
346                         status.enabled(false);
347                         return true;
348
349                 case LFUN_CLIPBOARD_PASTE:
350                 case LFUN_INSET_MODIFY:
351                 case LFUN_PASTE:
352                 case LFUN_PRIMARY_SELECTION_PASTE:
353                 case LFUN_QUOTE_INSERT:
354                         status.enabled(true);
355                         return true;
356
357                 // this one is difficult to get right. As a half-baked
358                 // solution, we consider only the first action of the sequence
359                 case LFUN_COMMAND_SEQUENCE: {
360                         // argument contains ';'-terminated commands
361                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
362                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
363                         func.origin = cmd.origin;
364                         return getStatus(cur, func, status);
365                 }
366
367                 default:
368                         return InsetCollapsable::getStatus(cur, cmd, status);
369         }
370 }
371
372
373 void InsetERT::setButtonLabel()
374 {
375         // FIXME UNICODE
376         if (decoration() == Classic)
377                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
378         else
379                 setLabel(getNewLabel(_("ERT")));
380 }
381
382
383 bool InsetERT::insetAllowed(InsetCode /* code */) const
384 {
385         return false;
386 }
387
388
389 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
390 {
391         Font tmpfont = mi.base.font;
392         getDrawFont(mi.base.font);
393         mi.base.font.realize(tmpfont);
394         InsetCollapsable::metrics(mi, dim);
395         mi.base.font = tmpfont;
396 }
397
398
399 void InsetERT::draw(PainterInfo & pi, int x, int y) const
400 {
401         Font tmpfont = pi.base.font;
402         getDrawFont(pi.base.font);
403         pi.base.font.realize(tmpfont);
404         const_cast<InsetERT &>(*this).setButtonLabel();
405         InsetCollapsable::draw(pi, x, y);
406         pi.base.font = tmpfont;
407 }
408
409
410 bool InsetERT::showInsetDialog(BufferView * bv) const
411 {
412         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
413         return true;
414 }
415
416
417 void InsetERT::getDrawFont(Font & font) const
418 {
419         font = Font(Font::ALL_INHERIT, latex_language);
420         font.realize(layout_.font);
421 }
422
423
424 string const InsetERTMailer::name_("ert");
425
426 InsetERTMailer::InsetERTMailer(InsetERT & inset)
427         : inset_(inset)
428 {}
429
430
431 string const InsetERTMailer::inset2string(Buffer const &) const
432 {
433         return params2string(inset_.status());
434 }
435
436
437 void InsetERTMailer::string2params(string const & in,
438                                    InsetCollapsable::CollapseStatus & status)
439 {
440         status = InsetCollapsable::Collapsed;
441         if (in.empty())
442                 return;
443
444         istringstream data(in);
445         Lexer lex(0,0);
446         lex.setStream(data);
447
448         string name;
449         lex >> name;
450         if (name != name_)
451                 return print_mailer_error("InsetERTMailer", in, 1, name_);
452
453         int s;
454         lex >> s;
455         if (lex)
456                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
457 }
458
459
460 string const
461 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
462 {
463         ostringstream data;
464         data << name_ << ' ' << status;
465         return data.str();
466 }
467
468
469 } // namespace lyx