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;
190 /** set to true only when the file is fully loaded.
191 * Used to prevent the premature generation of previews
192 * and by the citation inset.
194 bool file_fully_loaded;
198 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
200 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
201 filename(file), filepath(OnlyPath(file)), file_fully_loaded(false)
203 lyxvc.buffer(&parent);
204 if (readonly_ || lyxrc.use_tempdir)
205 temppath = CreateBufferTmpDir();
209 Buffer::Buffer(string const & file, bool ronly)
210 : pimpl_(new Impl(*this, file, ronly))
212 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
218 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
219 // here the buffer should take care that it is
220 // saved properly, before it goes into the void.
224 if (!temppath().empty() && destroyDir(temppath()) != 0) {
225 Alert::warning(_("Could not remove temporary directory"),
226 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
229 paragraphs().clear();
231 // Remove any previewed LaTeX snippets associated with this buffer.
232 lyx::graphics::Previews::get().removeLoader(*this);
236 limited_stack<Undo> & Buffer::undostack()
238 return pimpl_->undostack;
242 limited_stack<Undo> const & Buffer::undostack() const
244 return pimpl_->undostack;
248 limited_stack<Undo> & Buffer::redostack()
250 return pimpl_->redostack;
254 limited_stack<Undo> const & Buffer::redostack() const
256 return pimpl_->redostack;
260 BufferParams & Buffer::params()
262 return pimpl_->params;
266 BufferParams const & Buffer::params() const
268 return pimpl_->params;
272 ParagraphList & Buffer::paragraphs()
274 return pimpl_->paragraphs;
278 ParagraphList const & Buffer::paragraphs() const
280 return pimpl_->paragraphs;
284 LyXVC & Buffer::lyxvc()
286 return pimpl_->lyxvc;
290 LyXVC const & Buffer::lyxvc() const
292 return pimpl_->lyxvc;
296 string const & Buffer::temppath() const
298 return pimpl_->temppath;
302 bool & Buffer::niceFile()
304 return pimpl_->nicefile;
308 bool Buffer::niceFile() const
310 return pimpl_->nicefile;
314 TexRow & Buffer::texrow()
316 return pimpl_->texrow;
320 TexRow const & Buffer::texrow() const
322 return pimpl_->texrow;
326 string const Buffer::getLatexName(bool no_path) const
328 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
329 return no_path ? OnlyFilename(name) : name;
333 pair<Buffer::LogType, string> const Buffer::getLogName() const
335 string const filename = getLatexName(false);
337 if (filename.empty())
338 return make_pair(Buffer::latexlog, string());
340 string path = OnlyPath(filename);
342 if (lyxrc.use_tempdir || !IsDirWriteable(path))
345 string const fname = AddName(path,
346 OnlyFilename(ChangeExtension(filename,
349 AddName(path, OnlyFilename(
350 ChangeExtension(filename,
351 formats.extension("literate") + ".out")));
353 // If no Latex log or Build log is newer, show Build log
355 FileInfo const f_fi(fname);
356 FileInfo const b_fi(bname);
359 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
360 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
361 return make_pair(Buffer::buildlog, bname);
363 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
364 return make_pair(Buffer::latexlog, fname);
368 void Buffer::setReadonly(bool flag)
370 if (pimpl_->read_only != flag) {
371 pimpl_->read_only = flag;
377 void Buffer::setFileName(string const & newfile)
379 pimpl_->filename = MakeAbsPath(newfile);
380 pimpl_->filepath = OnlyPath(pimpl_->filename);
381 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
386 // We'll remove this later. (Lgb)
389 void unknownClass(string const & unknown)
391 Alert::warning(_("Unknown document class"),
392 bformat(_("Using the default document class, because the "
393 "class %1$s is unknown."), unknown));
398 int Buffer::readHeader(LyXLex & lex)
400 int unknown_tokens = 0;
404 string const token = lex.getString();
409 if (token == "\\end_header")
412 lyxerr[Debug::PARSER] << "Handling header token: `"
413 << token << '\'' << endl;
416 string unknown = params().readToken(lex, token);
417 if (!unknown.empty()) {
418 if (unknown[0] != '\\') {
419 unknownClass(unknown);
422 string const s = bformat(_("Unknown token: "
426 error(ErrorItem(_("Header error"), s,
431 return unknown_tokens;
435 // candidate for move to BufferView
436 // (at least some parts in the beginning of the func)
439 // changed to be public and have one parameter
440 // if par = 0 normal behavior
441 // else insert behavior
442 // Returns false if "\end_document" is not read (Asger)
443 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
445 Paragraph::depth_type depth = 0;
446 bool the_end_read = false;
448 if (paragraphs().empty()) {
450 if (!params().getLyXTextClass().load()) {
451 string theclass = params().getLyXTextClass().name();
452 Alert::error(_("Can't load document class"), bformat(
453 "Using the default document class, because the "
454 " class %1$s could not be loaded.", theclass));
455 params().textclass = 0;
458 // We don't want to adopt the parameters from the
459 // document we insert, so read them into a temporary buffer
460 // and then discard it
462 Buffer tmpbuf("", false);
463 tmpbuf.readHeader(lex);
468 string const token = lex.getString();
473 lyxerr[Debug::PARSER] << "Handling token: `"
474 << token << '\'' << endl;
476 if (token == "\\end_document") {
481 readParagraph(lex, token, paragraphs(), pit, depth);
488 int Buffer::readParagraph(LyXLex & lex, string const & token,
489 ParagraphList & pars, ParagraphList::iterator & pit,
490 lyx::depth_type & depth)
492 static Change current_change;
495 if (token == "\\begin_layout") {
496 lex.pushToken(token);
499 par.params().depth(depth);
500 if (params().tracking_changes)
502 LyXFont f(LyXFont::ALL_INHERIT, params().language);
506 if (pit != pars.end())
509 pit = pars.insert(pit, par);
511 // FIXME: goddamn InsetTabular makes us pass a Buffer
513 ::readParagraph(*this, *pit, lex);
515 } else if (token == "\\begin_deeper") {
517 } else if (token == "\\end_deeper") {
519 lex.printError("\\end_deeper: " "depth is already null");
530 // needed to insert the selection
531 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
532 LyXFont const & fn,string const & str)
534 LyXLayout_ptr const & layout = par->layout();
538 par->checkInsertChar(font);
539 // insert the string, don't insert doublespace
540 bool space_inserted = true;
541 bool autobreakrows = !par->inInset() ||
542 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
543 for(string::const_iterator cit = str.begin();
544 cit != str.end(); ++cit) {
546 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
547 breakParagraph(params(), paragraphs(), par, pos,
548 layout->isEnvironment());
551 space_inserted = true;
555 // do not insert consecutive spaces if !free_spacing
556 } else if ((*cit == ' ' || *cit == '\t') &&
557 space_inserted && !par->isFreeSpacing()) {
559 } else if (*cit == '\t') {
560 if (!par->isFreeSpacing()) {
561 // tabs are like spaces here
562 par->insertChar(pos, ' ', font);
564 space_inserted = true;
566 const pos_type nb = 8 - pos % 8;
567 for (pos_type a = 0; a < nb ; ++a) {
568 par->insertChar(pos, ' ', font);
571 space_inserted = true;
573 } else if (!IsPrintable(*cit)) {
574 // Ignore unprintables
577 // just insert the character
578 par->insertChar(pos, *cit, font);
580 space_inserted = (*cit == ' ');
587 bool Buffer::readFile(string const & filename)
589 // Check if the file is compressed.
590 string const format = getExtFromContents(filename);
591 if (format == "gzip" || format == "zip" || format == "compress") {
592 params().compressed = true;
595 bool ret = readFile(filename, paragraphs().begin());
597 // After we have read a file, we must ensure that the buffer
598 // language is set and used in the gui.
599 // If you know of a better place to put this, please tell me. (Lgb)
600 updateDocLang(params().language);
606 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
609 lex.setFile(filename);
611 return readFile(lex, filename, pit);
615 bool Buffer::fully_loaded() const
617 return pimpl_->file_fully_loaded;
621 bool Buffer::readFile(LyXLex & lex, string const & filename,
622 ParagraphList::iterator pit)
624 BOOST_ASSERT(!filename.empty());
627 Alert::error(_("Document could not be read"),
628 bformat(_("%1$s could not be read."), filename));
633 string const token(lex.getString());
636 Alert::error(_("Document could not be read"),
637 bformat(_("%1$s could not be read."), filename));
641 // the first token _must_ be...
642 if (token != "\\lyxformat") {
643 lyxerr << "Token: " << token << endl;
645 Alert::error(_("Document format failure"),
646 bformat(_("%1$s is not a LyX document."),
652 string tmp_format = lex.getString();
653 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
654 // if present remove ".," from string.
655 string::size_type dot = tmp_format.find_first_of(".,");
656 //lyxerr << " dot found at " << dot << endl;
657 if (dot != string::npos)
658 tmp_format.erase(dot, 1);
659 int file_format = strToInt(tmp_format);
660 //lyxerr << "format: " << file_format << endl;
662 if (file_format > LYX_FORMAT) {
663 Alert::warning(_("Document format failure"),
664 bformat(_("%1$s was created with a newer"
665 " version of LyX. This is likely to"
668 } else if (file_format < LYX_FORMAT) {
669 string const tmpfile = tempName();
670 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
671 if (command.empty()) {
672 Alert::error(_("Conversion script not found"),
673 bformat(_("%1$s is from an earlier"
674 " version of LyX, but the"
675 " conversion script lyx2lyx"
676 " could not be found."),
682 + " -o " + tmpfile + ' '
683 + QuoteName(filename);
684 lyxerr[Debug::INFO] << "Running '"
687 cmd_ret const ret = RunCommand(command);
688 if (ret.first != 0) {
689 Alert::error(_("Conversion script failed"),
690 bformat(_("%1$s is from an earlier version"
691 " of LyX, but the lyx2lyx script"
692 " failed to convert it."),
696 bool ret = readFile(tmpfile, pit);
697 // Do stuff with tmpfile name and buffer name here.
703 bool the_end = readBody(lex, pit);
704 params().setPaperStuff();
707 Alert::error(_("Document format failure"),
708 bformat(_("%1$s ended unexpectedly, which means"
709 " that it is probably corrupted."),
712 pimpl_->file_fully_loaded = true;
717 // Should probably be moved to somewhere else: BufferView? LyXView?
718 bool Buffer::save() const
720 // We don't need autosaves in the immediate future. (Asger)
721 resetAutosaveTimers();
725 if (lyxrc.make_backup) {
726 s = fileName() + '~';
727 if (!lyxrc.backupdir_path.empty())
728 s = AddName(lyxrc.backupdir_path,
729 subst(os::slashify_path(s),'/','!'));
731 // Rename is the wrong way of making a backup,
732 // this is the correct way.
733 /* truss cp fil fil2:
734 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
735 stat("LyXVC.lyx", 0xEFFFF688) = 0
736 open("LyXVC.lyx", O_RDONLY) = 3
737 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
738 fstat(4, 0xEFFFF508) = 0
739 fstat(3, 0xEFFFF508) = 0
740 read(3, " # T h i s f i l e w".., 8192) = 5579
741 write(4, " # T h i s f i l e w".., 5579) = 5579
742 read(3, 0xEFFFD4A0, 8192) = 0
745 chmod("LyXVC3.lyx", 0100644) = 0
746 lseek(0, 0, SEEK_CUR) = 46440
750 // Should probably have some more error checking here.
751 // Doing it this way, also makes the inodes stay the same.
752 // This is still not a very good solution, in particular we
753 // might loose the owner of the backup.
754 FileInfo finfo(fileName());
756 mode_t fmode = finfo.getMode();
757 struct utimbuf times = {
758 finfo.getAccessTime(),
759 finfo.getModificationTime() };
761 ifstream ifs(fileName().c_str());
762 ofstream ofs(s.c_str(), ios::out|ios::trunc);
767 ::chmod(s.c_str(), fmode);
769 if (::utime(s.c_str(), ×)) {
770 lyxerr << "utime error." << endl;
773 lyxerr << "LyX was not able to make "
774 "backup copy. Beware." << endl;
779 if (writeFile(fileName())) {
781 removeAutosaveFile(fileName());
783 // Saving failed, so backup is not backup
784 if (lyxrc.make_backup) {
785 rename(s, fileName());
793 bool Buffer::writeFile(string const & fname) const
795 if (pimpl_->read_only && (fname == fileName())) {
799 FileInfo finfo(fname);
800 if (finfo.exist() && !finfo.writable()) {
806 if (params().compressed) {
807 gz::ogzstream ofs(fname.c_str());
812 retval = do_writeFile(ofs);
815 ofstream ofs(fname.c_str());
819 retval = do_writeFile(ofs);
826 bool Buffer::do_writeFile(ostream & ofs) const
830 // Use the standard "C" locale for file output.
831 ofs.imbue(std::locale::classic());
834 // The top of the file should not be written by params().
836 // write out a comment in the top of the file
837 ofs << "#LyX " << lyx_version
838 << " created this file. For more info see http://www.lyx.org/\n"
839 << "\\lyxformat " << LYX_FORMAT << "\n";
841 // now write out the buffer paramters.
842 params().writeFile(ofs);
844 ofs << "\\end_header\n";
846 Paragraph::depth_type depth = 0;
848 // this will write out all the paragraphs
849 // using recursive descent.
850 ParagraphList::const_iterator pit = paragraphs().begin();
851 ParagraphList::const_iterator pend = paragraphs().end();
852 for (; pit != pend; ++pit)
853 pit->write(*this, ofs, params(), depth);
855 // Write marker that shows file is complete
856 ofs << "\n\\end_document" << endl;
858 // Shouldn't really be needed....
861 // how to check if close went ok?
862 // Following is an attempt... (BE 20001011)
864 // good() returns false if any error occured, including some
866 // bad() returns true if something bad happened in the buffer,
867 // which should include file system full errors.
874 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
876 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
888 pair<int, string> const addDepth(int depth, int ldepth)
892 d += (ldepth - depth) * 2;
893 return make_pair(d, string(d, ' '));
899 string const Buffer::asciiParagraph(Paragraph const & par,
900 unsigned int linelen,
901 bool noparbreak) const
903 ostringstream buffer;
905 Paragraph::depth_type ltype_depth = 0;
906 bool ref_printed = false;
907 Paragraph::depth_type depth = par.params().depth();
909 // First write the layout
910 string const & tmp = par.layout()->name();
911 if (compare_no_case(tmp, "itemize") == 0) {
913 ltype_depth = depth + 1;
914 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
916 ltype_depth = depth + 1;
917 } else if (contains(ascii_lowercase(tmp), "ection")) {
919 ltype_depth = depth + 1;
920 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
922 ltype_depth = depth + 1;
923 } else if (compare_ascii_no_case(tmp, "description") == 0) {
925 ltype_depth = depth + 1;
926 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
929 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
937 /* maybe some vertical spaces */
939 /* the labelwidthstring used in lists */
943 /* some pagebreaks? */
947 /* what about the alignment */
949 // linelen <= 0 is special and means we don't have paragraph breaks
951 string::size_type currlinelen = 0;
957 buffer << string(depth * 2, ' ');
958 currlinelen += depth * 2;
961 // we should probably change to the paragraph language in the
962 // gettext here (if possible) so that strings are outputted in
963 // the correct language! (20012712 Jug)
967 case 4: // (Sub)Paragraph
968 case 5: // Description
972 buffer << _("Abstract") << "\n\n";
975 string const abst = _("Abstract: ");
977 currlinelen += abst.length();
980 case 7: // Bibliography
983 buffer << _("References") << "\n\n";
986 string const refs = _("References: ");
988 currlinelen += refs.length();
996 string const parlab = par.params().labelString();
997 buffer << parlab << ' ';
998 currlinelen += parlab.length() + 1;
1006 pair<int, string> p = addDepth(depth, ltype_depth);
1008 currlinelen += p.first;
1011 // this is to change the linebreak to do it by word a bit more
1012 // intelligent hopefully! (only in the case where we have a
1013 // max linelength!) (Jug)
1017 for (pos_type i = 0; i < par.size(); ++i) {
1018 char c = par.getUChar(params(), i);
1020 case Paragraph::META_INSET:
1022 InsetOld const * inset = par.getInset(i);
1026 currlinelen += word.length();
1029 if (inset->ascii(*this, buffer, linelen)) {
1030 // to be sure it breaks paragraph
1031 currlinelen += linelen;
1040 currlinelen + word.length() > linelen - 10) {
1042 pair<int, string> p = addDepth(depth, ltype_depth);
1044 currlinelen = p.first;
1047 buffer << word << ' ';
1048 currlinelen += word.length() + 1;
1055 lyxerr[Debug::INFO] <<
1056 "writeAsciiFile: NULL char in structure." << endl;
1058 if ((linelen > 0) &&
1059 (currlinelen + word.length()) > linelen)
1063 pair<int, string> p =
1064 addDepth(depth, ltype_depth);
1066 currlinelen = p.first;
1073 return buffer.str();
1077 void Buffer::writeFileAscii(string const & fname, int linelen)
1080 if (!::openFileWrite(ofs, fname))
1082 writeFileAscii(ofs, linelen);
1086 void Buffer::writeFileAscii(ostream & os, int linelen)
1088 ParagraphList::iterator beg = paragraphs().begin();
1089 ParagraphList::iterator end = paragraphs().end();
1090 ParagraphList::iterator it = beg;
1091 for (; it != end; ++it) {
1092 os << asciiParagraph(*it, linelen, it == beg);
1098 void Buffer::makeLaTeXFile(string const & fname,
1099 string const & original_path,
1100 LatexRunParams const & runparams,
1101 bool output_preamble, bool output_body)
1103 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1106 if (!::openFileWrite(ofs, fname))
1109 makeLaTeXFile(ofs, original_path,
1110 runparams, output_preamble, output_body);
1114 lyxerr << "File was not closed properly." << endl;
1119 void Buffer::makeLaTeXFile(ostream & os,
1120 string const & original_path,
1121 LatexRunParams const & runparams_in,
1122 bool output_preamble, bool output_body)
1124 LatexRunParams runparams = runparams_in;
1125 niceFile() = runparams.nice; // this will be used by Insetincludes.
1127 // validate the buffer.
1128 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1129 LaTeXFeatures features(*this, params());
1131 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1134 // The starting paragraph of the coming rows is the
1135 // first paragraph of the document. (Asger)
1136 texrow().start(paragraphs().begin()->id(), 0);
1138 if (output_preamble && runparams.nice) {
1139 os << "%% LyX " << lyx_version << " created this file. "
1140 "For more info, see http://www.lyx.org/.\n"
1141 "%% Do not edit unless you really know what "
1146 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1147 // There are a few differences between nice LaTeX and usual files:
1148 // usual is \batchmode and has a
1149 // special input@path to allow the including of figures
1150 // with either \input or \includegraphics (what figinsets do).
1151 // input@path is set when the actual parameter
1152 // original_path is set. This is done for usual tex-file, but not
1153 // for nice-latex-file. (Matthias 250696)
1154 if (output_preamble) {
1155 if (!runparams.nice) {
1156 // code for usual, NOT nice-latex-file
1157 os << "\\batchmode\n"; // changed
1158 // from \nonstopmode
1161 if (!original_path.empty()) {
1162 string inputpath = os::external_path(original_path);
1163 subst(inputpath, "~", "\\string~");
1164 os << "\\makeatletter\n"
1165 << "\\def\\input@path{{"
1166 << inputpath << "/}}\n"
1167 << "\\makeatother\n";
1173 // Write the preamble
1174 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1180 os << "\\begin{document}\n";
1182 } // output_preamble
1183 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
1185 if (!lyxrc.language_auto_begin) {
1186 os << subst(lyxrc.language_command_begin, "$$lang",
1187 params().language->babel())
1192 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1194 // add this just in case after all the paragraphs
1198 if (!lyxrc.language_auto_end) {
1199 os << subst(lyxrc.language_command_end, "$$lang",
1200 params().language->babel())
1205 if (output_preamble) {
1206 os << "\\end{document}\n";
1209 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1211 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1215 // Just to be sure. (Asger)
1218 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1219 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1222 // we want this to be true outside previews (for insetexternal)
1227 bool Buffer::isLatex() const
1229 return params().getLyXTextClass().outputType() == LATEX;
1233 bool Buffer::isLinuxDoc() const
1235 return params().getLyXTextClass().outputType() == LINUXDOC;
1239 bool Buffer::isLiterate() const
1241 return params().getLyXTextClass().outputType() == LITERATE;
1245 bool Buffer::isDocBook() const
1247 return params().getLyXTextClass().outputType() == DOCBOOK;
1251 bool Buffer::isSGML() const
1253 LyXTextClass const & tclass = params().getLyXTextClass();
1255 return tclass.outputType() == LINUXDOC ||
1256 tclass.outputType() == DOCBOOK;
1260 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
1263 if (!::openFileWrite(ofs, fname))
1266 niceFile() = nice; // this will be used by included files.
1268 LaTeXFeatures features(*this, params());
1274 LyXTextClass const & tclass = params().getLyXTextClass();
1276 string top_element = tclass.latexname();
1279 ofs << "<!doctype linuxdoc system";
1281 string preamble = params().preamble;
1282 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1284 preamble += features.getIncludedFiles(name);
1285 preamble += features.getLyXSGMLEntities();
1287 if (!preamble.empty()) {
1288 ofs << " [ " << preamble << " ]";
1292 if (params().options.empty())
1293 sgml::openTag(ofs, 0, false, top_element);
1295 string top = top_element;
1297 top += params().options;
1298 sgml::openTag(ofs, 0, false, top);
1302 ofs << "<!-- LyX " << lyx_version
1303 << " created this file. For more info see http://www.lyx.org/"
1306 Paragraph::depth_type depth = 0; // paragraph depth
1308 vector<string> environment_stack(5);
1310 ParagraphList::iterator pit = paragraphs().begin();
1311 ParagraphList::iterator pend = paragraphs().end();
1312 for (; pit != pend; ++pit) {
1313 LyXLayout_ptr const & style = pit->layout();
1314 // treat <toc> as a special case for compatibility with old code
1315 if (pit->isInset(0)) {
1316 InsetOld * inset = pit->getInset(0);
1317 InsetOld::Code lyx_code = inset->lyxCode();
1318 if (lyx_code == InsetOld::TOC_CODE) {
1319 string const temp = "toc";
1320 sgml::openTag(ofs, depth, false, temp);
1325 // environment tag closing
1326 for (; depth > pit->params().depth(); --depth) {
1327 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1328 environment_stack[depth].erase();
1331 // write opening SGML tags
1332 switch (style->latextype) {
1333 case LATEX_PARAGRAPH:
1334 if (depth == pit->params().depth()
1335 && !environment_stack[depth].empty()) {
1336 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1337 environment_stack[depth].erase();
1343 sgml::openTag(ofs, depth, false, style->latexname());
1348 error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1350 if (!environment_stack[depth].empty()) {
1351 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1355 environment_stack[depth].erase();
1356 sgml::openTag(ofs, depth, false, style->latexname());
1359 case LATEX_ENVIRONMENT:
1360 case LATEX_ITEM_ENVIRONMENT:
1361 case LATEX_BIB_ENVIRONMENT:
1363 string const & latexname = style->latexname();
1365 if (depth == pit->params().depth()
1366 && environment_stack[depth] != latexname) {
1367 sgml::closeTag(ofs, depth, false,
1368 environment_stack[depth]);
1369 environment_stack[depth].erase();
1371 if (depth < pit->params().depth()) {
1372 depth = pit->params().depth();
1373 environment_stack[depth].erase();
1375 if (environment_stack[depth] != latexname) {
1377 sgml::openTag(ofs, depth, false, "p");
1379 sgml::openTag(ofs, depth, false, latexname);
1381 if (environment_stack.size() == depth + 1)
1382 environment_stack.push_back("!-- --");
1383 environment_stack[depth] = latexname;
1386 if (style->latexparam() == "CDATA")
1389 if (style->latextype == LATEX_ENVIRONMENT) break;
1391 if (style->labeltype == LABEL_MANUAL)
1396 sgml::openTag(ofs, depth + 1, false, item_name);
1401 sgml::openTag(ofs, depth, false, style->latexname());
1405 simpleLinuxDocOnePar(ofs, pit, depth);
1408 // write closing SGML tags
1409 switch (style->latextype) {
1412 case LATEX_ENVIRONMENT:
1413 case LATEX_ITEM_ENVIRONMENT:
1414 case LATEX_BIB_ENVIRONMENT:
1415 if (style->latexparam() == "CDATA")
1419 sgml::closeTag(ofs, depth, false, style->latexname());
1425 for (int i = depth; i >= 0; --i)
1426 sgml::closeTag(ofs, depth, false, environment_stack[i]);
1430 sgml::closeTag(ofs, 0, false, top_element);
1434 // How to check for successful close
1436 // we want this to be true outside previews (for insetexternal)
1441 // checks, if newcol chars should be put into this line
1442 // writes newline, if necessary.
1445 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1446 string::size_type newcol)
1449 if (colcount > lyxrc.ascii_linelen) {
1451 colcount = newcol; // assume write after this call
1466 string tag_name(PAR_TAG const & pt) {
1468 case NONE: return "!-- --";
1469 case TT: return "tt";
1470 case SF: return "sf";
1471 case BF: return "bf";
1472 case IT: return "it";
1473 case SL: return "sl";
1474 case EM: return "em";
1481 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1483 p1 = static_cast<PAR_TAG>(p1 | p2);
1488 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1490 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1496 // Handle internal paragraph parsing -- layout already processed.
1497 void Buffer::simpleLinuxDocOnePar(ostream & os,
1498 ParagraphList::iterator par,
1499 lyx::depth_type /*depth*/) const
1501 LyXLayout_ptr const & style = par->layout();
1503 string::size_type char_line_count = 5; // Heuristic choice ;-)
1505 // gets paragraph main font
1508 if (style->labeltype == LABEL_MANUAL) {
1509 font_old = style->labelfont;
1512 font_old = style->font;
1516 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1517 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1518 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1521 stack<PAR_TAG> tag_state;
1522 // parsing main loop
1523 for (pos_type i = 0; i < par->size(); ++i) {
1525 PAR_TAG tag_close = NONE;
1526 list < PAR_TAG > tag_open;
1528 LyXFont const font = par->getFont(params(), i, outerFont(par, paragraphs()));
1530 if (font_old.family() != font.family()) {
1531 switch (family_type) {
1532 case LyXFont::SANS_FAMILY:
1535 case LyXFont::TYPEWRITER_FAMILY:
1542 family_type = font.family();
1544 switch (family_type) {
1545 case LyXFont::SANS_FAMILY:
1546 tag_open.push_back(SF);
1548 case LyXFont::TYPEWRITER_FAMILY:
1549 tag_open.push_back(TT);
1556 if (font_old.series() != font.series()) {
1557 switch (series_type) {
1558 case LyXFont::BOLD_SERIES:
1565 series_type = font.series();
1567 switch (series_type) {
1568 case LyXFont::BOLD_SERIES:
1569 tag_open.push_back(BF);
1577 if (font_old.shape() != font.shape()) {
1578 switch (shape_type) {
1579 case LyXFont::ITALIC_SHAPE:
1582 case LyXFont::SLANTED_SHAPE:
1589 shape_type = font.shape();
1591 switch (shape_type) {
1592 case LyXFont::ITALIC_SHAPE:
1593 tag_open.push_back(IT);
1595 case LyXFont::SLANTED_SHAPE:
1596 tag_open.push_back(SL);
1603 if (font_old.emph() != font.emph()) {
1604 if (font.emph() == LyXFont::ON) {
1605 tag_open.push_back(EM);
1614 list < PAR_TAG > temp;
1615 while (!tag_state.empty() && tag_close) {
1616 PAR_TAG k = tag_state.top();
1618 os << "</" << tag_name(k) << '>';
1625 for(list< PAR_TAG >::const_iterator j = temp.begin();
1626 j != temp.end(); ++j) {
1628 os << '<' << tag_name(*j) << '>';
1631 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1632 j != tag_open.end(); ++j) {
1634 os << '<' << tag_name(*j) << '>';
1637 char c = par->getChar(i);
1639 if (c == Paragraph::META_INSET) {
1640 InsetOld * inset = par->getInset(i);
1641 inset->linuxdoc(*this, os);
1646 if (style->latexparam() == "CDATA") {
1647 // "TeX"-Mode on == > SGML-Mode on.
1654 boost::tie(ws, str) = sgml::escapeChar(c);
1655 if (ws && !par->isFreeSpacing()) {
1656 // in freespacing mode, spaces are
1657 // non-breaking characters
1658 if (desc_on) {// if char is ' ' then...
1661 sgmlLineBreak(os, char_line_count, 6);
1665 sgmlLineBreak(os, char_line_count, 1);
1670 char_line_count += str.length();
1676 while (!tag_state.empty()) {
1677 os << "</" << tag_name(tag_state.top()) << '>';
1681 // resets description flag correctly
1683 // <tag> not closed...
1684 sgmlLineBreak(os, char_line_count, 6);
1690 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
1693 if (!::openFileWrite(ofs, fname))
1696 niceFile() = nice; // this will be used by Insetincludes.
1698 LaTeXFeatures features(*this, params());
1703 LyXTextClass const & tclass = params().getLyXTextClass();
1704 string top_element = tclass.latexname();
1707 ofs << "<!DOCTYPE " << top_element
1708 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
1710 string preamble = params().preamble;
1711 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1713 preamble += features.getIncludedFiles(name);
1714 preamble += features.getLyXSGMLEntities();
1716 if (!preamble.empty()) {
1717 ofs << "\n [ " << preamble << " ]";
1722 string top = top_element;
1724 top += params().language->code();
1727 if (!params().options.empty()) {
1729 top += params().options;
1731 sgml::openTag(ofs, 0, false, top);
1733 ofs << "<!-- DocBook file was created by LyX " << lyx_version
1734 << "\n See http://www.lyx.org/ for more information -->\n";
1736 vector<string> environment_stack(10);
1737 vector<string> environment_inner(10);
1738 vector<string> command_stack(10);
1740 bool command_flag = false;
1741 Paragraph::depth_type command_depth = 0;
1742 Paragraph::depth_type command_base = 0;
1743 Paragraph::depth_type cmd_depth = 0;
1744 Paragraph::depth_type depth = 0; // paragraph depth
1747 string command_name;
1749 ParagraphList::iterator par = paragraphs().begin();
1750 ParagraphList::iterator pend = paragraphs().end();
1752 for (; par != pend; ++par) {
1756 int desc_on = 0; // description mode
1758 LyXLayout_ptr const & style = par->layout();
1760 // environment tag closing
1761 for (; depth > par->params().depth(); --depth) {
1762 if (environment_inner[depth] != "!-- --" && !environment_inner[depth].empty()) {
1763 item_name = "listitem";
1764 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1765 if (environment_inner[depth] == "varlistentry")
1766 sgml::closeTag(ofs, depth+command_depth, false, environment_inner[depth]);
1768 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1769 environment_stack[depth].erase();
1770 environment_inner[depth].erase();
1773 if (depth == par->params().depth()
1774 && environment_stack[depth] != style->latexname()
1775 && !environment_stack[depth].empty()) {
1776 if (environment_inner[depth] != "!-- --") {
1777 item_name= "listitem";
1778 sgml::closeTag(ofs, command_depth+depth, false, item_name);
1779 if (environment_inner[depth] == "varlistentry")
1780 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1783 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1785 environment_stack[depth].erase();
1786 environment_inner[depth].erase();
1789 // Write opening SGML tags.
1790 switch (style->latextype) {
1791 case LATEX_PARAGRAPH:
1792 sgml::openTag(ofs, depth + command_depth,
1793 false, style->latexname());
1798 error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1800 command_name = style->latexname();
1802 sgmlparam = style->latexparam();
1803 c_params = split(sgmlparam, c_depth,'|');
1805 cmd_depth = atoi(c_depth);
1808 if (cmd_depth < command_base) {
1809 for (Paragraph::depth_type j = command_depth;
1810 j >= command_base; --j) {
1811 sgml::closeTag(ofs, j, false, command_stack[j]);
1814 command_depth = command_base = cmd_depth;
1815 } else if (cmd_depth <= command_depth) {
1816 for (int j = command_depth;
1817 j >= int(cmd_depth); --j) {
1818 sgml::closeTag(ofs, j, false, command_stack[j]);
1821 command_depth = cmd_depth;
1823 command_depth = cmd_depth;
1825 command_depth = command_base = cmd_depth;
1826 command_flag = true;
1828 if (command_stack.size() == command_depth + 1)
1829 command_stack.push_back(string());
1830 command_stack[command_depth] = command_name;
1832 // treat label as a special case for
1833 // more WYSIWYM handling.
1834 // This is a hack while paragraphs can't have
1835 // attributes, like id in this case.
1836 if (par->isInset(0)) {
1837 InsetOld * inset = par->getInset(0);
1838 InsetOld::Code lyx_code = inset->lyxCode();
1839 if (lyx_code == InsetOld::LABEL_CODE) {
1840 command_name += " id=\"";
1841 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1842 command_name += '"';
1847 sgml::openTag(ofs, depth + command_depth, false, command_name);
1849 item_name = c_params.empty() ? "title" : c_params;
1850 sgml::openTag(ofs, depth + 1 + command_depth, false, item_name);
1853 case LATEX_ENVIRONMENT:
1854 case LATEX_ITEM_ENVIRONMENT:
1855 if (depth < par->params().depth()) {
1856 depth = par->params().depth();
1857 environment_stack[depth].erase();
1860 if (environment_stack[depth] != style->latexname()) {
1861 if (environment_stack.size() == depth + 1) {
1862 environment_stack.push_back("!-- --");
1863 environment_inner.push_back("!-- --");
1865 environment_stack[depth] = style->latexname();
1866 environment_inner[depth] = "!-- --";
1867 sgml::openTag(ofs, depth + command_depth, false, environment_stack[depth]);
1869 if (environment_inner[depth] != "!-- --") {
1870 item_name= "listitem";
1871 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1872 if (environment_inner[depth] == "varlistentry")
1873 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1877 if (style->latextype == LATEX_ENVIRONMENT) {
1878 if (!style->latexparam().empty()) {
1879 if (style->latexparam() == "CDATA")
1882 sgml::openTag(ofs, depth + command_depth, false, style->latexparam());
1887 desc_on = (style->labeltype == LABEL_MANUAL);
1889 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1890 sgml::openTag(ofs, depth + 1 + command_depth,
1891 false, environment_inner[depth]);
1893 item_name = desc_on ? "term" : "para";
1894 sgml::openTag(ofs, depth + 1 + command_depth,
1898 sgml::openTag(ofs, depth + command_depth,
1899 false, style->latexname());
1903 simpleDocBookOnePar(ofs, par, desc_on,
1904 depth + 1 + command_depth);
1907 // write closing SGML tags
1908 switch (style->latextype) {
1910 end_tag = c_params.empty() ? "title" : c_params;
1911 sgml::closeTag(ofs, depth + command_depth,
1914 case LATEX_ENVIRONMENT:
1915 if (!style->latexparam().empty()) {
1916 if (style->latexparam() == "CDATA")
1919 sgml::closeTag(ofs, depth + command_depth, false, style->latexparam());
1922 case LATEX_ITEM_ENVIRONMENT:
1923 if (desc_on == 1) break;
1925 sgml::closeTag(ofs, depth + 1 + command_depth, false, end_tag);
1927 case LATEX_PARAGRAPH:
1928 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1931 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1937 for (int d = depth; d >= 0; --d) {
1938 if (!environment_stack[depth].empty()) {
1939 if (environment_inner[depth] != "!-- --") {
1940 item_name = "listitem";
1941 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1942 if (environment_inner[depth] == "varlistentry")
1943 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1946 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1950 for (int j = command_depth; j >= 0 ; --j)
1951 if (!command_stack[j].empty()) {
1952 sgml::closeTag(ofs, j, false, command_stack[j]);
1957 sgml::closeTag(ofs, 0, false, top_element);
1960 // How to check for successful close
1962 // we want this to be true outside previews (for insetexternal)
1967 void Buffer::simpleDocBookOnePar(ostream & os,
1968 ParagraphList::iterator par, int & desc_on,
1969 lyx::depth_type depth) const
1971 bool emph_flag = false;
1973 LyXLayout_ptr const & style = par->layout();
1975 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1977 int char_line_count = depth;
1978 //if (!style.free_spacing)
1979 // os << string(depth,' ');
1981 // parsing main loop
1982 for (pos_type i = 0; i < par->size(); ++i) {
1983 LyXFont font = par->getFont(params(), i, outerFont(par, paragraphs()));
1985 // handle <emphasis> tag
1986 if (font_old.emph() != font.emph()) {
1987 if (font.emph() == LyXFont::ON) {
1988 if (style->latexparam() == "CDATA")
1991 if (style->latexparam() == "CDATA")
1995 if (style->latexparam() == "CDATA")
1997 os << "</emphasis>";
1998 if (style->latexparam() == "CDATA")
2005 if (par->isInset(i)) {
2006 InsetOld * inset = par->getInset(i);
2007 // don't print the inset in position 0 if desc_on == 3 (label)
2008 if (i || desc_on != 3) {
2009 if (style->latexparam() == "CDATA")
2011 inset->docbook(*this, os, false);
2012 if (style->latexparam() == "CDATA")
2016 char c = par->getChar(i);
2019 boost::tie(ws, str) = sgml::escapeChar(c);
2021 if (style->pass_thru) {
2023 } else if (par->isFreeSpacing() || c != ' ') {
2025 } else if (desc_on == 1) {
2027 os << "\n</term><listitem><para>";
2037 if (style->latexparam() == "CDATA")
2039 os << "</emphasis>";
2040 if (style->latexparam() == "CDATA")
2044 // resets description flag correctly
2046 // <term> not closed...
2047 os << "</term>\n<listitem><para> </para>";
2049 if (style->free_spacing)
2054 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
2055 // Other flags: -wall -v0 -x
2056 int Buffer::runChktex()
2060 // get LaTeX-Filename
2061 string const name = getLatexName();
2062 string path = filePath();
2064 string const org_path = path;
2065 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
2069 Path p(path); // path to LaTeX file
2070 message(_("Running chktex..."));
2072 // Generate the LaTeX file if neccessary
2073 LatexRunParams runparams;
2074 runparams.flavor = LatexRunParams::LATEX;
2075 runparams.nice = false;
2076 makeLaTeXFile(name, org_path, runparams);
2079 Chktex chktex(lyxrc.chktex_command, name, filePath());
2080 int res = chktex.run(terr); // run chktex
2083 Alert::error(_("chktex failure"),
2084 _("Could not run chktex successfully."));
2085 } else if (res > 0) {
2086 // Insert all errors as errors boxes
2087 bufferErrors(*this, terr);
2096 void Buffer::validate(LaTeXFeatures & features) const
2098 LyXTextClass const & tclass = params().getLyXTextClass();
2100 if (params().tracking_changes) {
2101 features.require("dvipost");
2102 features.require("color");
2105 // AMS Style is at document level
2106 if (params().use_amsmath == BufferParams::AMS_ON
2107 || tclass.provides(LyXTextClass::amsmath))
2108 features.require("amsmath");
2110 for_each(paragraphs().begin(), paragraphs().end(),
2111 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
2113 // the bullet shapes are buffer level not paragraph level
2114 // so they are tested here
2115 for (int i = 0; i < 4; ++i) {
2116 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
2117 int const font = params().user_defined_bullet(i).getFont();
2119 int const c = params()
2120 .user_defined_bullet(i)
2127 features.require("latexsym");
2129 } else if (font == 1) {
2130 features.require("amssymb");
2131 } else if ((font >= 2 && font <= 5)) {
2132 features.require("pifont");
2137 if (lyxerr.debugging(Debug::LATEX)) {
2138 features.showStruct();
2143 void Buffer::getLabelList(std::vector<string> & list) const
2145 /// if this is a child document and the parent is already loaded
2146 /// Use the parent's list instead [ale990407]
2147 if (!params().parentname.empty()
2148 && bufferlist.exists(params().parentname)) {
2149 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2151 tmp->getLabelList(list);
2156 for (inset_iterator it = inset_const_iterator_begin();
2157 it != inset_const_iterator_end(); ++it) {
2158 it->getLabelList(*this, list);
2163 // This is also a buffer property (ale)
2164 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
2166 /// if this is a child document and the parent is already loaded
2167 /// use the parent's list instead [ale990412]
2168 if (!params().parentname.empty() &&
2169 bufferlist.exists(params().parentname)) {
2170 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2172 tmp->fillWithBibKeys(keys);
2177 for (inset_iterator it = inset_const_iterator_begin();
2178 it != inset_const_iterator_end(); ++it) {
2179 if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
2180 InsetBibtex const & inset =
2181 dynamic_cast<InsetBibtex const &>(*it);
2182 inset.fillWithBibKeys(*this, keys);
2183 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
2184 InsetInclude const & inset =
2185 dynamic_cast<InsetInclude const &>(*it);
2186 inset.fillWithBibKeys(*this, keys);
2187 } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
2188 InsetBibitem const & inset =
2189 dynamic_cast<InsetBibitem const &>(*it);
2190 string const key = inset.getContents();
2191 string const opt = inset.getOptions();
2192 string const ref; // = pit->asString(this, false);
2193 string const info = opt + "TheBibliographyRef" + ref;
2194 keys.push_back(pair<string, string>(key, info));
2200 bool Buffer::isDepClean(string const & name) const
2202 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
2203 if (it == pimpl_->dep_clean.end())
2209 void Buffer::markDepClean(string const & name)
2211 pimpl_->dep_clean[name] = true;
2215 bool Buffer::dispatch(string const & command, bool * result)
2217 return dispatch(lyxaction.lookupFunc(command), result);
2221 bool Buffer::dispatch(FuncRequest const & func, bool * result)
2223 bool dispatched = true;
2225 switch (func.action) {
2227 bool const tmp = Exporter::Export(this, func.argument, false);
2240 void Buffer::changeLanguage(Language const * from, Language const * to)
2242 lyxerr << "Changing Language!" << endl;
2244 // Take care of l10n/i18n
2247 ParIterator end = par_iterator_end();
2248 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2249 it->changeLanguage(params(), from, to);
2253 void Buffer::updateDocLang(Language const * nlang)
2255 pimpl_->messages.reset(new Messages(nlang->code()));
2259 bool Buffer::isMultiLingual()
2261 ParIterator end = par_iterator_end();
2262 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2263 if (it->isMultiLingual(params()))
2270 void Buffer::inset_iterator::setParagraph()
2272 while (pit != pend) {
2273 it = pit->insetlist.begin();
2274 if (it != pit->insetlist.end())
2281 InsetOld * Buffer::getInsetFromID(int id_arg) const
2283 for (inset_iterator it = inset_const_iterator_begin();
2284 it != inset_const_iterator_end(); ++it)
2286 if (it->id() == id_arg)
2288 InsetOld * in = it->getInsetFromID(id_arg);
2296 ParIterator Buffer::getParFromID(int id) const
2298 #warning FIXME: const correctness! (Andre)
2299 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
2300 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
2302 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
2304 // John says this is called with id == -1 from undo
2305 lyxerr << "getParFromID(), id: " << id << endl;
2309 for (; it != end; ++it)
2317 bool Buffer::hasParWithID(int id) const
2319 ParConstIterator it = par_iterator_begin();
2320 ParConstIterator end = par_iterator_end();
2323 // John says this is called with id == -1 from undo
2324 lyxerr << "hasParWithID(), id: " << id << endl;
2328 for (; it != end; ++it)
2336 ParIterator Buffer::par_iterator_begin()
2338 return ParIterator(paragraphs().begin(), paragraphs());
2342 ParIterator Buffer::par_iterator_end()
2344 return ParIterator(paragraphs().end(), paragraphs());
2348 ParConstIterator Buffer::par_iterator_begin() const
2350 ParagraphList const & pars = paragraphs();
2351 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
2355 ParConstIterator Buffer::par_iterator_end() const
2357 ParagraphList const & pars = paragraphs();
2358 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
2362 Language const * Buffer::getLanguage() const
2364 return params().language;
2368 string const Buffer::B_(string const & l10n) const
2370 if (pimpl_->messages.get()) {
2371 return pimpl_->messages->get(l10n);
2378 bool Buffer::isClean() const
2380 return pimpl_->lyx_clean;
2384 bool Buffer::isBakClean() const
2386 return pimpl_->bak_clean;
2390 void Buffer::markClean() const
2392 if (!pimpl_->lyx_clean) {
2393 pimpl_->lyx_clean = true;
2396 // if the .lyx file has been saved, we don't need an
2398 pimpl_->bak_clean = true;
2402 void Buffer::markBakClean()
2404 pimpl_->bak_clean = true;
2408 void Buffer::setUnnamed(bool flag)
2410 pimpl_->unnamed = flag;
2414 bool Buffer::isUnnamed()
2416 return pimpl_->unnamed;
2420 void Buffer::markDirty()
2422 if (pimpl_->lyx_clean) {
2423 pimpl_->lyx_clean = false;
2426 pimpl_->bak_clean = false;
2428 DepClean::iterator it = pimpl_->dep_clean.begin();
2429 DepClean::const_iterator const end = pimpl_->dep_clean.end();
2431 for (; it != end; ++it) {
2437 string const & Buffer::fileName() const
2439 return pimpl_->filename;
2443 string const & Buffer::filePath() const
2445 return pimpl_->filepath;
2449 bool Buffer::isReadonly() const
2451 return pimpl_->read_only;
2455 void Buffer::setParentName(string const & name)
2457 params().parentname = name;
2461 Buffer::inset_iterator::inset_iterator()
2466 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
2473 Buffer::inset_iterator Buffer::inset_iterator_begin()
2475 return inset_iterator(paragraphs().begin(), paragraphs().end());
2479 Buffer::inset_iterator Buffer::inset_iterator_end()
2481 return inset_iterator(paragraphs().end(), paragraphs().end());
2485 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
2487 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2488 return inset_iterator(pars.begin(), pars.end());
2492 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
2494 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2495 return inset_iterator(pars.end(), pars.end());
2499 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
2503 if (it == pit->insetlist.end()) {
2512 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
2514 inset_iterator tmp = *this;
2520 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
2526 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
2532 ParagraphList::iterator Buffer::inset_iterator::getPar() const
2538 lyx::pos_type Buffer::inset_iterator::getPos() const
2544 bool operator==(Buffer::inset_iterator const & iter1,
2545 Buffer::inset_iterator const & iter2)
2547 return iter1.pit == iter2.pit
2548 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
2552 bool operator!=(Buffer::inset_iterator const & iter1,
2553 Buffer::inset_iterator const & iter2)
2555 return !(iter1 == iter2);