+
+int numberOfLabelHfills(Paragraph const & par, Row const & row)
+{
+ pos_type last = row.endpos() - 1;
+ pos_type first = row.pos();
+
+ // hfill *DO* count at the beginning of paragraphs!
+ if (first) {
+ while (first < last && par.isHfill(first))
+ ++first;
+ }
+
+ last = min(last, par.beginOfBody());
+ int n = 0;
+ for (pos_type p = first; p < last; ++p) {
+ if (par.isHfill(p))
+ ++n;
+ }
+ return n;
+}
+
+
+int numberOfHfills(Paragraph const & par, Row const & row)
+{
+ pos_type const last = row.endpos() - 1;
+ pos_type first = row.pos();
+
+ // hfill *DO* count at the beginning of paragraphs!
+ if (first) {
+ while (first < last && par.isHfill(first))
+ ++first;
+ }
+
+ first = max(first, par.beginOfBody());
+
+ int n = 0;
+ for (pos_type p = first; p < last; ++p) {
+ if (par.isHfill(p))
+ ++n;
+ }
+ return n;
+}
+
+
+void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
+ string const & token, LyXFont & font)
+{
+ static Change change;
+
+ BufferParams const & bp = buf.params();
+
+ if (token[0] != '\\') {
+ string::const_iterator cit = token.begin();
+ for (; cit != token.end(); ++cit) {
+ par.insertChar(par.size(), (*cit), font, change);
+ }
+ } else if (token == "\\begin_layout") {
+ lex.eatLine();
+ string layoutname = lex.getString();
+
+ font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
+ change = Change();
+
+ LyXTextClass const & tclass = bp.getLyXTextClass();
+
+ if (layoutname.empty()) {
+ layoutname = tclass.defaultLayoutName();
+ }
+
+ bool hasLayout = tclass.hasLayout(layoutname);
+
+ if (!hasLayout) {
+ buf.error(ErrorItem(_("Unknown layout"),
+ bformat(_("Layout '%1$s' does not exists in textclass '%2$s'\nTrying to use the default instead.\n"),
+ layoutname, tclass.name()), par.id(), 0, par.size()));
+ layoutname = tclass.defaultLayoutName();
+ }
+
+ par.layout(bp.getLyXTextClass()[layoutname]);
+
+ // Test whether the layout is obsolete.
+ LyXLayout_ptr const & layout = par.layout();
+ if (!layout->obsoleted_by().empty())
+ par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
+
+ par.params().read(lex);
+
+ } else if (token == "\\end_layout") {
+ lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
+ << "Missing \\begin_layout?.\n";
+ } else if (token == "\\end_inset") {
+ lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
+ << "Missing \\begin_inset?.\n";
+ } else if (token == "\\begin_inset") {
+ InsetBase * inset = readInset(lex, buf);
+ if (inset)
+ par.insertInset(par.size(), inset, font, change);
+ else {
+ lex.eatLine();
+ string line = lex.getString();
+ buf.error(ErrorItem(_("Unknown Inset"), line,
+ par.id(), 0, par.size()));
+ }
+ } else if (token == "\\family") {
+ lex.next();
+ font.setLyXFamily(lex.getString());
+ } else if (token == "\\series") {
+ lex.next();
+ font.setLyXSeries(lex.getString());
+ } else if (token == "\\shape") {
+ lex.next();
+ font.setLyXShape(lex.getString());
+ } else if (token == "\\size") {
+ lex.next();
+ font.setLyXSize(lex.getString());
+ } else if (token == "\\lang") {
+ lex.next();
+ string const tok = lex.getString();
+ Language const * lang = languages.getLanguage(tok);
+ if (lang) {
+ font.setLanguage(lang);
+ } else {
+ font.setLanguage(bp.language);
+ lex.printError("Unknown language `$$Token'");
+ }
+ } else if (token == "\\numeric") {
+ lex.next();
+ font.setNumber(font.setLyXMisc(lex.getString()));
+ } else if (token == "\\emph") {
+ lex.next();
+ font.setEmph(font.setLyXMisc(lex.getString()));
+ } else if (token == "\\bar") {
+ lex.next();
+ string const tok = lex.getString();
+
+ if (tok == "under")
+ font.setUnderbar(LyXFont::ON);
+ else if (tok == "no")
+ font.setUnderbar(LyXFont::OFF);
+ else if (tok == "default")
+ font.setUnderbar(LyXFont::INHERIT);
+ else
+ lex.printError("Unknown bar font flag "
+ "`$$Token'");
+ } else if (token == "\\noun") {
+ lex.next();
+ font.setNoun(font.setLyXMisc(lex.getString()));
+ } else if (token == "\\color") {
+ lex.next();
+ font.setLyXColor(lex.getString());
+ } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
+
+ // Insets don't make sense in a free-spacing context! ---Kayvan
+ if (par.isFreeSpacing()) {
+ if (token == "\\InsetSpace")
+ par.insertChar(par.size(), ' ', font, change);
+ else if (lex.isOK()) {
+ lex.next();
+ string const next_token = lex.getString();
+ if (next_token == "\\-")
+ par.insertChar(par.size(), '-', font, change);
+ else {
+ lex.printError("Token `$$Token' "
+ "is in free space "
+ "paragraph layout!");
+ }
+ }
+ } else {
+ auto_ptr<InsetBase> inset;
+ if (token == "\\SpecialChar" )
+ inset.reset(new InsetSpecialChar);
+ else
+ inset.reset(new InsetSpace);
+ inset->read(buf, lex);
+ par.insertInset(par.size(), inset.release(),
+ font, change);
+ }
+ } else if (token == "\\i") {
+ auto_ptr<InsetBase> inset(new InsetLatexAccent);
+ inset->read(buf, lex);
+ par.insertInset(par.size(), inset.release(), font, change);
+ } else if (token == "\\backslash") {
+ par.insertChar(par.size(), '\\', font, change);
+ } else if (token == "\\newline") {
+ auto_ptr<InsetBase> inset(new InsetNewline);
+ inset->read(buf, lex);
+ par.insertInset(par.size(), inset.release(), font, change);
+ } else if (token == "\\LyXTable") {
+ auto_ptr<InsetBase> inset(new InsetTabular(buf));
+ inset->read(buf, lex);
+ par.insertInset(par.size(), inset.release(), font, change);
+ } else if (token == "\\bibitem") {
+ InsetCommandParams p("bibitem", "dummy");
+ auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
+ inset->read(buf, lex);
+ par.insertInset(par.size(), inset.release(), font, change);
+ } else if (token == "\\hfill") {
+ par.insertInset(par.size(), new InsetHFill, font, change);
+ } else if (token == "\\lyxline") {
+ par.insertInset(par.size(), new InsetLine, font, change);
+ } else if (token == "\\newpage") {
+ par.insertInset(par.size(), new InsetPagebreak, font, change);
+ } else if (token == "\\change_unchanged") {
+ // Hack ! Needed for empty paragraphs :/
+ // FIXME: is it still ??
+ if (!par.size())
+ par.cleanChanges();
+ change = Change(Change::UNCHANGED);
+ } else if (token == "\\change_inserted") {
+ lex.eatLine();
+ std::istringstream is(lex.getString());
+ int aid;
+ lyx::time_type ct;
+ is >> aid >> ct;
+ change = Change(Change::INSERTED, bp.author_map[aid], ct);
+ } else if (token == "\\change_deleted") {
+ lex.eatLine();
+ std::istringstream is(lex.getString());
+ int aid;
+ lyx::time_type ct;
+ is >> aid >> ct;
+ change = Change(Change::DELETED, bp.author_map[aid], ct);
+ } else {
+ lex.eatLine();
+ buf.error(ErrorItem(_("Unknown token"),
+ bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
+ par.id(), 0, par.size()));
+ }
+}
+
+
+void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
+{
+ lex.nextToken();
+ string token = lex.getString();
+ LyXFont font;
+
+ while (lex.isOK()) {
+
+ readParToken(buf, par, lex, token, font);
+
+ lex.nextToken();
+ token = lex.getString();
+
+ if (token.empty())
+ continue;
+
+ if (token == "\\end_layout") {
+ //Ok, paragraph finished
+ break;
+ }
+
+ lyxerr[Debug::PARSER] << "Handling paragraph token: `"
+ << token << '\'' << endl;
+ if (token == "\\begin_layout" || token == "\\end_document"
+ || token == "\\end_inset" || token == "\\begin_deeper"
+ || token == "\\end_deeper") {
+ lex.pushToken(token);
+ lyxerr << "Paragraph ended in line "
+ << lex.getLineNo() << "\n"
+ << "Missing \\end_layout.\n";
+ break;
+ }
+ }
+}
+
+