]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
Rename ascii to plaintext and LatexRunParams to OutputParams.
[lyx.git] / src / paragraph_funcs.C
1 /**
2  * \file paragraph_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "paragraph_funcs.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17
18 #include "debug.h"
19 #include "encoding.h"
20 #include "errorlist.h"
21 #include "factory.h"
22 #include "gettext.h"
23 #include "iterators.h"
24 #include "language.h"
25 #include "lyxlex.h"
26 #include "lyxrc.h"
27 #include "outputparams.h"
28 #include "paragraph_pimpl.h"
29 #include "sgml.h"
30 #include "texrow.h"
31 #include "vspace.h"
32
33 #include "insets/insetbibitem.h"
34 #include "insets/insethfill.h"
35 #include "insets/insetlatexaccent.h"
36 #include "insets/insetline.h"
37 #include "insets/insetnewline.h"
38 #include "insets/insetpagebreak.h"
39 #include "insets/insetoptarg.h"
40 #include "insets/insetspace.h"
41 #include "insets/insetspecialchar.h"
42 #include "insets/insettabular.h"
43
44 #include "support/filetools.h"
45 #include "support/lstrings.h"
46 #include "support/lyxlib.h"
47 #include "support/std_sstream.h"
48
49 #include <vector>
50
51 using lyx::pos_type;
52
53 using lyx::support::ascii_lowercase;
54 using lyx::support::atoi;
55 using lyx::support::bformat;
56 using lyx::support::compare_ascii_no_case;
57 using lyx::support::compare_no_case;
58 using lyx::support::contains;
59 using lyx::support::split;
60 using lyx::support::subst;
61
62 using std::auto_ptr;
63 using std::endl;
64 using std::string;
65 using std::vector;
66 using std::istringstream;
67 using std::ostream;
68 using std::pair;
69
70
71 namespace {
72
73 bool moveItem(Paragraph & from, Paragraph & to,
74         BufferParams const & params, pos_type i, pos_type j)
75 {
76         char const tmpchar = from.getChar(i);
77         LyXFont tmpfont = from.getFontSettings(params, i);
78
79         if (tmpchar == Paragraph::META_INSET) {
80                 InsetOld * tmpinset = 0;
81                 if (from.getInset(i)) {
82                         // the inset is not in a paragraph anymore
83                         tmpinset = from.insetlist.release(i);
84                 }
85
86                 if (!to.insetAllowed(tmpinset->lyxCode()))
87                         return false;
88                 to.insertInset(j, tmpinset, tmpfont);
89         } else {
90                 if (!to.checkInsertChar(tmpfont))
91                         return false;
92                 to.insertChar(j, tmpchar, tmpfont);
93         }
94         return true;
95 }
96
97 }
98
99
100 void breakParagraph(BufferParams const & bparams,
101                     ParagraphList & paragraphs,
102                     ParagraphList::iterator par,
103                     pos_type pos,
104                     int flag)
105 {
106         // create a new paragraph, and insert into the list
107         ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
108                                                         Paragraph());
109
110         // without doing that we get a crash when typing <Return> at the
111         // end of a paragraph
112         tmp->layout(bparams.getLyXTextClass().defaultLayout());
113         // remember to set the inset_owner
114         tmp->setInsetOwner(par->inInset());
115
116         if (bparams.tracking_changes)
117                 tmp->trackChanges();
118
119         // this is an idea for a more userfriendly layout handling, I will
120         // see what the users say
121
122         // layout stays the same with latex-environments
123         if (flag) {
124                 tmp->layout(par->layout());
125                 tmp->setLabelWidthString(par->params().labelWidthString());
126         }
127
128         bool const isempty = (par->allowEmpty() && par->empty());
129
130         if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
131                 tmp->layout(par->layout());
132                 tmp->params().align(par->params().align());
133                 tmp->setLabelWidthString(par->params().labelWidthString());
134
135                 tmp->params().spaceBottom(par->params().spaceBottom());
136                 par->params().spaceBottom(VSpace(VSpace::NONE));
137
138                 tmp->params().depth(par->params().depth());
139                 tmp->params().noindent(par->params().noindent());
140
141                 // copy everything behind the break-position
142                 // to the new paragraph
143
144 #ifdef WITH_WARNINGS
145 #warning this seems wrong
146 #endif
147                 /* FIXME: if !keepempty, empty() == true, then we reach
148                  * here with size() == 0. So pos_end becomes - 1. Why
149                  * doesn't this cause problems ???
150                  */
151                 pos_type pos_end = par->size() - 1;
152                 pos_type i = pos;
153                 pos_type j = pos;
154
155                 for (; i <= pos_end; ++i) {
156                         Change::Type change = par->lookupChange(i);
157                         if (moveItem(*par, *tmp, bparams, i, j - pos)) {
158                                 tmp->setChange(j - pos, change);
159                                 ++j;
160                         }
161                 }
162
163                 for (i = pos_end; i >= pos; --i)
164                         par->eraseIntern(i);
165         }
166
167         if (pos)
168                 return;
169
170         tmp->params().spaceTop(par->params().spaceTop());
171         par->params().clear();
172
173         par->layout(bparams.getLyXTextClass().defaultLayout());
174
175         // layout stays the same with latex-environments
176         if (flag) {
177                 par->layout(tmp->layout());
178                 par->setLabelWidthString(tmp->params().labelWidthString());
179                 par->params().depth(tmp->params().depth());
180         }
181
182         // subtle, but needed to get empty pars working right
183         if (bparams.tracking_changes) {
184                 if (!par->size()) {
185                         par->cleanChanges();
186                 } else if (!tmp->size()) {
187                         tmp->cleanChanges();
188                 }
189         }
190 }
191
192
193 void breakParagraphConservative(BufferParams const & bparams,
194                                 ParagraphList & paragraphs,
195                                 ParagraphList::iterator par,
196                                 pos_type pos)
197 {
198         // create a new paragraph
199         ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
200                                                         Paragraph());
201         tmp->makeSameLayout(*par);
202
203         // When can pos > size()?
204         // I guess pos == size() is possible.
205         if (par->size() > pos) {
206                 // copy everything behind the break-position to the new
207                 // paragraph
208                 pos_type pos_end = par->size() - 1;
209
210                 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
211                         if (moveItem(*par, *tmp, bparams, i, j - pos))
212                                 ++j;
213
214                 for (pos_type k = pos_end; k >= pos; --k)
215                         par->erase(k);
216         }
217 }
218
219
220 void mergeParagraph(BufferParams const & bparams,
221                     ParagraphList & paragraphs,
222                     ParagraphList::iterator par)
223 {
224         ParagraphList::iterator the_next = boost::next(par);
225
226         // first the DTP-stuff
227         par->params().spaceBottom(the_next->params().spaceBottom());
228
229         pos_type pos_end = the_next->size() - 1;
230         pos_type pos_insert = par->size();
231
232         // ok, now copy the paragraph
233         for (pos_type i = 0, j = 0; i <= pos_end; ++i)
234                 if (moveItem(*the_next, *par, bparams, i, pos_insert + j))
235                         ++j;
236
237         paragraphs.erase(the_next);
238 }
239
240
241 ParagraphList::iterator depthHook(ParagraphList::iterator pit,
242                                   ParagraphList const & plist,
243                                   Paragraph::depth_type depth)
244 {
245         ParagraphList::iterator newpit = pit;
246         ParagraphList::iterator beg = const_cast<ParagraphList&>(plist).begin();
247
248         if (newpit != beg)
249                 --newpit;
250
251         while (newpit != beg && newpit->getDepth() > depth) {
252                 --newpit;
253         }
254
255         if (newpit->getDepth() > depth)
256                 return pit;
257
258         return newpit;
259 }
260
261
262 ParagraphList::iterator outerHook(ParagraphList::iterator pit,
263                                   ParagraphList const & plist)
264 {
265         if (!pit->getDepth())
266                 return const_cast<ParagraphList&>(plist).end();
267         return depthHook(pit, plist,
268                          Paragraph::depth_type(pit->getDepth() - 1));
269 }
270
271
272 bool isFirstInSequence(ParagraphList::iterator pit,
273                        ParagraphList const & plist)
274 {
275         ParagraphList::iterator dhook = depthHook(pit, plist, pit->getDepth());
276         return (dhook == pit
277                 || dhook->layout() != pit->layout()
278                 || dhook->getDepth() != pit->getDepth());
279 }
280
281
282 int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist)
283 {
284         ParagraphList::iterator pit = p;
285         Paragraph::depth_type par_depth = p->getDepth();
286         while (pit != const_cast<ParagraphList&>(plist).end()) {
287                 LyXLayout_ptr const & layout = pit->layout();
288                 int const endlabeltype = layout->endlabeltype;
289
290                 if (endlabeltype != END_LABEL_NO_LABEL) {
291                         if (boost::next(p) == const_cast<ParagraphList&>(plist).end())
292                                 return endlabeltype;
293
294                         Paragraph::depth_type const next_depth = boost::next(p)->getDepth();
295                         if (par_depth > next_depth ||
296                             (par_depth == next_depth &&
297                              layout != boost::next(p)->layout()))
298                                 return endlabeltype;
299                         break;
300                 }
301                 if (par_depth == 0)
302                         break;
303                 pit = outerHook(pit, plist);
304                 if (pit != const_cast<ParagraphList&>(plist).end())
305                         par_depth = pit->getDepth();
306         }
307         return END_LABEL_NO_LABEL;
308 }
309
310
311 namespace {
312
313 int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
314 {
315         static LyXFont font;
316         static Change change;
317
318         BufferParams const & bp = buf.params();
319
320         if (token[0] != '\\') {
321                 string::const_iterator cit = token.begin();
322                 for (; cit != token.end(); ++cit) {
323                         par.insertChar(par.size(), (*cit), font, change);
324                 }
325         } else if (token == "\\begin_layout") {
326                 lex.eatLine();
327                 string layoutname = lex.getString();
328
329                 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
330                 change = Change();
331
332                 LyXTextClass const & tclass = bp.getLyXTextClass();
333
334                 if (layoutname.empty()) {
335                         layoutname = tclass.defaultLayoutName();
336                 }
337
338                 bool hasLayout = tclass.hasLayout(layoutname);
339
340                 if (!hasLayout) {
341                         lyxerr << "Layout '" << layoutname << "' does not"
342                                << " exist in textclass '" << tclass.name()
343                                << "'." << endl;
344                         lyxerr << "Trying to use default layout instead."
345                                << endl;
346                         layoutname = tclass.defaultLayoutName();
347                 }
348
349                 par.layout(bp.getLyXTextClass()[layoutname]);
350
351                 // Test whether the layout is obsolete.
352                 LyXLayout_ptr const & layout = par.layout();
353                 if (!layout->obsoleted_by().empty())
354                         par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
355
356                 par.params().read(lex);
357
358         } else if (token == "\\end_layout") {
359                 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
360                        << "Missing \\begin_layout?.\n";
361         } else if (token == "\\end_inset") {
362                 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
363                        << "Missing \\begin_inset?.\n";
364         } else if (token == "\\begin_inset") {
365                 InsetOld * inset = readInset(lex, buf);
366                 if (inset)
367                         par.insertInset(par.size(), inset, font, change);
368                 else {
369                         lex.eatLine();
370                         string line = lex.getString();
371                         buf.error(ErrorItem(_("Unknown Inset"), line,
372                                             buf.paragraphs().back().id(),
373                                             0, par.size()));
374                         return 1;
375                 }
376         } else if (token == "\\family") {
377                 lex.next();
378                 font.setLyXFamily(lex.getString());
379         } else if (token == "\\series") {
380                 lex.next();
381                 font.setLyXSeries(lex.getString());
382         } else if (token == "\\shape") {
383                 lex.next();
384                 font.setLyXShape(lex.getString());
385         } else if (token == "\\size") {
386                 lex.next();
387                 font.setLyXSize(lex.getString());
388         } else if (token == "\\lang") {
389                 lex.next();
390                 string const tok = lex.getString();
391                 Language const * lang = languages.getLanguage(tok);
392                 if (lang) {
393                         font.setLanguage(lang);
394                 } else {
395                         font.setLanguage(bp.language);
396                         lex.printError("Unknown language `$$Token'");
397                 }
398         } else if (token == "\\numeric") {
399                 lex.next();
400                 font.setNumber(font.setLyXMisc(lex.getString()));
401         } else if (token == "\\emph") {
402                 lex.next();
403                 font.setEmph(font.setLyXMisc(lex.getString()));
404         } else if (token == "\\bar") {
405                 lex.next();
406                 string const tok = lex.getString();
407
408                 if (tok == "under")
409                         font.setUnderbar(LyXFont::ON);
410                 else if (tok == "no")
411                         font.setUnderbar(LyXFont::OFF);
412                 else if (tok == "default")
413                         font.setUnderbar(LyXFont::INHERIT);
414                 else
415                         lex.printError("Unknown bar font flag "
416                                        "`$$Token'");
417         } else if (token == "\\noun") {
418                 lex.next();
419                 font.setNoun(font.setLyXMisc(lex.getString()));
420         } else if (token == "\\color") {
421                 lex.next();
422                 font.setLyXColor(lex.getString());
423         } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
424
425                 // Insets don't make sense in a free-spacing context! ---Kayvan
426                 if (par.isFreeSpacing()) {
427                         if (token == "\\InsetSpace")
428                                 par.insertChar(par.size(), ' ', font, change);
429                         else if (lex.isOK()) {
430                                 lex.next();
431                                 string const next_token = lex.getString();
432                                 if (next_token == "\\-")
433                                         par.insertChar(par.size(), '-', font, change);
434                                 else {
435                                         lex.printError("Token `$$Token' "
436                                                        "is in free space "
437                                                        "paragraph layout!");
438                                 }
439                         }
440                 } else {
441                         auto_ptr<InsetOld> inset;
442                         if (token == "\\SpecialChar" )
443                                 inset.reset(new InsetSpecialChar);
444                         else
445                                 inset.reset(new InsetSpace);
446                         inset->read(buf, lex);
447                         par.insertInset(par.size(), inset.release(),
448                                         font, change);
449                 }
450         } else if (token == "\\i") {
451                 auto_ptr<InsetOld> inset(new InsetLatexAccent);
452                 inset->read(buf, lex);
453                 par.insertInset(par.size(), inset.release(), font, change);
454         } else if (token == "\\backslash") {
455                 par.insertChar(par.size(), '\\', font, change);
456         } else if (token == "\\newline") {
457                 auto_ptr<InsetOld> inset(new InsetNewline);
458                 inset->read(buf, lex);
459                 par.insertInset(par.size(), inset.release(), font, change);
460         } else if (token == "\\LyXTable") {
461                 auto_ptr<InsetOld> inset(new InsetTabular(buf));
462                 inset->read(buf, lex);
463                 par.insertInset(par.size(), inset.release(), font, change);
464         } else if (token == "\\bibitem") {
465                 InsetCommandParams p("bibitem", "dummy");
466                 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
467                 inset->read(buf, lex);
468                 par.insertInset(par.size(), inset.release(), font, change);
469         } else if (token == "\\hfill") {
470                 par.insertInset(par.size(), new InsetHFill, font, change);
471         } else if (token == "\\lyxline") {
472                 par.insertInset(par.size(), new InsetLine, font, change);
473         } else if (token == "\\newpage") {
474                 par.insertInset(par.size(), new InsetPagebreak, font, change);
475         } else if (token == "\\change_unchanged") {
476                 // Hack ! Needed for empty paragraphs :/
477                 // FIXME: is it still ??
478                 if (!par.size())
479                         par.cleanChanges();
480                 change = Change(Change::UNCHANGED);
481         } else if (token == "\\change_inserted") {
482                 lex.nextToken();
483                 istringstream is(lex.getString());
484                 int aid;
485                 lyx::time_type ct;
486                 is >> aid >> ct;
487                 change = Change(Change::INSERTED, bp.author_map[aid], ct);
488         } else if (token == "\\change_deleted") {
489                 lex.nextToken();
490                 istringstream is(lex.getString());
491                 int aid;
492                 lyx::time_type ct;
493                 is >> aid >> ct;
494                 change = Change(Change::DELETED, bp.author_map[aid], ct);
495         } else {
496                 lex.eatLine();
497                 string const s = bformat(_("Unknown token: %1$s %2$s\n"),
498                         token, lex.getString());
499
500                 buf.error(ErrorItem(_("Unknown token"), s,
501                                     buf.paragraphs().back().id(),
502                                     0, par.size()));
503                 return 1;
504         }
505         return 0;
506 }
507
508 }
509
510
511 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
512 {
513         int unknown = 0;
514
515         lex.nextToken();
516         string token = lex.getString();
517
518         while (lex.isOK()) {
519
520                 unknown += readParToken(buf, par, lex, token);
521
522                 lex.nextToken();
523                 token = lex.getString();
524
525                 if (token.empty())
526                         continue;
527
528                 if (token == "\\end_layout") {
529                         //Ok, paragraph finished
530                         break;
531                 }
532
533                 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
534                                       << token << '\'' << endl;
535                 if (token == "\\begin_layout" || token == "\\end_document"
536                     || token == "\\end_inset" || token == "\\begin_deeper"
537                     || token == "\\end_deeper") {
538                         lex.pushToken(token);
539                         lyxerr << "Paragraph ended in line "
540                                << lex.getLineNo() << "\n"
541                                << "Missing \\end_layout.\n";
542                         break;
543                 }
544         }
545
546         return unknown;
547 }
548
549
550 LyXFont const outerFont(ParagraphList::iterator pit,
551                         ParagraphList const & plist)
552 {
553         Paragraph::depth_type par_depth = pit->getDepth();
554         LyXFont tmpfont(LyXFont::ALL_INHERIT);
555
556         // Resolve against environment font information
557         while (pit != const_cast<ParagraphList&>(plist).end() &&
558                par_depth && !tmpfont.resolved()) {
559                 pit = outerHook(pit, plist);
560                 if (pit != const_cast<ParagraphList&>(plist).end()) {
561                         tmpfont.realize(pit->layout()->font);
562                         par_depth = pit->getDepth();
563                 }
564         }
565
566         return tmpfont;
567 }
568
569
570 ParagraphList::iterator outerPar(Buffer const & buf, InsetOld const * inset)
571 {
572         ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
573         ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
574         for ( ; pit != end; ++pit) {
575
576                 ParagraphList * plist;
577                 // the second '=' below is intentional
578                 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
579                         if (plist == &pit.plist())
580                                 return pit.outerPar();
581
582                 InsetList::iterator ii = pit->insetlist.begin();
583                 InsetList::iterator iend = pit->insetlist.end();
584                 for ( ; ii != iend; ++ii)
585                         if (ii->inset == inset)
586                                 return pit.outerPar();
587         }
588         lyxerr << "outerPar: should not happen" << endl;
589         BOOST_ASSERT(false);
590         return const_cast<Buffer &>(buf).paragraphs().end(); // shut up compiler
591 }
592
593
594 Paragraph const & ownerPar(Buffer const & buf, InsetOld const * inset)
595 {
596         ParConstIterator pit = buf.par_iterator_begin();
597         ParConstIterator end = buf.par_iterator_end();
598         for ( ; pit != end; ++pit) {
599                 ParagraphList * plist;
600                 // the second '=' below is intentional
601                 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
602                         if (plist == &pit.plist())
603                                 return *pit.pit();
604
605                 InsetList::const_iterator ii = pit->insetlist.begin();
606                 InsetList::const_iterator iend = pit->insetlist.end();
607                 for ( ; ii != iend; ++ii)
608                         if (ii->inset == inset)
609                                 return *pit.pit();
610         }
611         lyxerr << "ownerPar: should not happen" << endl;
612         BOOST_ASSERT(false);
613         return buf.paragraphs().front(); // shut up compiler
614 }