3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
16 #include "buffer_funcs.h"
17 #include "bufferlist.h"
18 #include "bufferparams.h"
22 #include "errorlist.h"
25 #include "funcrequest.h"
27 #include "iterators.h"
30 #include "LaTeXFeatures.h"
31 #include "LyXAction.h"
36 #include "paragraph.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
44 #include "insets/insetbibitem.h"
45 #include "insets/insetbibtex.h"
46 #include "insets/insetinclude.h"
47 #include "insets/insettext.h"
49 #include "frontends/Alert.h"
51 #include "graphics/Previews.h"
53 #include "support/FileInfo.h"
54 #include "support/filetools.h"
55 #include "support/gzstream.h"
56 #include "support/lyxlib.h"
57 #include "support/os.h"
58 #include "support/path.h"
59 #include "support/textutils.h"
60 #include "support/tostr.h"
62 #include <boost/bind.hpp>
63 #include <boost/tuple/tuple.hpp>
65 #include "support/std_sstream.h"
77 using lyx::support::AddName;
78 using lyx::support::ascii_lowercase;
79 using lyx::support::atoi;
80 using lyx::support::bformat;
81 using lyx::support::ChangeExtension;
82 using lyx::support::cmd_ret;
83 using lyx::support::compare_ascii_no_case;
84 using lyx::support::compare_no_case;
85 using lyx::support::contains;
86 using lyx::support::CreateBufferTmpDir;
87 using lyx::support::destroyDir;
88 using lyx::support::FileInfo;
89 using lyx::support::FileInfo;
90 using lyx::support::getExtFromContents;
91 using lyx::support::IsDirWriteable;
92 using lyx::support::IsFileWriteable;
93 using lyx::support::LibFileSearch;
94 using lyx::support::ltrim;
95 using lyx::support::MakeAbsPath;
96 using lyx::support::MakeDisplayPath;
97 using lyx::support::MakeLatexName;
98 using lyx::support::OnlyFilename;
99 using lyx::support::OnlyPath;
100 using lyx::support::Path;
101 using lyx::support::QuoteName;
102 using lyx::support::removeAutosaveFile;
103 using lyx::support::rename;
104 using lyx::support::RunCommand;
105 using lyx::support::split;
106 using lyx::support::strToInt;
107 using lyx::support::subst;
108 using lyx::support::tempName;
109 using lyx::support::trim;
111 namespace os = lyx::support::os;
115 using std::make_pair;
121 using std::ostringstream;
129 // all these externs should eventually be removed.
130 extern BufferList bufferlist;
134 const int LYX_FORMAT = 225;
136 bool openFileWrite(ofstream & ofs, string const & fname)
138 ofs.open(fname.c_str());
140 string const file = MakeDisplayPath(fname, 50);
141 string text = bformat(_("Could not open the specified "
142 "document\n%1$s."), file);
143 Alert::error(_("Could not open file"), text);
152 typedef std::map<string, bool> DepClean;
156 Impl(Buffer & parent, string const & file, bool readonly);
158 limited_stack<Undo> undostack;
159 limited_stack<Undo> redostack;
161 ParagraphList paragraphs;
167 /// need to regenerate .tex ?
171 mutable bool lyx_clean;
173 /// is autosave needed
174 mutable bool bak_clean;
176 /// is this a unnamed file (New...)
182 /// name of the file the buffer is associated with.
185 /// The path to the document file.
188 boost::scoped_ptr<Messages> messages;
192 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
194 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
195 filename(file), filepath(OnlyPath(file))
197 lyxvc.buffer(&parent);
198 if (readonly_ || lyxrc.use_tempdir)
199 temppath = CreateBufferTmpDir();
203 Buffer::Buffer(string const & file, bool ronly)
204 : pimpl_(new Impl(*this, file, ronly))
206 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
212 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
213 // here the buffer should take care that it is
214 // saved properly, before it goes into the void.
218 if (!temppath().empty() && destroyDir(temppath()) != 0) {
219 Alert::warning(_("Could not remove temporary directory"),
220 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
223 paragraphs().clear();
225 // Remove any previewed LaTeX snippets associated with this buffer.
226 lyx::graphics::Previews::get().removeLoader(*this);
230 limited_stack<Undo> & Buffer::undostack()
232 return pimpl_->undostack;
236 limited_stack<Undo> const & Buffer::undostack() const
238 return pimpl_->undostack;
242 limited_stack<Undo> & Buffer::redostack()
244 return pimpl_->redostack;
248 limited_stack<Undo> const & Buffer::redostack() const
250 return pimpl_->redostack;
254 BufferParams & Buffer::params()
256 return pimpl_->params;
260 BufferParams const & Buffer::params() const
262 return pimpl_->params;
266 ParagraphList & Buffer::paragraphs()
268 return pimpl_->paragraphs;
272 ParagraphList const & Buffer::paragraphs() const
274 return pimpl_->paragraphs;
278 LyXVC & Buffer::lyxvc()
280 return pimpl_->lyxvc;
284 LyXVC const & Buffer::lyxvc() const
286 return pimpl_->lyxvc;
290 string const & Buffer::temppath() const
292 return pimpl_->temppath;
296 bool & Buffer::niceFile()
298 return pimpl_->nicefile;
302 bool Buffer::niceFile() const
304 return pimpl_->nicefile;
308 TexRow & Buffer::texrow()
310 return pimpl_->texrow;
314 TexRow const & Buffer::texrow() const
316 return pimpl_->texrow;
320 string const Buffer::getLatexName(bool no_path) const
322 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
323 return no_path ? OnlyFilename(name) : name;
327 pair<Buffer::LogType, string> const Buffer::getLogName() const
329 string const filename = getLatexName(false);
331 if (filename.empty())
332 return make_pair(Buffer::latexlog, string());
334 string path = OnlyPath(filename);
336 if (lyxrc.use_tempdir || !IsDirWriteable(path))
339 string const fname = AddName(path,
340 OnlyFilename(ChangeExtension(filename,
343 AddName(path, OnlyFilename(
344 ChangeExtension(filename,
345 formats.extension("literate") + ".out")));
347 // If no Latex log or Build log is newer, show Build log
349 FileInfo const f_fi(fname);
350 FileInfo const b_fi(bname);
353 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
354 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
355 return make_pair(Buffer::buildlog, bname);
357 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
358 return make_pair(Buffer::latexlog, fname);
362 void Buffer::setReadonly(bool flag)
364 if (pimpl_->read_only != flag) {
365 pimpl_->read_only = flag;
371 void Buffer::setFileName(string const & newfile)
373 pimpl_->filename = MakeAbsPath(newfile);
374 pimpl_->filepath = OnlyPath(pimpl_->filename);
375 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
380 // We'll remove this later. (Lgb)
383 void unknownClass(string const & unknown)
385 Alert::warning(_("Unknown document class"),
386 bformat(_("Using the default document class, because the "
387 "class %1$s is unknown."), unknown));
392 int Buffer::readHeader(LyXLex & lex)
394 int unknown_tokens = 0;
398 string const token = lex.getString();
403 if (token == "\\end_header")
406 lyxerr[Debug::PARSER] << "Handling header token: `"
407 << token << '\'' << endl;
410 string unknown = params().readToken(lex, token);
411 if (!unknown.empty()) {
412 if (unknown[0] != '\\') {
413 unknownClass(unknown);
416 string const s = bformat(_("Unknown token: "
420 error(ErrorItem(_("Header error"), s,
425 return unknown_tokens;
429 // candidate for move to BufferView
430 // (at least some parts in the beginning of the func)
433 // changed to be public and have one parameter
434 // if par = 0 normal behavior
435 // else insert behavior
436 // Returns false if "\end_document" is not read (Asger)
437 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
439 Paragraph::depth_type depth = 0;
440 bool the_end_read = false;
442 if (paragraphs().empty()) {
444 if (!params().getLyXTextClass().load()) {
445 string theclass = params().getLyXTextClass().name();
446 Alert::error(_("Can't load document class"), bformat(
447 "Using the default document class, because the "
448 " class %1$s could not be loaded.", theclass));
449 params().textclass = 0;
452 // We don't want to adopt the parameters from the
453 // document we insert, so read them into a temporary buffer
454 // and then discard it
456 Buffer tmpbuf("", false);
457 tmpbuf.readHeader(lex);
462 string const token = lex.getString();
467 lyxerr[Debug::PARSER] << "Handling token: `"
468 << token << '\'' << endl;
470 if (token == "\\end_document") {
475 readParagraph(lex, token, paragraphs(), pit, depth);
482 int Buffer::readParagraph(LyXLex & lex, string const & token,
483 ParagraphList & pars, ParagraphList::iterator & pit,
484 Paragraph::depth_type & depth)
486 static Change current_change;
489 if (token == "\\begin_layout") {
490 lex.pushToken(token);
493 par.params().depth(depth);
494 if (params().tracking_changes)
496 LyXFont f(LyXFont::ALL_INHERIT, params().language);
500 if (pit != pars.end())
503 pit = pars.insert(pit, par);
505 // FIXME: goddamn InsetTabular makes us pass a Buffer
507 ::readParagraph(*this, *pit, lex);
509 } else if (token == "\\begin_deeper") {
511 } else if (token == "\\end_deeper") {
513 lex.printError("\\end_deeper: " "depth is already null");
524 // needed to insert the selection
525 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
526 LyXFont const & fn,string const & str)
528 LyXLayout_ptr const & layout = par->layout();
532 par->checkInsertChar(font);
533 // insert the string, don't insert doublespace
534 bool space_inserted = true;
535 bool autobreakrows = !par->inInset() ||
536 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
537 for(string::const_iterator cit = str.begin();
538 cit != str.end(); ++cit) {
540 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
541 breakParagraph(params(), paragraphs(), par, pos,
542 layout->isEnvironment());
545 space_inserted = true;
549 // do not insert consecutive spaces if !free_spacing
550 } else if ((*cit == ' ' || *cit == '\t') &&
551 space_inserted && !par->isFreeSpacing()) {
553 } else if (*cit == '\t') {
554 if (!par->isFreeSpacing()) {
555 // tabs are like spaces here
556 par->insertChar(pos, ' ', font);
558 space_inserted = true;
560 const pos_type nb = 8 - pos % 8;
561 for (pos_type a = 0; a < nb ; ++a) {
562 par->insertChar(pos, ' ', font);
565 space_inserted = true;
567 } else if (!IsPrintable(*cit)) {
568 // Ignore unprintables
571 // just insert the character
572 par->insertChar(pos, *cit, font);
574 space_inserted = (*cit == ' ');
581 bool Buffer::readFile(string const & filename)
583 // Check if the file is compressed.
584 string const format = getExtFromContents(filename);
585 if (format == "gzip" || format == "zip" || format == "compress") {
586 params().compressed = true;
589 bool ret = readFile(filename, paragraphs().begin());
591 // After we have read a file, we must ensure that the buffer
592 // language is set and used in the gui.
593 // If you know of a better place to put this, please tell me. (Lgb)
594 updateDocLang(params().language);
600 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
603 lex.setFile(filename);
605 return readFile(lex, filename, pit);
609 bool Buffer::readFile(LyXLex & lex, string const & filename,
610 ParagraphList::iterator pit)
612 BOOST_ASSERT(!filename.empty());
615 Alert::error(_("Document could not be read"),
616 bformat(_("%1$s could not be read."), filename));
621 string const token(lex.getString());
624 Alert::error(_("Document could not be read"),
625 bformat(_("%1$s could not be read."), filename));
629 // the first token _must_ be...
630 if (token != "\\lyxformat") {
631 lyxerr << "Token: " << token << endl;
633 Alert::error(_("Document format failure"),
634 bformat(_("%1$s is not a LyX document."),
640 string tmp_format = lex.getString();
641 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
642 // if present remove ".," from string.
643 string::size_type dot = tmp_format.find_first_of(".,");
644 //lyxerr << " dot found at " << dot << endl;
645 if (dot != string::npos)
646 tmp_format.erase(dot, 1);
647 int file_format = strToInt(tmp_format);
648 //lyxerr << "format: " << file_format << endl;
650 if (file_format > LYX_FORMAT) {
651 Alert::warning(_("Document format failure"),
652 bformat(_("%1$s was created with a newer"
653 " version of LyX. This is likely to"
656 } else if (file_format < LYX_FORMAT) {
657 string const tmpfile = tempName();
658 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
659 if (command.empty()) {
660 Alert::error(_("Conversion script not found"),
661 bformat(_("%1$s is from an earlier"
662 " version of LyX, but the"
663 " conversion script lyx2lyx"
664 " could not be found."),
670 + " -o " + tmpfile + ' '
671 + QuoteName(filename);
672 lyxerr[Debug::INFO] << "Running '"
675 cmd_ret const ret = RunCommand(command);
676 if (ret.first != 0) {
677 Alert::error(_("Conversion script failed"),
678 bformat(_("%1$s is from an earlier version"
679 " of LyX, but the lyx2lyx script"
680 " failed to convert it."),
684 bool ret = readFile(tmpfile, pit);
685 // Do stuff with tmpfile name and buffer name here.
691 bool the_end = readBody(lex, pit);
692 params().setPaperStuff();
695 Alert::error(_("Document format failure"),
696 bformat(_("%1$s ended unexpectedly, which means"
697 " that it is probably corrupted."),
704 // Should probably be moved to somewhere else: BufferView? LyXView?
705 bool Buffer::save() const
707 // We don't need autosaves in the immediate future. (Asger)
708 resetAutosaveTimers();
712 if (lyxrc.make_backup) {
713 s = fileName() + '~';
714 if (!lyxrc.backupdir_path.empty())
715 s = AddName(lyxrc.backupdir_path,
716 subst(os::slashify_path(s),'/','!'));
718 // Rename is the wrong way of making a backup,
719 // this is the correct way.
720 /* truss cp fil fil2:
721 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
722 stat("LyXVC.lyx", 0xEFFFF688) = 0
723 open("LyXVC.lyx", O_RDONLY) = 3
724 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
725 fstat(4, 0xEFFFF508) = 0
726 fstat(3, 0xEFFFF508) = 0
727 read(3, " # T h i s f i l e w".., 8192) = 5579
728 write(4, " # T h i s f i l e w".., 5579) = 5579
729 read(3, 0xEFFFD4A0, 8192) = 0
732 chmod("LyXVC3.lyx", 0100644) = 0
733 lseek(0, 0, SEEK_CUR) = 46440
737 // Should probably have some more error checking here.
738 // Doing it this way, also makes the inodes stay the same.
739 // This is still not a very good solution, in particular we
740 // might loose the owner of the backup.
741 FileInfo finfo(fileName());
743 mode_t fmode = finfo.getMode();
744 struct utimbuf times = {
745 finfo.getAccessTime(),
746 finfo.getModificationTime() };
748 ifstream ifs(fileName().c_str());
749 ofstream ofs(s.c_str(), ios::out|ios::trunc);
754 ::chmod(s.c_str(), fmode);
756 if (::utime(s.c_str(), ×)) {
757 lyxerr << "utime error." << endl;
760 lyxerr << "LyX was not able to make "
761 "backup copy. Beware." << endl;
766 if (writeFile(fileName())) {
768 removeAutosaveFile(fileName());
770 // Saving failed, so backup is not backup
771 if (lyxrc.make_backup) {
772 rename(s, fileName());
780 bool Buffer::writeFile(string const & fname) const
782 if (pimpl_->read_only && (fname == fileName())) {
786 FileInfo finfo(fname);
787 if (finfo.exist() && !finfo.writable()) {
793 if (params().compressed) {
794 gz::ogzstream ofs(fname.c_str());
799 retval = do_writeFile(ofs);
802 ofstream ofs(fname.c_str());
806 retval = do_writeFile(ofs);
813 bool Buffer::do_writeFile(ostream & ofs) const
817 // Use the standard "C" locale for file output.
818 ofs.imbue(std::locale::classic());
821 // The top of the file should not be written by params().
823 // write out a comment in the top of the file
824 ofs << "#LyX " << lyx_version
825 << " created this file. For more info see http://www.lyx.org/\n"
826 << "\\lyxformat " << LYX_FORMAT << "\n";
828 // now write out the buffer paramters.
829 params().writeFile(ofs);
831 ofs << "\\end_header\n";
833 Paragraph::depth_type depth = 0;
835 // this will write out all the paragraphs
836 // using recursive descent.
837 ParagraphList::const_iterator pit = paragraphs().begin();
838 ParagraphList::const_iterator pend = paragraphs().end();
839 for (; pit != pend; ++pit)
840 pit->write(*this, ofs, params(), depth);
842 // Write marker that shows file is complete
843 ofs << "\n\\end_document" << endl;
845 // Shouldn't really be needed....
848 // how to check if close went ok?
849 // Following is an attempt... (BE 20001011)
851 // good() returns false if any error occured, including some
853 // bad() returns true if something bad happened in the buffer,
854 // which should include file system full errors.
861 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
863 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
875 pair<int, string> const addDepth(int depth, int ldepth)
879 d += (ldepth - depth) * 2;
880 return make_pair(d, string(d, ' '));
886 string const Buffer::asciiParagraph(Paragraph const & par,
887 unsigned int linelen,
888 bool noparbreak) const
890 ostringstream buffer;
892 Paragraph::depth_type ltype_depth = 0;
893 bool ref_printed = false;
894 Paragraph::depth_type depth = par.params().depth();
896 // First write the layout
897 string const & tmp = par.layout()->name();
898 if (compare_no_case(tmp, "itemize") == 0) {
900 ltype_depth = depth + 1;
901 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
903 ltype_depth = depth + 1;
904 } else if (contains(ascii_lowercase(tmp), "ection")) {
906 ltype_depth = depth + 1;
907 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
909 ltype_depth = depth + 1;
910 } else if (compare_ascii_no_case(tmp, "description") == 0) {
912 ltype_depth = depth + 1;
913 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
916 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
924 /* maybe some vertical spaces */
926 /* the labelwidthstring used in lists */
930 /* some pagebreaks? */
934 /* what about the alignment */
936 // linelen <= 0 is special and means we don't have paragraph breaks
938 string::size_type currlinelen = 0;
944 buffer << string(depth * 2, ' ');
945 currlinelen += depth * 2;
948 // we should probably change to the paragraph language in the
949 // gettext here (if possible) so that strings are outputted in
950 // the correct language! (20012712 Jug)
954 case 4: // (Sub)Paragraph
955 case 5: // Description
959 buffer << _("Abstract") << "\n\n";
962 string const abst = _("Abstract: ");
964 currlinelen += abst.length();
967 case 7: // Bibliography
970 buffer << _("References") << "\n\n";
973 string const refs = _("References: ");
975 currlinelen += refs.length();
983 string const parlab = par.params().labelString();
984 buffer << parlab << ' ';
985 currlinelen += parlab.length() + 1;
993 pair<int, string> p = addDepth(depth, ltype_depth);
995 currlinelen += p.first;
998 // this is to change the linebreak to do it by word a bit more
999 // intelligent hopefully! (only in the case where we have a
1000 // max linelength!) (Jug)
1004 for (pos_type i = 0; i < par.size(); ++i) {
1005 char c = par.getUChar(params(), i);
1007 case Paragraph::META_INSET:
1009 InsetOld const * inset = par.getInset(i);
1013 currlinelen += word.length();
1016 if (inset->ascii(*this, buffer, linelen)) {
1017 // to be sure it breaks paragraph
1018 currlinelen += linelen;
1027 currlinelen + word.length() > linelen - 10) {
1029 pair<int, string> p = addDepth(depth, ltype_depth);
1031 currlinelen = p.first;
1034 buffer << word << ' ';
1035 currlinelen += word.length() + 1;
1042 lyxerr[Debug::INFO] <<
1043 "writeAsciiFile: NULL char in structure." << endl;
1045 if ((linelen > 0) &&
1046 (currlinelen + word.length()) > linelen)
1050 pair<int, string> p =
1051 addDepth(depth, ltype_depth);
1053 currlinelen = p.first;
1060 return buffer.str();
1064 void Buffer::writeFileAscii(string const & fname, int linelen)
1067 if (!::openFileWrite(ofs, fname))
1069 writeFileAscii(ofs, linelen);
1073 void Buffer::writeFileAscii(ostream & os, int linelen)
1075 ParagraphList::iterator beg = paragraphs().begin();
1076 ParagraphList::iterator end = paragraphs().end();
1077 ParagraphList::iterator it = beg;
1078 for (; it != end; ++it) {
1079 os << asciiParagraph(*it, linelen, it == beg);
1085 void Buffer::makeLaTeXFile(string const & fname,
1086 string const & original_path,
1087 LatexRunParams const & runparams,
1088 bool output_preamble, bool output_body)
1090 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1093 if (!::openFileWrite(ofs, fname))
1096 makeLaTeXFile(ofs, original_path,
1097 runparams, output_preamble, output_body);
1101 lyxerr << "File was not closed properly." << endl;
1106 void Buffer::makeLaTeXFile(ostream & os,
1107 string const & original_path,
1108 LatexRunParams const & runparams_in,
1109 bool output_preamble, bool output_body)
1111 LatexRunParams runparams = runparams_in;
1112 niceFile() = runparams.nice; // this will be used by Insetincludes.
1114 // validate the buffer.
1115 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1116 LaTeXFeatures features(*this, params());
1118 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1121 // The starting paragraph of the coming rows is the
1122 // first paragraph of the document. (Asger)
1123 texrow().start(paragraphs().begin()->id(), 0);
1125 if (output_preamble && runparams.nice) {
1126 os << "%% LyX " << lyx_version << " created this file. "
1127 "For more info, see http://www.lyx.org/.\n"
1128 "%% Do not edit unless you really know what "
1133 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1134 // There are a few differences between nice LaTeX and usual files:
1135 // usual is \batchmode and has a
1136 // special input@path to allow the including of figures
1137 // with either \input or \includegraphics (what figinsets do).
1138 // input@path is set when the actual parameter
1139 // original_path is set. This is done for usual tex-file, but not
1140 // for nice-latex-file. (Matthias 250696)
1141 if (output_preamble) {
1142 if (!runparams.nice) {
1143 // code for usual, NOT nice-latex-file
1144 os << "\\batchmode\n"; // changed
1145 // from \nonstopmode
1148 if (!original_path.empty()) {
1149 string inputpath = os::external_path(original_path);
1150 subst(inputpath, "~", "\\string~");
1151 os << "\\makeatletter\n"
1152 << "\\def\\input@path{{"
1153 << inputpath << "/}}\n"
1154 << "\\makeatother\n";
1160 // Write the preamble
1161 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1167 os << "\\begin{document}\n";
1169 } // output_preamble
1170 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
1172 if (!lyxrc.language_auto_begin) {
1173 os << subst(lyxrc.language_command_begin, "$$lang",
1174 params().language->babel())
1179 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1181 // add this just in case after all the paragraphs
1185 if (!lyxrc.language_auto_end) {
1186 os << subst(lyxrc.language_command_end, "$$lang",
1187 params().language->babel())
1192 if (output_preamble) {
1193 os << "\\end{document}\n";
1196 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1198 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1202 // Just to be sure. (Asger)
1205 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1206 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1209 // we want this to be true outside previews (for insetexternal)
1214 bool Buffer::isLatex() const
1216 return params().getLyXTextClass().outputType() == LATEX;
1220 bool Buffer::isLinuxDoc() const
1222 return params().getLyXTextClass().outputType() == LINUXDOC;
1226 bool Buffer::isLiterate() const
1228 return params().getLyXTextClass().outputType() == LITERATE;
1232 bool Buffer::isDocBook() const
1234 return params().getLyXTextClass().outputType() == DOCBOOK;
1238 bool Buffer::isSGML() const
1240 LyXTextClass const & tclass = params().getLyXTextClass();
1242 return tclass.outputType() == LINUXDOC ||
1243 tclass.outputType() == DOCBOOK;
1247 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
1250 if (!::openFileWrite(ofs, fname))
1253 niceFile() = nice; // this will be used by included files.
1255 LaTeXFeatures features(*this, params());
1261 LyXTextClass const & tclass = params().getLyXTextClass();
1263 string top_element = tclass.latexname();
1266 ofs << "<!doctype linuxdoc system";
1268 string preamble = params().preamble;
1269 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1271 preamble += features.getIncludedFiles(name);
1272 preamble += features.getLyXSGMLEntities();
1274 if (!preamble.empty()) {
1275 ofs << " [ " << preamble << " ]";
1279 if (params().options.empty())
1280 sgml::openTag(ofs, 0, false, top_element);
1282 string top = top_element;
1284 top += params().options;
1285 sgml::openTag(ofs, 0, false, top);
1289 ofs << "<!-- LyX " << lyx_version
1290 << " created this file. For more info see http://www.lyx.org/"
1293 Paragraph::depth_type depth = 0; // paragraph depth
1295 vector<string> environment_stack(5);
1297 ParagraphList::iterator pit = paragraphs().begin();
1298 ParagraphList::iterator pend = paragraphs().end();
1299 for (; pit != pend; ++pit) {
1300 LyXLayout_ptr const & style = pit->layout();
1301 // treat <toc> as a special case for compatibility with old code
1302 if (pit->isInset(0)) {
1303 InsetOld * inset = pit->getInset(0);
1304 InsetOld::Code lyx_code = inset->lyxCode();
1305 if (lyx_code == InsetOld::TOC_CODE) {
1306 string const temp = "toc";
1307 sgml::openTag(ofs, depth, false, temp);
1312 // environment tag closing
1313 for (; depth > pit->params().depth(); --depth) {
1314 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1315 environment_stack[depth].erase();
1318 // write opening SGML tags
1319 switch (style->latextype) {
1320 case LATEX_PARAGRAPH:
1321 if (depth == pit->params().depth()
1322 && !environment_stack[depth].empty()) {
1323 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1324 environment_stack[depth].erase();
1330 sgml::openTag(ofs, depth, false, style->latexname());
1335 error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1337 if (!environment_stack[depth].empty()) {
1338 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1342 environment_stack[depth].erase();
1343 sgml::openTag(ofs, depth, false, style->latexname());
1346 case LATEX_ENVIRONMENT:
1347 case LATEX_ITEM_ENVIRONMENT:
1348 case LATEX_BIB_ENVIRONMENT:
1350 string const & latexname = style->latexname();
1352 if (depth == pit->params().depth()
1353 && environment_stack[depth] != latexname) {
1354 sgml::closeTag(ofs, depth, false,
1355 environment_stack[depth]);
1356 environment_stack[depth].erase();
1358 if (depth < pit->params().depth()) {
1359 depth = pit->params().depth();
1360 environment_stack[depth].erase();
1362 if (environment_stack[depth] != latexname) {
1364 sgml::openTag(ofs, depth, false, "p");
1366 sgml::openTag(ofs, depth, false, latexname);
1368 if (environment_stack.size() == depth + 1)
1369 environment_stack.push_back("!-- --");
1370 environment_stack[depth] = latexname;
1373 if (style->latexparam() == "CDATA")
1376 if (style->latextype == LATEX_ENVIRONMENT) break;
1378 if (style->labeltype == LABEL_MANUAL)
1383 sgml::openTag(ofs, depth + 1, false, item_name);
1388 sgml::openTag(ofs, depth, false, style->latexname());
1392 simpleLinuxDocOnePar(ofs, pit, depth);
1395 // write closing SGML tags
1396 switch (style->latextype) {
1399 case LATEX_ENVIRONMENT:
1400 case LATEX_ITEM_ENVIRONMENT:
1401 case LATEX_BIB_ENVIRONMENT:
1402 if (style->latexparam() == "CDATA")
1406 sgml::closeTag(ofs, depth, false, style->latexname());
1412 for (int i = depth; i >= 0; --i)
1413 sgml::closeTag(ofs, depth, false, environment_stack[i]);
1417 sgml::closeTag(ofs, 0, false, top_element);
1421 // How to check for successful close
1423 // we want this to be true outside previews (for insetexternal)
1428 // checks, if newcol chars should be put into this line
1429 // writes newline, if necessary.
1432 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1433 string::size_type newcol)
1436 if (colcount > lyxrc.ascii_linelen) {
1438 colcount = newcol; // assume write after this call
1453 string tag_name(PAR_TAG const & pt) {
1455 case NONE: return "!-- --";
1456 case TT: return "tt";
1457 case SF: return "sf";
1458 case BF: return "bf";
1459 case IT: return "it";
1460 case SL: return "sl";
1461 case EM: return "em";
1468 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1470 p1 = static_cast<PAR_TAG>(p1 | p2);
1475 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1477 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1483 // Handle internal paragraph parsing -- layout already processed.
1484 void Buffer::simpleLinuxDocOnePar(ostream & os,
1485 ParagraphList::iterator par,
1486 Paragraph::depth_type /*depth*/) const
1488 LyXLayout_ptr const & style = par->layout();
1490 string::size_type char_line_count = 5; // Heuristic choice ;-)
1492 // gets paragraph main font
1495 if (style->labeltype == LABEL_MANUAL) {
1496 font_old = style->labelfont;
1499 font_old = style->font;
1503 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1504 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1505 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1508 stack<PAR_TAG> tag_state;
1509 // parsing main loop
1510 for (pos_type i = 0; i < par->size(); ++i) {
1512 PAR_TAG tag_close = NONE;
1513 list < PAR_TAG > tag_open;
1515 LyXFont const font = par->getFont(params(), i, outerFont(par, paragraphs()));
1517 if (font_old.family() != font.family()) {
1518 switch (family_type) {
1519 case LyXFont::SANS_FAMILY:
1522 case LyXFont::TYPEWRITER_FAMILY:
1529 family_type = font.family();
1531 switch (family_type) {
1532 case LyXFont::SANS_FAMILY:
1533 tag_open.push_back(SF);
1535 case LyXFont::TYPEWRITER_FAMILY:
1536 tag_open.push_back(TT);
1543 if (font_old.series() != font.series()) {
1544 switch (series_type) {
1545 case LyXFont::BOLD_SERIES:
1552 series_type = font.series();
1554 switch (series_type) {
1555 case LyXFont::BOLD_SERIES:
1556 tag_open.push_back(BF);
1564 if (font_old.shape() != font.shape()) {
1565 switch (shape_type) {
1566 case LyXFont::ITALIC_SHAPE:
1569 case LyXFont::SLANTED_SHAPE:
1576 shape_type = font.shape();
1578 switch (shape_type) {
1579 case LyXFont::ITALIC_SHAPE:
1580 tag_open.push_back(IT);
1582 case LyXFont::SLANTED_SHAPE:
1583 tag_open.push_back(SL);
1590 if (font_old.emph() != font.emph()) {
1591 if (font.emph() == LyXFont::ON) {
1592 tag_open.push_back(EM);
1601 list < PAR_TAG > temp;
1602 while (!tag_state.empty() && tag_close) {
1603 PAR_TAG k = tag_state.top();
1605 os << "</" << tag_name(k) << '>';
1612 for(list< PAR_TAG >::const_iterator j = temp.begin();
1613 j != temp.end(); ++j) {
1615 os << '<' << tag_name(*j) << '>';
1618 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1619 j != tag_open.end(); ++j) {
1621 os << '<' << tag_name(*j) << '>';
1624 char c = par->getChar(i);
1626 if (c == Paragraph::META_INSET) {
1627 InsetOld * inset = par->getInset(i);
1628 inset->linuxdoc(*this, os);
1633 if (style->latexparam() == "CDATA") {
1634 // "TeX"-Mode on == > SGML-Mode on.
1641 boost::tie(ws, str) = sgml::escapeChar(c);
1642 if (ws && !par->isFreeSpacing()) {
1643 // in freespacing mode, spaces are
1644 // non-breaking characters
1645 if (desc_on) {// if char is ' ' then...
1648 sgmlLineBreak(os, char_line_count, 6);
1652 sgmlLineBreak(os, char_line_count, 1);
1657 char_line_count += str.length();
1663 while (!tag_state.empty()) {
1664 os << "</" << tag_name(tag_state.top()) << '>';
1668 // resets description flag correctly
1670 // <tag> not closed...
1671 sgmlLineBreak(os, char_line_count, 6);
1677 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
1680 if (!::openFileWrite(ofs, fname))
1683 niceFile() = nice; // this will be used by Insetincludes.
1685 LaTeXFeatures features(*this, params());
1690 LyXTextClass const & tclass = params().getLyXTextClass();
1691 string top_element = tclass.latexname();
1694 ofs << "<!DOCTYPE " << top_element
1695 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
1697 string preamble = params().preamble;
1698 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1700 preamble += features.getIncludedFiles(name);
1701 preamble += features.getLyXSGMLEntities();
1703 if (!preamble.empty()) {
1704 ofs << "\n [ " << preamble << " ]";
1709 string top = top_element;
1711 top += params().language->code();
1714 if (!params().options.empty()) {
1716 top += params().options;
1718 sgml::openTag(ofs, 0, false, top);
1720 ofs << "<!-- DocBook file was created by LyX " << lyx_version
1721 << "\n See http://www.lyx.org/ for more information -->\n";
1723 vector<string> environment_stack(10);
1724 vector<string> environment_inner(10);
1725 vector<string> command_stack(10);
1727 bool command_flag = false;
1728 Paragraph::depth_type command_depth = 0;
1729 Paragraph::depth_type command_base = 0;
1730 Paragraph::depth_type cmd_depth = 0;
1731 Paragraph::depth_type depth = 0; // paragraph depth
1734 string command_name;
1736 ParagraphList::iterator par = paragraphs().begin();
1737 ParagraphList::iterator pend = paragraphs().end();
1739 for (; par != pend; ++par) {
1743 int desc_on = 0; // description mode
1745 LyXLayout_ptr const & style = par->layout();
1747 // environment tag closing
1748 for (; depth > par->params().depth(); --depth) {
1749 if (environment_inner[depth] != "!-- --" && !environment_inner[depth].empty()) {
1750 item_name = "listitem";
1751 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1752 if (environment_inner[depth] == "varlistentry")
1753 sgml::closeTag(ofs, depth+command_depth, false, environment_inner[depth]);
1755 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1756 environment_stack[depth].erase();
1757 environment_inner[depth].erase();
1760 if (depth == par->params().depth()
1761 && environment_stack[depth] != style->latexname()
1762 && !environment_stack[depth].empty()) {
1763 if (environment_inner[depth] != "!-- --") {
1764 item_name= "listitem";
1765 sgml::closeTag(ofs, command_depth+depth, false, item_name);
1766 if (environment_inner[depth] == "varlistentry")
1767 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1770 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1772 environment_stack[depth].erase();
1773 environment_inner[depth].erase();
1776 // Write opening SGML tags.
1777 switch (style->latextype) {
1778 case LATEX_PARAGRAPH:
1779 sgml::openTag(ofs, depth + command_depth,
1780 false, style->latexname());
1785 error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1787 command_name = style->latexname();
1789 sgmlparam = style->latexparam();
1790 c_params = split(sgmlparam, c_depth,'|');
1792 cmd_depth = atoi(c_depth);
1795 if (cmd_depth < command_base) {
1796 for (Paragraph::depth_type j = command_depth;
1797 j >= command_base; --j) {
1798 sgml::closeTag(ofs, j, false, command_stack[j]);
1801 command_depth = command_base = cmd_depth;
1802 } else if (cmd_depth <= command_depth) {
1803 for (int j = command_depth;
1804 j >= int(cmd_depth); --j) {
1805 sgml::closeTag(ofs, j, false, command_stack[j]);
1808 command_depth = cmd_depth;
1810 command_depth = cmd_depth;
1812 command_depth = command_base = cmd_depth;
1813 command_flag = true;
1815 if (command_stack.size() == command_depth + 1)
1816 command_stack.push_back(string());
1817 command_stack[command_depth] = command_name;
1819 // treat label as a special case for
1820 // more WYSIWYM handling.
1821 // This is a hack while paragraphs can't have
1822 // attributes, like id in this case.
1823 if (par->isInset(0)) {
1824 InsetOld * inset = par->getInset(0);
1825 InsetOld::Code lyx_code = inset->lyxCode();
1826 if (lyx_code == InsetOld::LABEL_CODE) {
1827 command_name += " id=\"";
1828 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1829 command_name += '"';
1834 sgml::openTag(ofs, depth + command_depth, false, command_name);
1836 item_name = c_params.empty() ? "title" : c_params;
1837 sgml::openTag(ofs, depth + 1 + command_depth, false, item_name);
1840 case LATEX_ENVIRONMENT:
1841 case LATEX_ITEM_ENVIRONMENT:
1842 if (depth < par->params().depth()) {
1843 depth = par->params().depth();
1844 environment_stack[depth].erase();
1847 if (environment_stack[depth] != style->latexname()) {
1848 if (environment_stack.size() == depth + 1) {
1849 environment_stack.push_back("!-- --");
1850 environment_inner.push_back("!-- --");
1852 environment_stack[depth] = style->latexname();
1853 environment_inner[depth] = "!-- --";
1854 sgml::openTag(ofs, depth + command_depth, false, environment_stack[depth]);
1856 if (environment_inner[depth] != "!-- --") {
1857 item_name= "listitem";
1858 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1859 if (environment_inner[depth] == "varlistentry")
1860 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1864 if (style->latextype == LATEX_ENVIRONMENT) {
1865 if (!style->latexparam().empty()) {
1866 if (style->latexparam() == "CDATA")
1869 sgml::openTag(ofs, depth + command_depth, false, style->latexparam());
1874 desc_on = (style->labeltype == LABEL_MANUAL);
1876 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1877 sgml::openTag(ofs, depth + 1 + command_depth,
1878 false, environment_inner[depth]);
1880 item_name = desc_on ? "term" : "para";
1881 sgml::openTag(ofs, depth + 1 + command_depth,
1885 sgml::openTag(ofs, depth + command_depth,
1886 false, style->latexname());
1890 simpleDocBookOnePar(ofs, par, desc_on,
1891 depth + 1 + command_depth);
1894 // write closing SGML tags
1895 switch (style->latextype) {
1897 end_tag = c_params.empty() ? "title" : c_params;
1898 sgml::closeTag(ofs, depth + command_depth,
1901 case LATEX_ENVIRONMENT:
1902 if (!style->latexparam().empty()) {
1903 if (style->latexparam() == "CDATA")
1906 sgml::closeTag(ofs, depth + command_depth, false, style->latexparam());
1909 case LATEX_ITEM_ENVIRONMENT:
1910 if (desc_on == 1) break;
1912 sgml::closeTag(ofs, depth + 1 + command_depth, false, end_tag);
1914 case LATEX_PARAGRAPH:
1915 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1918 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1924 for (int d = depth; d >= 0; --d) {
1925 if (!environment_stack[depth].empty()) {
1926 if (environment_inner[depth] != "!-- --") {
1927 item_name = "listitem";
1928 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1929 if (environment_inner[depth] == "varlistentry")
1930 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1933 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1937 for (int j = command_depth; j >= 0 ; --j)
1938 if (!command_stack[j].empty()) {
1939 sgml::closeTag(ofs, j, false, command_stack[j]);
1944 sgml::closeTag(ofs, 0, false, top_element);
1947 // How to check for successful close
1949 // we want this to be true outside previews (for insetexternal)
1954 void Buffer::simpleDocBookOnePar(ostream & os,
1955 ParagraphList::iterator par, int & desc_on,
1956 Paragraph::depth_type depth) const
1958 bool emph_flag = false;
1960 LyXLayout_ptr const & style = par->layout();
1962 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1964 int char_line_count = depth;
1965 //if (!style.free_spacing)
1966 // os << string(depth,' ');
1968 // parsing main loop
1969 for (pos_type i = 0; i < par->size(); ++i) {
1970 LyXFont font = par->getFont(params(), i, outerFont(par, paragraphs()));
1972 // handle <emphasis> tag
1973 if (font_old.emph() != font.emph()) {
1974 if (font.emph() == LyXFont::ON) {
1975 if (style->latexparam() == "CDATA")
1978 if (style->latexparam() == "CDATA")
1982 if (style->latexparam() == "CDATA")
1984 os << "</emphasis>";
1985 if (style->latexparam() == "CDATA")
1992 if (par->isInset(i)) {
1993 InsetOld * inset = par->getInset(i);
1994 // don't print the inset in position 0 if desc_on == 3 (label)
1995 if (i || desc_on != 3) {
1996 if (style->latexparam() == "CDATA")
1998 inset->docbook(*this, os, false);
1999 if (style->latexparam() == "CDATA")
2003 char c = par->getChar(i);
2006 boost::tie(ws, str) = sgml::escapeChar(c);
2008 if (style->pass_thru) {
2010 } else if (par->isFreeSpacing() || c != ' ') {
2012 } else if (desc_on == 1) {
2014 os << "\n</term><listitem><para>";
2024 if (style->latexparam() == "CDATA")
2026 os << "</emphasis>";
2027 if (style->latexparam() == "CDATA")
2031 // resets description flag correctly
2033 // <term> not closed...
2034 os << "</term>\n<listitem><para> </para>";
2036 if (style->free_spacing)
2041 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
2042 // Other flags: -wall -v0 -x
2043 int Buffer::runChktex()
2047 // get LaTeX-Filename
2048 string const name = getLatexName();
2049 string path = filePath();
2051 string const org_path = path;
2052 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
2056 Path p(path); // path to LaTeX file
2057 message(_("Running chktex..."));
2059 // Generate the LaTeX file if neccessary
2060 LatexRunParams runparams;
2061 runparams.flavor = LatexRunParams::LATEX;
2062 runparams.nice = false;
2063 makeLaTeXFile(name, org_path, runparams);
2066 Chktex chktex(lyxrc.chktex_command, name, filePath());
2067 int res = chktex.run(terr); // run chktex
2070 Alert::error(_("chktex failure"),
2071 _("Could not run chktex successfully."));
2072 } else if (res > 0) {
2073 // Insert all errors as errors boxes
2074 bufferErrors(*this, terr);
2083 void Buffer::validate(LaTeXFeatures & features) const
2085 LyXTextClass const & tclass = params().getLyXTextClass();
2087 if (params().tracking_changes) {
2088 features.require("dvipost");
2089 features.require("color");
2092 // AMS Style is at document level
2093 if (params().use_amsmath == BufferParams::AMS_ON
2094 || tclass.provides(LyXTextClass::amsmath))
2095 features.require("amsmath");
2097 for_each(paragraphs().begin(), paragraphs().end(),
2098 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
2100 // the bullet shapes are buffer level not paragraph level
2101 // so they are tested here
2102 for (int i = 0; i < 4; ++i) {
2103 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
2104 int const font = params().user_defined_bullet(i).getFont();
2106 int const c = params()
2107 .user_defined_bullet(i)
2114 features.require("latexsym");
2116 } else if (font == 1) {
2117 features.require("amssymb");
2118 } else if ((font >= 2 && font <= 5)) {
2119 features.require("pifont");
2124 if (lyxerr.debugging(Debug::LATEX)) {
2125 features.showStruct();
2130 void Buffer::getLabelList(std::vector<string> & list) const
2132 /// if this is a child document and the parent is already loaded
2133 /// Use the parent's list instead [ale990407]
2134 if (!params().parentname.empty()
2135 && bufferlist.exists(params().parentname)) {
2136 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2138 tmp->getLabelList(list);
2143 for (inset_iterator it = inset_const_iterator_begin();
2144 it != inset_const_iterator_end(); ++it) {
2145 it->getLabelList(*this, list);
2150 // This is also a buffer property (ale)
2151 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
2153 /// if this is a child document and the parent is already loaded
2154 /// use the parent's list instead [ale990412]
2155 if (!params().parentname.empty() &&
2156 bufferlist.exists(params().parentname)) {
2157 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2159 tmp->fillWithBibKeys(keys);
2164 for (inset_iterator it = inset_const_iterator_begin();
2165 it != inset_const_iterator_end(); ++it) {
2166 if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
2167 InsetBibtex const & inset =
2168 dynamic_cast<InsetBibtex const &>(*it);
2169 inset.fillWithBibKeys(*this, keys);
2170 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
2171 InsetInclude const & inset =
2172 dynamic_cast<InsetInclude const &>(*it);
2173 inset.fillWithBibKeys(*this, keys);
2174 } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
2175 InsetBibitem const & inset =
2176 dynamic_cast<InsetBibitem const &>(*it);
2177 string const key = inset.getContents();
2178 string const opt = inset.getOptions();
2179 string const ref; // = pit->asString(this, false);
2180 string const info = opt + "TheBibliographyRef" + ref;
2181 keys.push_back(pair<string, string>(key, info));
2187 bool Buffer::isDepClean(string const & name) const
2189 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
2190 if (it == pimpl_->dep_clean.end())
2196 void Buffer::markDepClean(string const & name)
2198 pimpl_->dep_clean[name] = true;
2202 bool Buffer::dispatch(string const & command, bool * result)
2204 return dispatch(lyxaction.lookupFunc(command), result);
2208 bool Buffer::dispatch(FuncRequest const & func, bool * result)
2210 bool dispatched = true;
2212 switch (func.action) {
2214 bool const tmp = Exporter::Export(this, func.argument, false);
2227 void Buffer::changeLanguage(Language const * from, Language const * to)
2229 lyxerr << "Changing Language!" << endl;
2231 // Take care of l10n/i18n
2234 ParIterator end = par_iterator_end();
2235 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2236 it->changeLanguage(params(), from, to);
2240 void Buffer::updateDocLang(Language const * nlang)
2242 pimpl_->messages.reset(new Messages(nlang->code()));
2246 bool Buffer::isMultiLingual()
2248 ParIterator end = par_iterator_end();
2249 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2250 if (it->isMultiLingual(params()))
2257 void Buffer::inset_iterator::setParagraph()
2259 while (pit != pend) {
2260 it = pit->insetlist.begin();
2261 if (it != pit->insetlist.end())
2268 InsetOld * Buffer::getInsetFromID(int id_arg) const
2270 for (inset_iterator it = inset_const_iterator_begin();
2271 it != inset_const_iterator_end(); ++it)
2273 if (it->id() == id_arg)
2275 InsetOld * in = it->getInsetFromID(id_arg);
2283 ParIterator Buffer::getParFromID(int id) const
2285 #warning FIXME: const correctness! (Andre)
2286 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
2287 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
2289 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
2291 // John says this is called with id == -1 from undo
2292 lyxerr << "getParFromID(), id: " << id << endl;
2296 for (; it != end; ++it)
2304 bool Buffer::hasParWithID(int id) const
2306 ParConstIterator it = par_iterator_begin();
2307 ParConstIterator end = par_iterator_end();
2310 // John says this is called with id == -1 from undo
2311 lyxerr << "hasParWithID(), id: " << id << endl;
2315 for (; it != end; ++it)
2323 ParIterator Buffer::par_iterator_begin()
2325 return ParIterator(paragraphs().begin(), paragraphs());
2329 ParIterator Buffer::par_iterator_end()
2331 return ParIterator(paragraphs().end(), paragraphs());
2335 ParConstIterator Buffer::par_iterator_begin() const
2337 ParagraphList const & pars = paragraphs();
2338 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
2342 ParConstIterator Buffer::par_iterator_end() const
2344 ParagraphList const & pars = paragraphs();
2345 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
2349 Language const * Buffer::getLanguage() const
2351 return params().language;
2355 string const Buffer::B_(string const & l10n) const
2357 if (pimpl_->messages.get()) {
2358 return pimpl_->messages->get(l10n);
2365 bool Buffer::isClean() const
2367 return pimpl_->lyx_clean;
2371 bool Buffer::isBakClean() const
2373 return pimpl_->bak_clean;
2377 void Buffer::markClean() const
2379 if (!pimpl_->lyx_clean) {
2380 pimpl_->lyx_clean = true;
2383 // if the .lyx file has been saved, we don't need an
2385 pimpl_->bak_clean = true;
2389 void Buffer::markBakClean()
2391 pimpl_->bak_clean = true;
2395 void Buffer::setUnnamed(bool flag)
2397 pimpl_->unnamed = flag;
2401 bool Buffer::isUnnamed()
2403 return pimpl_->unnamed;
2407 void Buffer::markDirty()
2409 if (pimpl_->lyx_clean) {
2410 pimpl_->lyx_clean = false;
2413 pimpl_->bak_clean = false;
2415 DepClean::iterator it = pimpl_->dep_clean.begin();
2416 DepClean::const_iterator const end = pimpl_->dep_clean.end();
2418 for (; it != end; ++it) {
2424 string const & Buffer::fileName() const
2426 return pimpl_->filename;
2430 string const & Buffer::filePath() const
2432 return pimpl_->filepath;
2436 bool Buffer::isReadonly() const
2438 return pimpl_->read_only;
2442 void Buffer::setParentName(string const & name)
2444 params().parentname = name;
2448 Buffer::inset_iterator::inset_iterator()
2453 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
2460 Buffer::inset_iterator Buffer::inset_iterator_begin()
2462 return inset_iterator(paragraphs().begin(), paragraphs().end());
2466 Buffer::inset_iterator Buffer::inset_iterator_end()
2468 return inset_iterator(paragraphs().end(), paragraphs().end());
2472 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
2474 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2475 return inset_iterator(pars.begin(), pars.end());
2479 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
2481 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2482 return inset_iterator(pars.end(), pars.end());
2486 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
2490 if (it == pit->insetlist.end()) {
2499 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
2501 inset_iterator tmp = *this;
2507 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
2513 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
2519 ParagraphList::iterator Buffer::inset_iterator::getPar() const
2525 lyx::pos_type Buffer::inset_iterator::getPos() const
2531 bool operator==(Buffer::inset_iterator const & iter1,
2532 Buffer::inset_iterator const & iter2)
2534 return iter1.pit == iter2.pit
2535 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
2539 bool operator!=(Buffer::inset_iterator const & iter1,
2540 Buffer::inset_iterator const & iter2)
2542 return !(iter1 == iter2);