]> git.lyx.org Git - lyx.git/blob - src/Buffer.cpp
cosmetics
[lyx.git] / src / Buffer.cpp
1 /**
2  * \file Buffer.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Stefan Schimanski
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Buffer.h"
15
16 #include "Author.h"
17 #include "BiblioInfo.h"
18 #include "BranchList.h"
19 #include "buffer_funcs.h"
20 #include "BufferList.h"
21 #include "BufferParams.h"
22 #include "Bullet.h"
23 #include "Chktex.h"
24 #include "Converter.h"
25 #include "Counters.h"
26 #include "debug.h"
27 #include "DocIterator.h"
28 #include "EmbeddedFiles.h"
29 #include "Encoding.h"
30 #include "ErrorList.h"
31 #include "Exporter.h"
32 #include "Format.h"
33 #include "FuncRequest.h"
34 #include "gettext.h"
35 #include "InsetIterator.h"
36 #include "InsetList.h"
37 #include "Language.h"
38 #include "LaTeXFeatures.h"
39 #include "LaTeX.h"
40 #include "Layout.h"
41 #include "Lexer.h"
42 #include "LyXAction.h"
43 #include "LyX.h"
44 #include "LyXRC.h"
45 #include "LyXVC.h"
46 #include "Messages.h"
47 #include "output_docbook.h"
48 #include "output.h"
49 #include "output_latex.h"
50 #include "output_plaintext.h"
51 #include "paragraph_funcs.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
55 #include "PDFOptions.h"
56 #include "Session.h"
57 #include "sgml.h"
58 #include "TexRow.h"
59 #include "TexStream.h"
60 #include "TextClassList.h"
61 #include "Text.h"
62 #include "TocBackend.h"
63 #include "Undo.h"
64 #include "VCBackend.h"
65 #include "version.h"
66
67 #include "insets/InsetBibitem.h"
68 #include "insets/InsetBibtex.h"
69 #include "insets/InsetInclude.h"
70 #include "insets/InsetText.h"
71
72 #include "mathed/MacroTable.h"
73 #include "mathed/MathMacroTemplate.h"
74 #include "mathed/MathSupport.h"
75
76 #include "frontends/alert.h"
77 #include "frontends/Delegates.h"
78 #include "frontends/WorkAreaManager.h"
79 #include "frontends/FileDialog.h"
80
81 #include "graphics/Previews.h"
82
83 #include "support/types.h"
84 #include "support/lyxalgo.h"
85 #include "support/FileFilterList.h"
86 #include "support/filetools.h"
87 #include "support/Forkedcall.h"
88 #include "support/gzstream.h"
89 #include "support/lstrings.h"
90 #include "support/lyxlib.h"
91 #include "support/os.h"
92 #include "support/Path.h"
93 #include "support/textutils.h"
94 #include "support/convert.h"
95
96 #if !defined (HAVE_FORK)
97 # define fork() -1
98 #endif
99
100 #include <boost/bind.hpp>
101 #include <boost/shared_ptr.hpp>
102
103 #include <algorithm>
104 #include <iomanip>
105 #include <stack>
106 #include <sstream>
107 #include <fstream>
108
109 using std::endl;
110 using std::for_each;
111 using std::make_pair;
112
113 using std::ios;
114 using std::map;
115 using std::ostream;
116 using std::ostringstream;
117 using std::ofstream;
118 using std::ifstream;
119 using std::pair;
120 using std::stack;
121 using std::vector;
122 using std::string;
123 using std::time_t;
124
125 namespace lyx {
126
127 using support::addName;
128 using support::bformat;
129 using support::changeExtension;
130 using support::cmd_ret;
131 using support::createBufferTmpDir;
132 using support::FileName;
133 using support::libFileSearch;
134 using support::latex_path;
135 using support::ltrim;
136 using support::makeAbsPath;
137 using support::makeDisplayPath;
138 using support::makeLatexName;
139 using support::onlyFilename;
140 using support::onlyPath;
141 using support::quoteName;
142 using support::removeAutosaveFile;
143 using support::rename;
144 using support::runCommand;
145 using support::split;
146 using support::subst;
147 using support::tempName;
148 using support::trim;
149 using support::sum;
150 using support::suffixIs;
151
152 namespace Alert = frontend::Alert;
153 namespace os = support::os;
154
155 namespace {
156
157 int const LYX_FORMAT = 299; // Uwe: Hyperlink types
158
159 } // namespace anon
160
161
162 typedef std::map<string, bool> DepClean;
163
164 class Buffer::Impl
165 {
166 public:
167         Impl(Buffer & parent, FileName const & file, bool readonly);
168         
169         BufferParams params;
170         LyXVC lyxvc;
171         string temppath;
172         TexRow texrow;
173
174         /// need to regenerate .tex?
175         DepClean dep_clean;
176
177         /// is save needed?
178         mutable bool lyx_clean;
179
180         /// is autosave needed?
181         mutable bool bak_clean;
182
183         /// is this a unnamed file (New...)?
184         bool unnamed;
185
186         /// buffer is r/o
187         bool read_only;
188
189         /// name of the file the buffer is associated with.
190         FileName filename;
191
192         /** Set to true only when the file is fully loaded.
193          *  Used to prevent the premature generation of previews
194          *  and by the citation inset.
195          */
196         bool file_fully_loaded;
197
198         /// our Text that should be wrapped in an InsetText
199         InsetText inset;
200
201         ///
202         TocBackend toc_backend;
203
204         /// macro table
205         typedef std::map<unsigned int, MacroData, std::greater<int> > PositionToMacroMap;
206         typedef std::map<docstring, PositionToMacroMap> NameToPositionMacroMap;
207         NameToPositionMacroMap macros;
208
209         /// Container for all sort of Buffer dependant errors.
210         map<string, ErrorList> errorLists;
211
212         /// all embedded files of this buffer
213         EmbeddedFiles embedded_files;
214
215         /// timestamp and checksum used to test if the file has been externally
216         /// modified. (Used to properly enable 'File->Revert to saved', bug 4114).
217         time_t timestamp_;
218         unsigned long checksum_;
219
220         ///
221         frontend::WorkAreaManager * wa_;
222
223         ///
224         Undo undo_;
225 };
226
227
228 Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
229         : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
230           filename(file), file_fully_loaded(false), inset(params),
231           toc_backend(&parent), embedded_files(&parent), timestamp_(0),
232           checksum_(0), wa_(0), undo_(parent)
233 {
234         inset.setAutoBreakRows(true);
235         lyxvc.setBuffer(&parent);
236         temppath = createBufferTmpDir();
237         params.filepath = onlyPath(file.absFilename());
238         // FIXME: And now do something if temppath == string(), because we
239         // assume from now on that temppath points to a valid temp dir.
240         // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
241
242         if (use_gui)
243                 wa_ = new frontend::WorkAreaManager;
244 }
245
246
247 Buffer::Buffer(string const & file, bool readonly)
248         : pimpl_(new Impl(*this, FileName(file), readonly)), gui_(0)
249 {
250         LYXERR(Debug::INFO) << "Buffer::Buffer()" << endl;
251 }
252
253
254 Buffer::~Buffer()
255 {
256         LYXERR(Debug::INFO) << "Buffer::~Buffer()" << endl;
257         // here the buffer should take care that it is
258         // saved properly, before it goes into the void.
259
260         Buffer * master = masterBuffer();
261         if (master != this && use_gui)
262                 // We are closing buf which was a child document so we
263                 // must update the labels and section numbering of its master
264                 // Buffer.
265                 updateLabels(*master);
266
267         if (!temppath().empty() && !FileName(temppath()).destroyDirectory()) {
268                 Alert::warning(_("Could not remove temporary directory"),
269                         bformat(_("Could not remove the temporary directory %1$s"),
270                         from_utf8(temppath())));
271         }
272
273         // Remove any previewed LaTeX snippets associated with this buffer.
274         graphics::Previews::get().removeLoader(*this);
275
276         if (pimpl_->wa_) {
277                 pimpl_->wa_->closeAll();
278                 delete pimpl_->wa_;
279         }
280         delete pimpl_;
281 }
282
283
284 void Buffer::changed() const
285 {
286         if (pimpl_->wa_)
287                 pimpl_->wa_->redrawAll();
288 }
289
290
291 frontend::WorkAreaManager & Buffer::workAreaManager() const
292 {
293         BOOST_ASSERT(pimpl_->wa_);
294         return *pimpl_->wa_;
295 }
296
297
298 Text & Buffer::text() const
299 {
300         return const_cast<Text &>(pimpl_->inset.text_);
301 }
302
303
304 Inset & Buffer::inset() const
305 {
306         return const_cast<InsetText &>(pimpl_->inset);
307 }
308
309
310 BufferParams & Buffer::params()
311 {
312         return pimpl_->params;
313 }
314
315
316 BufferParams const & Buffer::params() const
317 {
318         return pimpl_->params;
319 }
320
321
322 ParagraphList & Buffer::paragraphs()
323 {
324         return text().paragraphs();
325 }
326
327
328 ParagraphList const & Buffer::paragraphs() const
329 {
330         return text().paragraphs();
331 }
332
333
334 LyXVC & Buffer::lyxvc()
335 {
336         return pimpl_->lyxvc;
337 }
338
339
340 LyXVC const & Buffer::lyxvc() const
341 {
342         return pimpl_->lyxvc;
343 }
344
345
346 string const & Buffer::temppath() const
347 {
348         return pimpl_->temppath;
349 }
350
351
352 TexRow & Buffer::texrow()
353 {
354         return pimpl_->texrow;
355 }
356
357
358 TexRow const & Buffer::texrow() const
359 {
360         return pimpl_->texrow;
361 }
362
363
364 TocBackend & Buffer::tocBackend()
365 {
366         return pimpl_->toc_backend;
367 }
368
369
370 TocBackend const & Buffer::tocBackend() const
371 {
372         return pimpl_->toc_backend;
373 }
374
375
376 EmbeddedFiles & Buffer::embeddedFiles()
377 {
378         return pimpl_->embedded_files;
379 }
380
381
382 EmbeddedFiles const & Buffer::embeddedFiles() const
383 {
384         return pimpl_->embedded_files;
385 }
386
387
388 Undo & Buffer::undo()
389 {
390         return pimpl_->undo_;
391 }
392
393
394 string Buffer::latexName(bool const no_path) const
395 {
396         string const name = changeExtension(makeLatexName(absFileName()), ".tex");
397         return no_path ? onlyFilename(name) : name;
398 }
399
400
401 string Buffer::logName(LogType * type) const
402 {
403         string const filename = latexName(false);
404
405         if (filename.empty()) {
406                 if (type)
407                         *type = latexlog;
408                 return string();
409         }
410
411         string const path = temppath();
412
413         FileName const fname(addName(temppath(),
414                                      onlyFilename(changeExtension(filename,
415                                                                   ".log"))));
416         FileName const bname(
417                 addName(path, onlyFilename(
418                         changeExtension(filename,
419                                         formats.extension("literate") + ".out"))));
420
421         // If no Latex log or Build log is newer, show Build log
422
423         if (bname.exists() &&
424             (!fname.exists() || fname.lastModified() < bname.lastModified())) {
425                 LYXERR(Debug::FILES) << "Log name calculated as: " << bname << endl;
426                 if (type)
427                         *type = buildlog;
428                 return bname.absFilename();
429         }
430         LYXERR(Debug::FILES) << "Log name calculated as: " << fname << endl;
431         if (type)
432                         *type = latexlog;
433         return fname.absFilename();
434 }
435
436
437 void Buffer::setReadonly(bool const flag)
438 {
439         if (pimpl_->read_only != flag) {
440                 pimpl_->read_only = flag;
441                 setReadOnly(flag);
442         }
443 }
444
445
446 void Buffer::setFileName(string const & newfile)
447 {
448         pimpl_->filename = makeAbsPath(newfile);
449         params().filepath = onlyPath(pimpl_->filename.absFilename());
450         setReadonly(pimpl_->filename.isReadOnly());
451         updateTitles();
452 }
453
454
455 int Buffer::readHeader(Lexer & lex)
456 {
457         int unknown_tokens = 0;
458         int line = -1;
459         int begin_header_line = -1;
460
461         // Initialize parameters that may be/go lacking in header:
462         params().branchlist().clear();
463         params().preamble.erase();
464         params().options.erase();
465         params().float_placement.erase();
466         params().paperwidth.erase();
467         params().paperheight.erase();
468         params().leftmargin.erase();
469         params().rightmargin.erase();
470         params().topmargin.erase();
471         params().bottommargin.erase();
472         params().headheight.erase();
473         params().headsep.erase();
474         params().footskip.erase();
475         params().listings_params.clear();
476         params().clearLayoutModules();
477         params().pdfoptions().clear();
478         
479         for (int i = 0; i < 4; ++i) {
480                 params().user_defined_bullet(i) = ITEMIZE_DEFAULTS[i];
481                 params().temp_bullet(i) = ITEMIZE_DEFAULTS[i];
482         }
483
484         ErrorList & errorList = pimpl_->errorLists["Parse"];
485
486         while (lex.isOK()) {
487                 lex.next();
488                 string const token = lex.getString();
489
490                 if (token.empty())
491                         continue;
492
493                 if (token == "\\end_header")
494                         break;
495
496                 ++line;
497                 if (token == "\\begin_header") {
498                         begin_header_line = line;
499                         continue;
500                 }
501
502                 LYXERR(Debug::PARSER) << "Handling document header token: `"
503                                       << token << '\'' << endl;
504
505                 string unknown = params().readToken(lex, token);
506                 if (!unknown.empty()) {
507                         if (unknown[0] != '\\' && token == "\\textclass") {
508                                 Alert::warning(_("Unknown document class"),
509                        bformat(_("Using the default document class, because the "
510                                               "class %1$s is unknown."), from_utf8(unknown)));
511                         } else {
512                                 ++unknown_tokens;
513                                 docstring const s = bformat(_("Unknown token: "
514                                                                         "%1$s %2$s\n"),
515                                                          from_utf8(token),
516                                                          lex.getDocString());
517                                 errorList.push_back(ErrorItem(_("Document header error"),
518                                         s, -1, 0, 0));
519                         }
520                 }
521         }
522         if (begin_header_line) {
523                 docstring const s = _("\\begin_header is missing");
524                 errorList.push_back(ErrorItem(_("Document header error"),
525                         s, -1, 0, 0));
526         }
527
528         return unknown_tokens;
529 }
530
531
532 // Uwe C. Schroeder
533 // changed to be public and have one parameter
534 // Returns false if "\end_document" is not read (Asger)
535 bool Buffer::readDocument(Lexer & lex)
536 {
537         ErrorList & errorList = pimpl_->errorLists["Parse"];
538         errorList.clear();
539
540         lex.next();
541         string const token = lex.getString();
542         if (token != "\\begin_document") {
543                 docstring const s = _("\\begin_document is missing");
544                 errorList.push_back(ErrorItem(_("Document header error"),
545                         s, -1, 0, 0));
546         }
547
548         // we are reading in a brand new document
549         BOOST_ASSERT(paragraphs().empty());
550
551         readHeader(lex);
552         TextClass const & baseClass = textclasslist[params().getBaseClass()];
553         if (!baseClass.load(filePath())) {
554                 string theclass = baseClass.name();
555                 Alert::error(_("Can't load document class"), bformat(
556                         _("Using the default document class, because the "
557                                      "class %1$s could not be loaded."), from_utf8(theclass)));
558                 params().setBaseClass(defaultTextclass());
559         }
560
561         if (params().outputChanges) {
562                 bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
563                 bool xcolorsoul = LaTeXFeatures::isAvailable("soul") &&
564                                   LaTeXFeatures::isAvailable("xcolor");
565
566                 if (!dvipost && !xcolorsoul) {
567                         Alert::warning(_("Changes not shown in LaTeX output"),
568                                        _("Changes will not be highlighted in LaTeX output, "
569                                          "because neither dvipost nor xcolor/soul are installed.\n"
570                                          "Please install these packages or redefine "
571                                          "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
572                 } else if (!xcolorsoul) {
573                         Alert::warning(_("Changes not shown in LaTeX output"),
574                                        _("Changes will not be highlighted in LaTeX output "
575                                          "when using pdflatex, because xcolor and soul are not installed.\n"
576                                          "Please install both packages or redefine "
577                                          "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
578                 }
579         }
580
581         // read main text
582         bool const res = text().read(*this, lex, errorList);
583         for_each(text().paragraphs().begin(),
584                  text().paragraphs().end(),
585                  bind(&Paragraph::setInsetOwner, _1, &inset()));
586
587         return res;
588 }
589
590
591 // needed to insert the selection
592 void Buffer::insertStringAsLines(ParagraphList & pars,
593         pit_type & pit, pos_type & pos,
594         Font const & fn, docstring const & str, bool autobreakrows)
595 {
596         Font font = fn;
597
598         // insert the string, don't insert doublespace
599         bool space_inserted = true;
600         for (docstring::const_iterator cit = str.begin();
601             cit != str.end(); ++cit) {
602                 Paragraph & par = pars[pit];
603                 if (*cit == '\n') {
604                         if (autobreakrows && (!par.empty() || par.allowEmpty())) {
605                                 breakParagraph(params(), pars, pit, pos,
606                                                par.layout()->isEnvironment());
607                                 ++pit;
608                                 pos = 0;
609                                 space_inserted = true;
610                         } else {
611                                 continue;
612                         }
613                         // do not insert consecutive spaces if !free_spacing
614                 } else if ((*cit == ' ' || *cit == '\t') &&
615                            space_inserted && !par.isFreeSpacing()) {
616                         continue;
617                 } else if (*cit == '\t') {
618                         if (!par.isFreeSpacing()) {
619                                 // tabs are like spaces here
620                                 par.insertChar(pos, ' ', font, params().trackChanges);
621                                 ++pos;
622                                 space_inserted = true;
623                         } else {
624                                 const pos_type n = 8 - pos % 8;
625                                 for (pos_type i = 0; i < n; ++i) {
626                                         par.insertChar(pos, ' ', font, params().trackChanges);
627                                         ++pos;
628                                 }
629                                 space_inserted = true;
630                         }
631                 } else if (!isPrintable(*cit)) {
632                         // Ignore unprintables
633                         continue;
634                 } else {
635                         // just insert the character
636                         par.insertChar(pos, *cit, font, params().trackChanges);
637                         ++pos;
638                         space_inserted = (*cit == ' ');
639                 }
640
641         }
642 }
643
644
645 bool Buffer::readString(std::string const & s)
646 {
647         params().compressed = false;
648
649         // remove dummy empty par
650         paragraphs().clear();
651         Lexer lex(0, 0);
652         std::istringstream is(s);
653         lex.setStream(is);
654         FileName const name(tempName());
655         switch (readFile(lex, name, true)) {
656         case failure:
657                 return false;
658         case wrongversion: {
659                 // We need to call lyx2lyx, so write the input to a file
660                 std::ofstream os(name.toFilesystemEncoding().c_str());
661                 os << s;
662                 os.close();
663                 return readFile(name);
664         }
665         case success:
666                 break;
667         }
668
669         return true;
670 }
671
672
673 bool Buffer::readFile(FileName const & filename)
674 {
675         FileName fname(filename);
676         // Check if the file is compressed.
677         string format = filename.guessFormatFromContents();
678         if (format == "zip") {
679                 // decompress to a temp directory
680                 LYXERR(Debug::FILES) << filename << " is in zip format. Unzip to " << temppath() << endl;
681                 ::unzipToDir(filename.toFilesystemEncoding(), temppath());
682                 //
683                 FileName lyxfile(addName(temppath(), "content.lyx"));
684                 // if both manifest.txt and file.lyx exist, this is am embedded file
685                 if (lyxfile.exists()) {
686                         params().embedded = true;
687                         fname = lyxfile;
688                 }
689         }
690         // The embedded lyx file can also be compressed, for backward compatibility
691         format = fname.guessFormatFromContents();
692         if (format == "gzip" || format == "zip" || format == "compress")
693                 params().compressed = true;
694
695         // remove dummy empty par
696         paragraphs().clear();
697         Lexer lex(0, 0);
698         lex.setFile(fname);
699         if (readFile(lex, fname) != success)
700                 return false;
701
702         return true;
703 }
704
705
706 bool Buffer::isFullyLoaded() const
707 {
708         return pimpl_->file_fully_loaded;
709 }
710
711
712 void Buffer::setFullyLoaded(bool value)
713 {
714         pimpl_->file_fully_loaded = value;
715 }
716
717
718 Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
719                 bool fromstring)
720 {
721         BOOST_ASSERT(!filename.empty());
722
723         if (!lex.isOK()) {
724                 Alert::error(_("Document could not be read"),
725                              bformat(_("%1$s could not be read."), from_utf8(filename.absFilename())));
726                 return failure;
727         }
728
729         lex.next();
730         string const token = lex.getString();
731
732         if (!lex) {
733                 Alert::error(_("Document could not be read"),
734                              bformat(_("%1$s could not be read."), from_utf8(filename.absFilename())));
735                 return failure;
736         }
737
738         // the first token _must_ be...
739         if (token != "\\lyxformat") {
740                 lyxerr << "Token: " << token << endl;
741
742                 Alert::error(_("Document format failure"),
743                              bformat(_("%1$s is not a LyX document."),
744                                        from_utf8(filename.absFilename())));
745                 return failure;
746         }
747
748         lex.next();
749         string tmp_format = lex.getString();
750         //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
751         // if present remove ".," from string.
752         string::size_type dot = tmp_format.find_first_of(".,");
753         //lyxerr << "           dot found at " << dot << endl;
754         if (dot != string::npos)
755                         tmp_format.erase(dot, 1);
756         int const file_format = convert<int>(tmp_format);
757         //lyxerr << "format: " << file_format << endl;
758
759         // save timestamp and checksum of the original disk file, making sure
760         // to not overwrite them with those of the file created in the tempdir
761         // when it has to be converted to the current format.
762         if (!pimpl_->checksum_) {
763                 // Save the timestamp and checksum of disk file. If filename is an
764                 // emergency file, save the timestamp and sum of the original lyx file
765                 // because isExternallyModified will check for this file. (BUG4193)
766                 string diskfile = filename.toFilesystemEncoding();
767                 if (suffixIs(diskfile, ".emergency"))
768                         diskfile = diskfile.substr(0, diskfile.size() - 10);
769                 saveCheckSum(FileName(diskfile));
770         }
771
772         if (file_format != LYX_FORMAT) {
773
774                 if (fromstring)
775                         // lyx2lyx would fail
776                         return wrongversion;
777
778                 FileName const tmpfile(tempName());
779                 if (tmpfile.empty()) {
780                         Alert::error(_("Conversion failed"),
781                                      bformat(_("%1$s is from a different"
782                                               " version of LyX, but a temporary"
783                                               " file for converting it could"
784                                                             " not be created."),
785                                               from_utf8(filename.absFilename())));
786                         return failure;
787                 }
788                 FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
789                 if (lyx2lyx.empty()) {
790                         Alert::error(_("Conversion script not found"),
791                                      bformat(_("%1$s is from a different"
792                                                " version of LyX, but the"
793                                                " conversion script lyx2lyx"
794                                                             " could not be found."),
795                                                from_utf8(filename.absFilename())));
796                         return failure;
797                 }
798                 ostringstream command;
799                 command << os::python()
800                         << ' ' << quoteName(lyx2lyx.toFilesystemEncoding())
801                         << " -t " << convert<string>(LYX_FORMAT)
802                         << " -o " << quoteName(tmpfile.toFilesystemEncoding())
803                         << ' ' << quoteName(filename.toFilesystemEncoding());
804                 string const command_str = command.str();
805
806                 LYXERR(Debug::INFO) << "Running '"
807                                     << command_str << '\''
808                                     << endl;
809
810                 cmd_ret const ret = runCommand(command_str);
811                 if (ret.first != 0) {
812                         Alert::error(_("Conversion script failed"),
813                                      bformat(_("%1$s is from a different version"
814                                               " of LyX, but the lyx2lyx script"
815                                                             " failed to convert it."),
816                                               from_utf8(filename.absFilename())));
817                         return failure;
818                 } else {
819                         bool const ret = readFile(tmpfile);
820                         // Do stuff with tmpfile name and buffer name here.
821                         return ret ? success : failure;
822                 }
823
824         }
825
826         if (readDocument(lex)) {
827                 Alert::error(_("Document format failure"),
828                              bformat(_("%1$s ended unexpectedly, which means"
829                                                     " that it is probably corrupted."),
830                                        from_utf8(filename.absFilename())));
831         }
832
833         pimpl_->file_fully_loaded = true;
834         return success;
835 }
836
837
838 // Should probably be moved to somewhere else: BufferView? LyXView?
839 bool Buffer::save() const
840 {
841         // We don't need autosaves in the immediate future. (Asger)
842         resetAutosaveTimers();
843
844         string const encodedFilename = pimpl_->filename.toFilesystemEncoding();
845
846         FileName backupName;
847         bool madeBackup = false;
848
849         // make a backup if the file already exists
850         if (lyxrc.make_backup && fileName().exists()) {
851                 backupName = FileName(absFileName() + '~');
852                 if (!lyxrc.backupdir_path.empty()) {
853                         string const mangledName =
854                                 subst(subst(os::internal_path(
855                                 backupName.absFilename()), '/', '!'), ':', '!');
856                         backupName = FileName(addName(lyxrc.backupdir_path,
857                                                       mangledName));
858                 }
859                 if (fileName().copyTo(backupName, false)) {
860                         madeBackup = true;
861                 } else {
862                         Alert::error(_("Backup failure"),
863                                      bformat(_("Cannot create backup file %1$s.\n"
864                                                "Please check whether the directory exists and is writeable."),
865                                              from_utf8(backupName.absFilename())));
866                         //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
867                 }
868         }
869
870         // ask if the disk file has been externally modified (use checksum method)
871         if (fileName().exists() && isExternallyModified(checksum_method)) {
872                 docstring const file = makeDisplayPath(absFileName(), 20);
873                 docstring text = bformat(_("Document %1$s has been externally modified. Are you sure "
874                                                              "you want to overwrite this file?"), file);
875                 int const ret = Alert::prompt(_("Overwrite modified file?"),
876                         text, 1, 1, _("&Overwrite"), _("&Cancel"));
877                 if (ret == 1)
878                         return false;
879         }
880
881         if (writeFile(pimpl_->filename)) {
882                 markClean();
883                 removeAutosaveFile(absFileName());
884                 saveCheckSum(pimpl_->filename);
885                 return true;
886         } else {
887                 // Saving failed, so backup is not backup
888                 if (madeBackup)
889                         rename(backupName, pimpl_->filename);
890                 return false;
891         }
892 }
893
894
895 bool Buffer::writeFile(FileName const & fname) const
896 {
897         if (pimpl_->read_only && fname == pimpl_->filename)
898                 return false;
899
900         bool retval = false;
901
902         FileName content;
903         if (params().embedded)
904                 // first write the .lyx file to the temporary directory
905                 content = FileName(addName(temppath(), "content.lyx"));
906         else
907                 content = fname;
908         
909         if (params().compressed) {
910                 gz::ogzstream ofs(content.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
911                 if (!ofs)
912                         return false;
913
914                 retval = write(ofs);
915         } else {
916                 ofstream ofs(content.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
917                 if (!ofs)
918                         return false;
919
920                 retval = write(ofs);
921         }
922
923         if (retval && params().embedded) {
924                 // write file.lyx and all the embedded files to the zip file fname
925                 // if embedding is enabled
926                 return pimpl_->embedded_files.writeFile(fname);
927         }
928         return retval;
929 }
930
931
932 bool Buffer::write(ostream & ofs) const
933 {
934 #ifdef HAVE_LOCALE
935         // Use the standard "C" locale for file output.
936         ofs.imbue(std::locale::classic());
937 #endif
938
939         // The top of the file should not be written by params().
940
941         // write out a comment in the top of the file
942         ofs << "#LyX " << lyx_version
943             << " created this file. For more info see http://www.lyx.org/\n"
944             << "\\lyxformat " << LYX_FORMAT << "\n"
945             << "\\begin_document\n";
946
947
948         /// For each author, set 'used' to true if there is a change
949         /// by this author in the document; otherwise set it to 'false'.
950         AuthorList::Authors::const_iterator a_it = params().authors().begin();
951         AuthorList::Authors::const_iterator a_end = params().authors().end();
952         for (; a_it != a_end; ++a_it)
953                 a_it->second.setUsed(false);
954
955         ParIterator const end = par_iterator_end();
956         ParIterator it = par_iterator_begin();
957         for ( ; it != end; ++it)
958                 it->checkAuthors(params().authors());
959
960         // now write out the buffer parameters.
961         ofs << "\\begin_header\n";
962         params().writeFile(ofs);
963         ofs << "\\end_header\n";
964
965         // write the text
966         ofs << "\n\\begin_body\n";
967         text().write(*this, ofs);
968         ofs << "\n\\end_body\n";
969
970         // Write marker that shows file is complete
971         ofs << "\\end_document" << endl;
972
973         // Shouldn't really be needed....
974         //ofs.close();
975
976         // how to check if close went ok?
977         // Following is an attempt... (BE 20001011)
978
979         // good() returns false if any error occured, including some
980         //        formatting error.
981         // bad()  returns true if something bad happened in the buffer,
982         //        which should include file system full errors.
983
984         bool status = true;
985         if (!ofs) {
986                 status = false;
987                 lyxerr << "File was not closed properly." << endl;
988         }
989
990         return status;
991 }
992
993
994 bool Buffer::makeLaTeXFile(FileName const & fname,
995                            string const & original_path,
996                            OutputParams const & runparams,
997                            bool output_preamble, bool output_body)
998 {
999         string const encoding = runparams.encoding->iconvName();
1000         LYXERR(Debug::LATEX) << "makeLaTeXFile encoding: "
1001                 << encoding << "..." << endl;
1002
1003         odocfstream ofs(encoding);
1004         if (!openFileWrite(ofs, fname))
1005                 return false;
1006
1007         //TexStream ts(ofs.rdbuf(), &texrow());
1008
1009         bool failed_export = false;
1010         try {
1011                 texrow().reset();
1012                 writeLaTeXSource(ofs, original_path,
1013                       runparams, output_preamble, output_body);
1014         }
1015         catch (iconv_codecvt_facet_exception & e) {
1016                 lyxerr << "Caught iconv exception: " << e.what() << endl;
1017                 failed_export = true;
1018         }
1019         catch (std::exception const & e) {
1020                 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
1021                 failed_export = true;
1022         }
1023         catch (...) {
1024                 lyxerr << "Caught some really weird exception..." << endl;
1025                 LyX::cref().emergencyCleanup();
1026                 abort();
1027         }
1028
1029         ofs.close();
1030         if (ofs.fail()) {
1031                 failed_export = true;
1032                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1033         }
1034
1035         if (failed_export) {
1036                 Alert::error(_("Encoding error"),
1037                         _("Some characters of your document are probably not "
1038                         "representable in the chosen encoding.\n"
1039                         "Changing the document encoding to utf8 could help."));
1040                 return false;
1041         }
1042         return true;
1043 }
1044
1045
1046 void Buffer::writeLaTeXSource(odocstream & os,
1047                            string const & original_path,
1048                            OutputParams const & runparams_in,
1049                            bool const output_preamble, bool const output_body)
1050 {
1051         OutputParams runparams = runparams_in;
1052
1053         // validate the buffer.
1054         LYXERR(Debug::LATEX) << "  Validating buffer..." << endl;
1055         LaTeXFeatures features(*this, params(), runparams);
1056         validate(features);
1057         LYXERR(Debug::LATEX) << "  Buffer validation done." << endl;
1058
1059         // The starting paragraph of the coming rows is the
1060         // first paragraph of the document. (Asger)
1061         if (output_preamble && runparams.nice) {
1062                 os << "%% LyX " << lyx_version << " created this file.  "
1063                         "For more info, see http://www.lyx.org/.\n"
1064                         "%% Do not edit unless you really know what "
1065                         "you are doing.\n";
1066                 texrow().newline();
1067                 texrow().newline();
1068         }
1069         LYXERR(Debug::INFO) << "lyx document header finished" << endl;
1070         // There are a few differences between nice LaTeX and usual files:
1071         // usual is \batchmode and has a
1072         // special input@path to allow the including of figures
1073         // with either \input or \includegraphics (what figinsets do).
1074         // input@path is set when the actual parameter
1075         // original_path is set. This is done for usual tex-file, but not
1076         // for nice-latex-file. (Matthias 250696)
1077         // Note that input@path is only needed for something the user does
1078         // in the preamble, included .tex files or ERT, files included by
1079         // LyX work without it.
1080         if (output_preamble) {
1081                 if (!runparams.nice) {
1082                         // code for usual, NOT nice-latex-file
1083                         os << "\\batchmode\n"; // changed
1084                         // from \nonstopmode
1085                         texrow().newline();
1086                 }
1087                 if (!original_path.empty()) {
1088                         // FIXME UNICODE
1089                         // We don't know the encoding of inputpath
1090                         docstring const inputpath = from_utf8(latex_path(original_path));
1091                         os << "\\makeatletter\n"
1092                            << "\\def\\input@path{{"
1093                            << inputpath << "/}}\n"
1094                            << "\\makeatother\n";
1095                         texrow().newline();
1096                         texrow().newline();
1097                         texrow().newline();
1098                 }
1099
1100                 // Write the preamble
1101                 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1102
1103                 if (!output_body)
1104                         return;
1105
1106                 // make the body.
1107                 os << "\\begin{document}\n";
1108                 texrow().newline();
1109         } // output_preamble
1110
1111         texrow().start(paragraphs().begin()->id(), 0);
1112         
1113         LYXERR(Debug::INFO) << "preamble finished, now the body." << endl;
1114
1115         if (!lyxrc.language_auto_begin &&
1116             !params().language->babel().empty()) {
1117                 // FIXME UNICODE
1118                 os << from_utf8(subst(lyxrc.language_command_begin,
1119                                            "$$lang",
1120                                            params().language->babel()))
1121                    << '\n';
1122                 texrow().newline();
1123         }
1124
1125         Encoding const & encoding = params().encoding();
1126         if (encoding.package() == Encoding::CJK) {
1127                 // Open a CJK environment, since in contrast to the encodings
1128                 // handled by inputenc the document encoding is not set in
1129                 // the preamble if it is handled by CJK.sty.
1130                 os << "\\begin{CJK}{" << from_ascii(encoding.latexName())
1131                    << "}{}\n";
1132                 texrow().newline();
1133         }
1134
1135         // if we are doing a real file with body, even if this is the
1136         // child of some other buffer, let's cut the link here.
1137         // This happens for example if only a child document is printed.
1138         string save_parentname;
1139         if (output_preamble) {
1140                 save_parentname = params().parentname;
1141                 params().parentname.erase();
1142         }
1143
1144         loadChildDocuments();
1145
1146         // the real stuff
1147         latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1148
1149         // Restore the parenthood if needed
1150         if (output_preamble)
1151                 params().parentname = save_parentname;
1152
1153         // add this just in case after all the paragraphs
1154         os << endl;
1155         texrow().newline();
1156
1157         if (encoding.package() == Encoding::CJK) {
1158                 // Close the open CJK environment.
1159                 // latexParagraphs will have opened one even if the last text
1160                 // was not CJK.
1161                 os << "\\end{CJK}\n";
1162                 texrow().newline();
1163         }
1164
1165         if (!lyxrc.language_auto_end &&
1166             !params().language->babel().empty()) {
1167                 os << from_utf8(subst(lyxrc.language_command_end,
1168                                            "$$lang",
1169                                            params().language->babel()))
1170                    << '\n';
1171                 texrow().newline();
1172         }
1173
1174         if (output_preamble) {
1175                 os << "\\end{document}\n";
1176                 texrow().newline();
1177
1178                 LYXERR(Debug::LATEX) << "makeLaTeXFile...done" << endl;
1179         } else {
1180                 LYXERR(Debug::LATEX) << "LaTeXFile for inclusion made."
1181                                      << endl;
1182         }
1183         runparams_in.encoding = runparams.encoding;
1184
1185         // Just to be sure. (Asger)
1186         texrow().newline();
1187
1188         LYXERR(Debug::INFO) << "Finished making LaTeX file." << endl;
1189         LYXERR(Debug::INFO) << "Row count was " << texrow().rows() - 1
1190                             << '.' << endl;
1191 }
1192
1193
1194 bool Buffer::isLatex() const
1195 {
1196         return params().getTextClass().outputType() == LATEX;
1197 }
1198
1199
1200 bool Buffer::isLiterate() const
1201 {
1202         return params().getTextClass().outputType() == LITERATE;
1203 }
1204
1205
1206 bool Buffer::isDocBook() const
1207 {
1208         return params().getTextClass().outputType() == DOCBOOK;
1209 }
1210
1211
1212 void Buffer::makeDocBookFile(FileName const & fname,
1213                               OutputParams const & runparams,
1214                               bool const body_only)
1215 {
1216         LYXERR(Debug::LATEX) << "makeDocBookFile..." << endl;
1217
1218         //ofstream ofs;
1219         odocfstream ofs;
1220         if (!openFileWrite(ofs, fname))
1221                 return;
1222
1223         writeDocBookSource(ofs, fname.absFilename(), runparams, body_only);
1224
1225         ofs.close();
1226         if (ofs.fail())
1227                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1228 }
1229
1230
1231 void Buffer::writeDocBookSource(odocstream & os, string const & fname,
1232                              OutputParams const & runparams,
1233                              bool const only_body)
1234 {
1235         LaTeXFeatures features(*this, params(), runparams);
1236         validate(features);
1237
1238         texrow().reset();
1239
1240         TextClass const & tclass = params().getTextClass();
1241         string const top_element = tclass.latexname();
1242
1243         if (!only_body) {
1244                 if (runparams.flavor == OutputParams::XML)
1245                         os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1246
1247                 // FIXME UNICODE
1248                 os << "<!DOCTYPE " << from_ascii(top_element) << ' ';
1249
1250                 // FIXME UNICODE
1251                 if (! tclass.class_header().empty())
1252                         os << from_ascii(tclass.class_header());
1253                 else if (runparams.flavor == OutputParams::XML)
1254                         os << "PUBLIC \"-//OASIS//DTD DocBook XML//EN\" "
1255                             << "\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\"";
1256                 else
1257                         os << " PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\"";
1258
1259                 docstring preamble = from_utf8(params().preamble);
1260                 if (runparams.flavor != OutputParams::XML ) {
1261                         preamble += "<!ENTITY % output.print.png \"IGNORE\">\n";
1262                         preamble += "<!ENTITY % output.print.pdf \"IGNORE\">\n";
1263                         preamble += "<!ENTITY % output.print.eps \"IGNORE\">\n";
1264                         preamble += "<!ENTITY % output.print.bmp \"IGNORE\">\n";
1265                 }
1266
1267                 string const name = runparams.nice
1268                         ? changeExtension(absFileName(), ".sgml") : fname;
1269                 preamble += features.getIncludedFiles(name);
1270                 preamble += features.getLyXSGMLEntities();
1271
1272                 if (!preamble.empty()) {
1273                         os << "\n [ " << preamble << " ]";
1274                 }
1275                 os << ">\n\n";
1276         }
1277
1278         string top = top_element;
1279         top += " lang=\"";
1280         if (runparams.flavor == OutputParams::XML)
1281                 top += params().language->code();
1282         else
1283                 top += params().language->code().substr(0,2);
1284         top += '"';
1285
1286         if (!params().options.empty()) {
1287                 top += ' ';
1288                 top += params().options;
1289         }
1290
1291         os << "<!-- " << ((runparams.flavor == OutputParams::XML)? "XML" : "SGML")
1292             << " file was created by LyX " << lyx_version
1293             << "\n  See http://www.lyx.org/ for more information -->\n";
1294
1295         params().getTextClass().counters().reset();
1296
1297         loadChildDocuments();
1298
1299         sgml::openTag(os, top);
1300         os << '\n';
1301         docbookParagraphs(paragraphs(), *this, os, runparams);
1302         sgml::closeTag(os, top_element);
1303 }
1304
1305
1306 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1307 // Other flags: -wall -v0 -x
1308 int Buffer::runChktex()
1309 {
1310         setBusy(true);
1311
1312         // get LaTeX-Filename
1313         FileName const path(temppath());
1314         string const name = addName(path.absFilename(), latexName());
1315         string const org_path = filePath();
1316
1317         support::PathChanger p(path); // path to LaTeX file
1318         message(_("Running chktex..."));
1319
1320         // Generate the LaTeX file if neccessary
1321         OutputParams runparams(&params().encoding());
1322         runparams.flavor = OutputParams::LATEX;
1323         runparams.nice = false;
1324         makeLaTeXFile(FileName(name), org_path, runparams);
1325
1326         TeXErrors terr;
1327         Chktex chktex(lyxrc.chktex_command, onlyFilename(name), filePath());
1328         int const res = chktex.run(terr); // run chktex
1329
1330         if (res == -1) {
1331                 Alert::error(_("chktex failure"),
1332                              _("Could not run chktex successfully."));
1333         } else if (res > 0) {
1334                 ErrorList & errlist = pimpl_->errorLists["ChkTeX"];
1335                 errlist.clear();
1336                 bufferErrors(terr, errlist);
1337         }
1338
1339         setBusy(false);
1340
1341         errors("ChkTeX");
1342
1343         return res;
1344 }
1345
1346
1347 void Buffer::validate(LaTeXFeatures & features) const
1348 {
1349         TextClass const & tclass = params().getTextClass();
1350
1351         if (params().outputChanges) {
1352                 bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
1353                 bool xcolorsoul = LaTeXFeatures::isAvailable("soul") &&
1354                                   LaTeXFeatures::isAvailable("xcolor");
1355
1356                 if (features.runparams().flavor == OutputParams::LATEX) {
1357                         if (dvipost) {
1358                                 features.require("ct-dvipost");
1359                                 features.require("dvipost");
1360                         } else if (xcolorsoul) {
1361                                 features.require("ct-xcolor-soul");
1362                                 features.require("soul");
1363                                 features.require("xcolor");
1364                         } else {
1365                                 features.require("ct-none");
1366                         }
1367                 } else if (features.runparams().flavor == OutputParams::PDFLATEX ) {
1368                         if (xcolorsoul) {
1369                                 features.require("ct-xcolor-soul");
1370                                 features.require("soul");
1371                                 features.require("xcolor");
1372                                 features.require("pdfcolmk"); // improves color handling in PDF output
1373                         } else {
1374                                 features.require("ct-none");
1375                         }
1376                 }
1377         }
1378
1379         // AMS Style is at document level
1380         if (params().use_amsmath == BufferParams::package_on
1381             || tclass.provides("amsmath"))
1382                 features.require("amsmath");
1383         if (params().use_esint == BufferParams::package_on)
1384                 features.require("esint");
1385
1386         loadChildDocuments();
1387
1388         for_each(paragraphs().begin(), paragraphs().end(),
1389                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1390
1391         // the bullet shapes are buffer level not paragraph level
1392         // so they are tested here
1393         for (int i = 0; i < 4; ++i) {
1394                 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1395                         int const font = params().user_defined_bullet(i).getFont();
1396                         if (font == 0) {
1397                                 int const c = params()
1398                                         .user_defined_bullet(i)
1399                                         .getCharacter();
1400                                 if (c == 16
1401                                    || c == 17
1402                                    || c == 25
1403                                    || c == 26
1404                                    || c == 31) {
1405                                         features.require("latexsym");
1406                                 }
1407                         } else if (font == 1) {
1408                                 features.require("amssymb");
1409                         } else if ((font >= 2 && font <= 5)) {
1410                                 features.require("pifont");
1411                         }
1412                 }
1413         }
1414
1415         if (lyxerr.debugging(Debug::LATEX)) {
1416                 features.showStruct();
1417         }
1418 }
1419
1420
1421 void Buffer::getLabelList(vector<docstring> & list) const
1422 {
1423         /// if this is a child document and the parent is already loaded
1424         /// Use the parent's list instead  [ale990407]
1425         Buffer const * tmp = masterBuffer();
1426         if (!tmp) {
1427                 lyxerr << "masterBuffer() failed!" << endl;
1428                 BOOST_ASSERT(tmp);
1429         }
1430         if (tmp != this) {
1431                 tmp->getLabelList(list);
1432                 return;
1433         }
1434
1435         loadChildDocuments();
1436
1437         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
1438                 it.nextInset()->getLabelList(*this, list);
1439 }
1440
1441
1442 void Buffer::updateBibfilesCache()
1443 {
1444         // if this is a child document and the parent is already loaded
1445         // update the parent's cache instead
1446         Buffer * tmp = masterBuffer();
1447         BOOST_ASSERT(tmp);
1448         if (tmp != this) {
1449                 tmp->updateBibfilesCache();
1450                 return;
1451         }
1452
1453         bibfilesCache_.clear();
1454         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
1455                 if (it->lyxCode() == BIBTEX_CODE) {
1456                         InsetBibtex const & inset =
1457                                 static_cast<InsetBibtex const &>(*it);
1458                         vector<FileName> const bibfiles = inset.getFiles(*this);
1459                         bibfilesCache_.insert(bibfilesCache_.end(),
1460                                 bibfiles.begin(),
1461                                 bibfiles.end());
1462                 } else if (it->lyxCode() == INCLUDE_CODE) {
1463                         InsetInclude & inset =
1464                                 static_cast<InsetInclude &>(*it);
1465                         inset.updateBibfilesCache(*this);
1466                         vector<FileName> const & bibfiles =
1467                                         inset.getBibfilesCache(*this);
1468                         bibfilesCache_.insert(bibfilesCache_.end(),
1469                                 bibfiles.begin(),
1470                                 bibfiles.end());
1471                 }
1472         }
1473 }
1474
1475
1476 vector<FileName> const & Buffer::getBibfilesCache() const
1477 {
1478         // if this is a child document and the parent is already loaded
1479         // use the parent's cache instead
1480         Buffer const * tmp = masterBuffer();
1481         BOOST_ASSERT(tmp);
1482         if (tmp != this)
1483                 return tmp->getBibfilesCache();
1484
1485         // We update the cache when first used instead of at loading time.
1486         if (bibfilesCache_.empty())
1487                 const_cast<Buffer *>(this)->updateBibfilesCache();
1488
1489         return bibfilesCache_;
1490 }
1491
1492
1493 bool Buffer::isDepClean(string const & name) const
1494 {
1495         DepClean::const_iterator const it = pimpl_->dep_clean.find(name);
1496         if (it == pimpl_->dep_clean.end())
1497                 return true;
1498         return it->second;
1499 }
1500
1501
1502 void Buffer::markDepClean(string const & name)
1503 {
1504         pimpl_->dep_clean[name] = true;
1505 }
1506
1507
1508 bool Buffer::dispatch(string const & command, bool * result)
1509 {
1510         return dispatch(lyxaction.lookupFunc(command), result);
1511 }
1512
1513
1514 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1515 {
1516         bool dispatched = true;
1517
1518         switch (func.action) {
1519                 case LFUN_BUFFER_EXPORT: {
1520                         bool const tmp = doExport(to_utf8(func.argument()), false);
1521                         if (result)
1522                                 *result = tmp;
1523                         break;
1524                 }
1525
1526                 default:
1527                         dispatched = false;
1528         }
1529         return dispatched;
1530 }
1531
1532
1533 void Buffer::changeLanguage(Language const * from, Language const * to)
1534 {
1535         BOOST_ASSERT(from);
1536         BOOST_ASSERT(to);
1537
1538         for_each(par_iterator_begin(),
1539                  par_iterator_end(),
1540                  bind(&Paragraph::changeLanguage, _1, params(), from, to));
1541 }
1542
1543
1544 bool Buffer::isMultiLingual() const
1545 {
1546         ParConstIterator end = par_iterator_end();
1547         for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1548                 if (it->isMultiLingual(params()))
1549                         return true;
1550
1551         return false;
1552 }
1553
1554
1555 ParIterator Buffer::getParFromID(int const id) const
1556 {
1557         ParConstIterator it = par_iterator_begin();
1558         ParConstIterator const end = par_iterator_end();
1559
1560         if (id < 0) {
1561                 // John says this is called with id == -1 from undo
1562                 lyxerr << "getParFromID(), id: " << id << endl;
1563                 return end;
1564         }
1565
1566         for (; it != end; ++it)
1567                 if (it->id() == id)
1568                         return it;
1569
1570         return end;
1571 }
1572
1573
1574 bool Buffer::hasParWithID(int const id) const
1575 {
1576         ParConstIterator const it = getParFromID(id);
1577         return it != par_iterator_end();
1578 }
1579
1580
1581 ParIterator Buffer::par_iterator_begin()
1582 {
1583         return lyx::par_iterator_begin(inset());
1584 }
1585
1586
1587 ParIterator Buffer::par_iterator_end()
1588 {
1589         return lyx::par_iterator_end(inset());
1590 }
1591
1592
1593 ParConstIterator Buffer::par_iterator_begin() const
1594 {
1595         return lyx::par_const_iterator_begin(inset());
1596 }
1597
1598
1599 ParConstIterator Buffer::par_iterator_end() const
1600 {
1601         return lyx::par_const_iterator_end(inset());
1602 }
1603
1604
1605 Language const * Buffer::language() const
1606 {
1607         return params().language;
1608 }
1609
1610
1611 docstring const Buffer::B_(string const & l10n) const
1612 {
1613         return params().B_(l10n);
1614 }
1615
1616
1617 bool Buffer::isClean() const
1618 {
1619         return pimpl_->lyx_clean;
1620 }
1621
1622
1623 bool Buffer::isBakClean() const
1624 {
1625         return pimpl_->bak_clean;
1626 }
1627
1628
1629 bool Buffer::isExternallyModified(CheckMethod method) const
1630 {
1631         BOOST_ASSERT(pimpl_->filename.exists());
1632         // if method == timestamp, check timestamp before checksum
1633         return (method == checksum_method 
1634                 || pimpl_->timestamp_ != pimpl_->filename.lastModified())
1635                 && pimpl_->checksum_ != sum(pimpl_->filename);
1636 }
1637
1638
1639 void Buffer::saveCheckSum(FileName const & file) const
1640 {
1641         if (file.exists()) {
1642                 pimpl_->timestamp_ = file.lastModified();
1643                 pimpl_->checksum_ = sum(file);
1644         } else {
1645                 // in the case of save to a new file.
1646                 pimpl_->timestamp_ = 0;
1647                 pimpl_->checksum_ = 0;
1648         }
1649 }
1650
1651
1652 void Buffer::markClean() const
1653 {
1654         if (!pimpl_->lyx_clean) {
1655                 pimpl_->lyx_clean = true;
1656                 updateTitles();
1657         }
1658         // if the .lyx file has been saved, we don't need an
1659         // autosave
1660         pimpl_->bak_clean = true;
1661 }
1662
1663
1664 void Buffer::markBakClean() const
1665 {
1666         pimpl_->bak_clean = true;
1667 }
1668
1669
1670 void Buffer::setUnnamed(bool flag)
1671 {
1672         pimpl_->unnamed = flag;
1673 }
1674
1675
1676 bool Buffer::isUnnamed() const
1677 {
1678         return pimpl_->unnamed;
1679 }
1680
1681
1682 // FIXME: this function should be moved to buffer_pimpl.C
1683 void Buffer::markDirty()
1684 {
1685         if (pimpl_->lyx_clean) {
1686                 pimpl_->lyx_clean = false;
1687                 updateTitles();
1688         }
1689         pimpl_->bak_clean = false;
1690
1691         DepClean::iterator it = pimpl_->dep_clean.begin();
1692         DepClean::const_iterator const end = pimpl_->dep_clean.end();
1693
1694         for (; it != end; ++it)
1695                 it->second = false;
1696 }
1697
1698
1699 FileName Buffer::fileName() const
1700 {
1701         return pimpl_->filename;
1702 }
1703
1704
1705 string Buffer::absFileName() const
1706 {
1707         return pimpl_->filename.absFilename();
1708 }
1709
1710
1711 string const & Buffer::filePath() const
1712 {
1713         return params().filepath;
1714 }
1715
1716
1717 bool Buffer::isReadonly() const
1718 {
1719         return pimpl_->read_only;
1720 }
1721
1722
1723 void Buffer::setParentName(string const & name)
1724 {
1725         if (name == pimpl_->filename.absFilename())
1726                 // Avoids recursive include.
1727                 params().parentname.clear();
1728         else
1729                 params().parentname = name;
1730 }
1731
1732
1733 Buffer const * Buffer::masterBuffer() const
1734 {
1735         if (!params().parentname.empty()
1736                 && theBufferList().exists(params().parentname)) {
1737                 Buffer const * buf = theBufferList().getBuffer(params().parentname);
1738                 //We need to check if the parent is us...
1739                 //FIXME RECURSIVE INCLUDE
1740                 //This is not sufficient, since recursive includes could be downstream.
1741                 if (buf && buf != this)
1742                         return buf->masterBuffer();
1743         }
1744
1745         return this;
1746 }
1747
1748
1749 Buffer * Buffer::masterBuffer()
1750 {
1751         if (!params().parentname.empty()
1752             && theBufferList().exists(params().parentname)) {
1753                 Buffer * buf = theBufferList().getBuffer(params().parentname);
1754                 //We need to check if the parent is us...
1755                 //FIXME RECURSIVE INCLUDE
1756                 //This is not sufficient, since recursive includes could be downstream.
1757                 if (buf && buf != this)
1758                         return buf->masterBuffer();
1759         }
1760
1761         return this;
1762 }
1763
1764
1765 bool Buffer::hasMacro(docstring const & name, Paragraph const & par) const
1766 {
1767         Impl::PositionToMacroMap::iterator it;
1768         it = pimpl_->macros[name].upper_bound(par.macrocontextPosition());
1769         if (it != pimpl_->macros[name].end())
1770                 return true;
1771
1772         // If there is a master buffer, query that
1773         const Buffer * master = masterBuffer();
1774         if (master && master != this)
1775                 return master->hasMacro(name);
1776
1777         return MacroTable::globalMacros().has(name);
1778 }
1779
1780
1781 bool Buffer::hasMacro(docstring const & name) const
1782 {
1783         if( !pimpl_->macros[name].empty() )
1784                 return true;
1785
1786         // If there is a master buffer, query that
1787         const Buffer * master = masterBuffer();
1788         if (master && master != this)
1789                 return master->hasMacro(name);
1790
1791         return MacroTable::globalMacros().has(name);
1792 }
1793
1794
1795 MacroData const & Buffer::getMacro(docstring const & name,
1796         Paragraph const & par) const
1797 {
1798         Impl::PositionToMacroMap::iterator it;
1799         it = pimpl_->macros[name].upper_bound(par.macrocontextPosition());
1800         if( it != pimpl_->macros[name].end() )
1801                 return it->second;
1802
1803         // If there is a master buffer, query that
1804         const Buffer * master = masterBuffer();
1805         if (master && master != this)
1806                 return master->getMacro(name);
1807
1808         return MacroTable::globalMacros().get(name);
1809 }
1810
1811
1812 MacroData const & Buffer::getMacro(docstring const & name) const
1813 {
1814         Impl::PositionToMacroMap::iterator it;
1815         it = pimpl_->macros[name].begin();
1816         if( it != pimpl_->macros[name].end() )
1817                 return it->second;
1818
1819         // If there is a master buffer, query that
1820         const Buffer * master = masterBuffer();
1821         if (master && master != this)
1822                 return master->getMacro(name);
1823
1824         return MacroTable::globalMacros().get(name);
1825 }
1826
1827
1828 void Buffer::updateMacros()
1829 {
1830         // start with empty table
1831         pimpl_->macros = Impl::NameToPositionMacroMap();
1832
1833         // Iterate over buffer
1834         ParagraphList & pars = text().paragraphs();
1835         for (size_t i = 0, n = pars.size(); i != n; ++i) {
1836                 // set position again
1837                 pars[i].setMacrocontextPosition(i);
1838
1839                 //lyxerr << "searching main par " << i
1840                 //      << " for macro definitions" << std::endl;
1841                 InsetList const & insets = pars[i].insetList();
1842                 InsetList::const_iterator it = insets.begin();
1843                 InsetList::const_iterator end = insets.end();
1844                 for ( ; it != end; ++it) {
1845                         if (it->inset->lyxCode() != MATHMACRO_CODE)
1846                                 continue;
1847                         
1848                         // get macro data
1849                         MathMacroTemplate const & macroTemplate
1850                         = static_cast<MathMacroTemplate const &>(*it->inset);
1851
1852                         // valid?
1853                         if (macroTemplate.validMacro()) {
1854                                 MacroData macro = macroTemplate.asMacroData();
1855
1856                                 // redefinition?
1857                                 // call hasMacro here instead of directly querying mc to
1858                                 // also take the master document into consideration
1859                                 macro.setRedefinition(hasMacro(macroTemplate.name()));
1860
1861                                 // register macro (possibly overwrite the previous one of this paragraph)
1862                                 pimpl_->macros[macroTemplate.name()][i] = macro;
1863                         }
1864                 }
1865         }
1866 }
1867
1868
1869 void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
1870         InsetCode code)
1871 {
1872         //FIXME: This does not work for child documents yet.
1873         BOOST_ASSERT(code == CITE_CODE || code == REF_CODE);
1874         // Check if the label 'from' appears more than once
1875         vector<docstring> labels;
1876
1877         string paramName;
1878         if (code == CITE_CODE) {
1879                 BiblioInfo keys;
1880                 keys.fillWithBibKeys(this);
1881                 BiblioInfo::const_iterator bit  = keys.begin();
1882                 BiblioInfo::const_iterator bend = keys.end();
1883
1884                 for (; bit != bend; ++bit)
1885                         // FIXME UNICODE
1886                         labels.push_back(bit->first);
1887                 paramName = "key";
1888         } else {
1889                 getLabelList(labels);
1890                 paramName = "reference";
1891         }
1892
1893         if (std::count(labels.begin(), labels.end(), from) > 1)
1894                 return;
1895
1896         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
1897                 if (it->lyxCode() == code) {
1898                         InsetCommand & inset = static_cast<InsetCommand &>(*it);
1899                         docstring const oldValue = inset.getParam(paramName);
1900                         if (oldValue == from)
1901                                 inset.setParam(paramName, to);
1902                 }
1903         }
1904 }
1905
1906
1907 void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
1908         pit_type par_end, bool full_source)
1909 {
1910         OutputParams runparams(&params().encoding());
1911         runparams.nice = true;
1912         runparams.flavor = OutputParams::LATEX;
1913         runparams.linelen = lyxrc.plaintext_linelen;
1914         // No side effect of file copying and image conversion
1915         runparams.dryrun = true;
1916
1917         texrow().reset();
1918         if (full_source) {
1919                 os << "% " << _("Preview source code") << "\n\n";
1920                 texrow().newline();
1921                 texrow().newline();
1922                 if (isLatex())
1923                         writeLaTeXSource(os, filePath(), runparams, true, true);
1924                 else {
1925                         writeDocBookSource(os, absFileName(), runparams, false);
1926                 }
1927         } else {
1928                 runparams.par_begin = par_begin;
1929                 runparams.par_end = par_end;
1930                 if (par_begin + 1 == par_end)
1931                         os << "% "
1932                            << bformat(_("Preview source code for paragraph %1$d"), par_begin)
1933                            << "\n\n";
1934                 else
1935                         os << "% "
1936                            << bformat(_("Preview source code from paragraph %1$s to %2$s"),
1937                                         convert<docstring>(par_begin),
1938                                         convert<docstring>(par_end - 1))
1939                            << "\n\n";
1940                 texrow().newline();
1941                 texrow().newline();
1942                 // output paragraphs
1943                 if (isLatex()) {
1944                         latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1945                 } else {
1946                         // DocBook
1947                         docbookParagraphs(paragraphs(), *this, os, runparams);
1948                 }
1949         }
1950 }
1951
1952
1953 ErrorList const & Buffer::errorList(string const & type) const
1954 {
1955         static ErrorList const emptyErrorList;
1956         std::map<string, ErrorList>::const_iterator I = pimpl_->errorLists.find(type);
1957         if (I == pimpl_->errorLists.end())
1958                 return emptyErrorList;
1959
1960         return I->second;
1961 }
1962
1963
1964 ErrorList & Buffer::errorList(string const & type)
1965 {
1966         return pimpl_->errorLists[type];
1967 }
1968
1969
1970 void Buffer::structureChanged() const
1971 {
1972         if (gui_)
1973                 gui_->structureChanged();
1974 }
1975
1976
1977 void Buffer::errors(std::string const & err) const
1978 {
1979         if (gui_)
1980                 gui_->errors(err);
1981 }
1982
1983
1984 void Buffer::message(docstring const & msg) const
1985 {
1986         if (gui_)
1987                 gui_->message(msg);
1988 }
1989
1990
1991 void Buffer::setBusy(bool on) const
1992 {
1993         if (gui_)
1994                 gui_->setBusy(on);
1995 }
1996
1997
1998 void Buffer::setReadOnly(bool on) const
1999 {
2000         if (pimpl_->wa_)
2001                 pimpl_->wa_->setReadOnly(on);
2002 }
2003
2004
2005 void Buffer::updateTitles() const
2006 {
2007         if (pimpl_->wa_)
2008                 pimpl_->wa_->updateTitles();
2009 }
2010
2011
2012 void Buffer::resetAutosaveTimers() const
2013 {
2014         if (gui_)
2015                 gui_->resetAutosaveTimers();
2016 }
2017
2018
2019 void Buffer::setGuiDelegate(frontend::GuiBufferDelegate * gui)
2020 {
2021         gui_ = gui;
2022 }
2023
2024
2025
2026 namespace {
2027
2028 class AutoSaveBuffer : public support::ForkedProcess {
2029 public:
2030         ///
2031         AutoSaveBuffer(Buffer const & buffer, FileName const & fname)
2032                 : buffer_(buffer), fname_(fname) {}
2033         ///
2034         virtual boost::shared_ptr<ForkedProcess> clone() const
2035         {
2036                 return boost::shared_ptr<ForkedProcess>(new AutoSaveBuffer(*this));
2037         }
2038         ///
2039         int start()
2040         {
2041                 command_ = to_utf8(bformat(_("Auto-saving %1$s"), 
2042                                                  from_utf8(fname_.absFilename())));
2043                 return run(DontWait);
2044         }
2045 private:
2046         ///
2047         virtual int generateChild();
2048         ///
2049         Buffer const & buffer_;
2050         FileName fname_;
2051 };
2052
2053
2054 #if !defined (HAVE_FORK)
2055 # define fork() -1
2056 #endif
2057
2058 int AutoSaveBuffer::generateChild()
2059 {
2060         // tmp_ret will be located (usually) in /tmp
2061         // will that be a problem?
2062         pid_t const pid = fork();
2063         // If you want to debug the autosave
2064         // you should set pid to -1, and comment out the fork.
2065         if (pid == 0 || pid == -1) {
2066                 // pid = -1 signifies that lyx was unable
2067                 // to fork. But we will do the save
2068                 // anyway.
2069                 bool failed = false;
2070
2071                 FileName const tmp_ret(tempName(FileName(), "lyxauto"));
2072                 if (!tmp_ret.empty()) {
2073                         buffer_.writeFile(tmp_ret);
2074                         // assume successful write of tmp_ret
2075                         if (!rename(tmp_ret, fname_)) {
2076                                 failed = true;
2077                                 // most likely couldn't move between
2078                                 // filesystems unless write of tmp_ret
2079                                 // failed so remove tmp file (if it
2080                                 // exists)
2081                                 unlink(tmp_ret);
2082                         }
2083                 } else {
2084                         failed = true;
2085                 }
2086
2087                 if (failed) {
2088                         // failed to write/rename tmp_ret so try writing direct
2089                         if (!buffer_.writeFile(fname_)) {
2090                                 // It is dangerous to do this in the child,
2091                                 // but safe in the parent, so...
2092                                 if (pid == -1) // emit message signal.
2093                                         buffer_.message(_("Autosave failed!"));
2094                         }
2095                 }
2096                 if (pid == 0) { // we are the child so...
2097                         _exit(0);
2098                 }
2099         }
2100         return pid;
2101 }
2102
2103 } // namespace anon
2104
2105
2106 // Perfect target for a thread...
2107 void Buffer::autoSave() const
2108 {
2109         if (isBakClean() || isReadonly()) {
2110                 // We don't save now, but we'll try again later
2111                 resetAutosaveTimers();
2112                 return;
2113         }
2114
2115         // emit message signal.
2116         message(_("Autosaving current document..."));
2117
2118         // create autosave filename
2119         string fname = filePath();
2120         fname += '#';
2121         fname += onlyFilename(absFileName());
2122         fname += '#';
2123
2124         AutoSaveBuffer autosave(*this, FileName(fname));
2125         autosave.start();
2126
2127         markBakClean();
2128         resetAutosaveTimers();
2129 }
2130
2131
2132 /** Write a buffer to a new file name and rename the buffer
2133     according to the new file name.
2134
2135     This function is e.g. used by menu callbacks and
2136     LFUN_BUFFER_WRITE_AS.
2137
2138     If 'newname' is empty (the default), the user is asked via a
2139     dialog for the buffer's new name and location.
2140
2141     If 'newname' is non-empty and has an absolute path, that is used.
2142     Otherwise the base directory of the buffer is used as the base
2143     for any relative path in 'newname'.
2144 */
2145
2146 bool Buffer::writeAs(string const & newname)
2147 {
2148         string fname = absFileName();
2149         string const oldname = fname;
2150
2151         if (newname.empty()) {  /// No argument? Ask user through dialog
2152
2153                 // FIXME UNICODE
2154                 FileDialog dlg(_("Choose a filename to save document as"),
2155                                    LFUN_BUFFER_WRITE_AS);
2156                 dlg.setButton1(_("Documents|#o#O"), from_utf8(lyxrc.document_path));
2157                 dlg.setButton2(_("Templates|#T#t"), from_utf8(lyxrc.template_path));
2158
2159                 if (!support::isLyXFilename(fname))
2160                         fname += ".lyx";
2161
2162                 support::FileFilterList const filter(_("LyX Documents (*.lyx)"));
2163
2164                 FileDialog::Result result =
2165                         dlg.save(from_utf8(onlyPath(fname)),
2166                                      filter,
2167                                      from_utf8(onlyFilename(fname)));
2168
2169                 if (result.first == FileDialog::Later)
2170                         return false;
2171
2172                 fname = to_utf8(result.second);
2173
2174                 if (fname.empty())
2175                         return false;
2176
2177                 // Make sure the absolute filename ends with appropriate suffix
2178                 fname = makeAbsPath(fname).absFilename();
2179                 if (!support::isLyXFilename(fname))
2180                         fname += ".lyx";
2181
2182         } else 
2183                 fname = makeAbsPath(newname, onlyPath(oldname)).absFilename();
2184
2185         if (FileName(fname).exists()) {
2186                 docstring const file = makeDisplayPath(fname, 30);
2187                 docstring text = bformat(_("The document %1$s already "
2188                                            "exists.\n\nDo you want to "
2189                                            "overwrite that document?"), 
2190                                          file);
2191                 int const ret = Alert::prompt(_("Overwrite document?"),
2192                         text, 0, 1, _("&Overwrite"), _("&Cancel"));
2193
2194                 if (ret == 1)
2195                         return false;
2196         }
2197
2198         // Ok, change the name of the buffer
2199         setFileName(fname);
2200         markDirty();
2201         bool unnamed = isUnnamed();
2202         setUnnamed(false);
2203         saveCheckSum(FileName(fname));
2204
2205         if (!menuWrite()) {
2206                 setFileName(oldname);
2207                 setUnnamed(unnamed);
2208                 saveCheckSum(FileName(oldname));
2209                 return false;
2210         }
2211
2212         removeAutosaveFile(oldname);
2213         return true;
2214 }
2215
2216
2217 bool Buffer::menuWrite()
2218 {
2219         if (save()) {
2220                 LyX::ref().session().lastFiles().add(FileName(absFileName()));
2221                 return true;
2222         }
2223
2224         // FIXME: we don't tell the user *WHY* the save failed !!
2225
2226         docstring const file = makeDisplayPath(absFileName(), 30);
2227
2228         docstring text = bformat(_("The document %1$s could not be saved.\n\n"
2229                                    "Do you want to rename the document and "
2230                                    "try again?"), file);
2231         int const ret = Alert::prompt(_("Rename and save?"),
2232                 text, 0, 1, _("&Rename"), _("&Cancel"));
2233
2234         if (ret != 0)
2235                 return false;
2236
2237         return writeAs();
2238 }
2239
2240
2241 void Buffer::loadChildDocuments() const
2242 {
2243         bool parse_error = false;
2244                 
2245         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
2246                 if (it->lyxCode() != INCLUDE_CODE)
2247                         continue;
2248                 InsetCommand const & inset = static_cast<InsetCommand const &>(*it);
2249                 InsetCommandParams const & ip = inset.params();
2250                 Buffer * child = loadIfNeeded(*this, ip);
2251                 if (!child)
2252                         continue;
2253                 parse_error |= !child->errorList("Parse").empty();
2254                 child->loadChildDocuments();
2255         }
2256
2257         if (use_gui && masterBuffer() == this)
2258                 updateLabels(*this);
2259 }
2260
2261
2262 string Buffer::bufferFormat() const
2263 {
2264         if (isDocBook())
2265                 return "docbook";
2266         if (isLiterate())
2267                 return "literate";
2268         return "latex";
2269 }
2270
2271
2272 bool Buffer::doExport(string const & format, bool put_in_tempdir,
2273         string & result_file)
2274 {
2275         string backend_format;
2276         OutputParams runparams(&params().encoding());
2277         runparams.flavor = OutputParams::LATEX;
2278         runparams.linelen = lyxrc.plaintext_linelen;
2279         vector<string> backs = backends();
2280         if (find(backs.begin(), backs.end(), format) == backs.end()) {
2281                 // Get shortest path to format
2282                 Graph::EdgePath path;
2283                 for (vector<string>::const_iterator it = backs.begin();
2284                      it != backs.end(); ++it) {
2285                         Graph::EdgePath p = theConverters().getPath(*it, format);
2286                         if (!p.empty() && (path.empty() || p.size() < path.size())) {
2287                                 backend_format = *it;
2288                                 path = p;
2289                         }
2290                 }
2291                 if (!path.empty())
2292                         runparams.flavor = theConverters().getFlavor(path);
2293                 else {
2294                         Alert::error(_("Couldn't export file"),
2295                                 bformat(_("No information for exporting the format %1$s."),
2296                                    formats.prettyName(format)));
2297                         return false;
2298                 }
2299         } else {
2300                 backend_format = format;
2301                 // FIXME: Don't hardcode format names here, but use a flag
2302                 if (backend_format == "pdflatex")
2303                         runparams.flavor = OutputParams::PDFLATEX;
2304         }
2305
2306         string filename = latexName(false);
2307         filename = addName(temppath(), filename);
2308         filename = changeExtension(filename,
2309                                    formats.extension(backend_format));
2310
2311         // Plain text backend
2312         if (backend_format == "text")
2313                 writePlaintextFile(*this, FileName(filename), runparams);
2314         // no backend
2315         else if (backend_format == "lyx")
2316                 writeFile(FileName(filename));
2317         // Docbook backend
2318         else if (isDocBook()) {
2319                 runparams.nice = !put_in_tempdir;
2320                 makeDocBookFile(FileName(filename), runparams);
2321         }
2322         // LaTeX backend
2323         else if (backend_format == format) {
2324                 runparams.nice = true;
2325                 if (!makeLaTeXFile(FileName(filename), string(), runparams))
2326                         return false;
2327         } else if (!lyxrc.tex_allows_spaces
2328                    && support::contains(filePath(), ' ')) {
2329                 Alert::error(_("File name error"),
2330                            _("The directory path to the document cannot contain spaces."));
2331                 return false;
2332         } else {
2333                 runparams.nice = false;
2334                 if (!makeLaTeXFile(FileName(filename), filePath(), runparams))
2335                         return false;
2336         }
2337
2338         string const error_type = (format == "program")
2339                 ? "Build" : bufferFormat();
2340         string const ext = formats.extension(format);
2341         FileName const tmp_result_file(changeExtension(filename, ext));
2342         bool const success = theConverters().convert(this, FileName(filename),
2343                 tmp_result_file, FileName(absFileName()), backend_format, format,
2344                 errorList(error_type));
2345         // Emit the signal to show the error list.
2346         if (format != backend_format)
2347                 errors(error_type);
2348         if (!success)
2349                 return false;
2350
2351         if (put_in_tempdir)
2352                 result_file = tmp_result_file.absFilename();
2353         else {
2354                 result_file = changeExtension(absFileName(), ext);
2355                 // We need to copy referenced files (e. g. included graphics
2356                 // if format == "dvi") to the result dir.
2357                 vector<ExportedFile> const files =
2358                         runparams.exportdata->externalFiles(format);
2359                 string const dest = onlyPath(result_file);
2360                 CopyStatus status = SUCCESS;
2361                 for (vector<ExportedFile>::const_iterator it = files.begin();
2362                                 it != files.end() && status != CANCEL; ++it) {
2363                         string const fmt =
2364                                 formats.getFormatFromFile(it->sourceName);
2365                         status = copyFile(fmt, it->sourceName,
2366                                           makeAbsPath(it->exportName, dest),
2367                                           it->exportName, status == FORCE);
2368                 }
2369                 if (status == CANCEL) {
2370                         message(_("Document export cancelled."));
2371                 } else if (tmp_result_file.exists()) {
2372                         // Finally copy the main file
2373                         status = copyFile(format, tmp_result_file,
2374                                           FileName(result_file), result_file,
2375                                           status == FORCE);
2376                         message(bformat(_("Document exported as %1$s "
2377                                                                "to file `%2$s'"),
2378                                                 formats.prettyName(format),
2379                                                 makeDisplayPath(result_file)));
2380                 } else {
2381                         // This must be a dummy converter like fax (bug 1888)
2382                         message(bformat(_("Document exported as %1$s"),
2383                                                 formats.prettyName(format)));
2384                 }
2385         }
2386
2387         return true;
2388 }
2389
2390
2391 bool Buffer::doExport(string const & format, bool put_in_tempdir)
2392 {
2393         string result_file;
2394         return doExport(format, put_in_tempdir, result_file);
2395 }
2396
2397
2398 bool Buffer::preview(string const & format)
2399 {
2400         string result_file;
2401         if (!doExport(format, true, result_file))
2402                 return false;
2403         return formats.view(*this, FileName(result_file), format);
2404 }
2405
2406
2407 bool Buffer::isExportable(string const & format) const
2408 {
2409         vector<string> backs = backends();
2410         for (vector<string>::const_iterator it = backs.begin();
2411              it != backs.end(); ++it)
2412                 if (theConverters().isReachable(*it, format))
2413                         return true;
2414         return false;
2415 }
2416
2417
2418 vector<Format const *> Buffer::exportableFormats(bool only_viewable) const
2419 {
2420         vector<string> backs = backends();
2421         vector<Format const *> result =
2422                 theConverters().getReachable(backs[0], only_viewable, true);
2423         for (vector<string>::const_iterator it = backs.begin() + 1;
2424              it != backs.end(); ++it) {
2425                 vector<Format const *>  r =
2426                         theConverters().getReachable(*it, only_viewable, false);
2427                 result.insert(result.end(), r.begin(), r.end());
2428         }
2429         return result;
2430 }
2431
2432
2433 vector<string> Buffer::backends() const
2434 {
2435         vector<string> v;
2436         if (params().getTextClass().isTeXClassAvailable()) {
2437                 v.push_back(bufferFormat());
2438                 // FIXME: Don't hardcode format names here, but use a flag
2439                 if (v.back() == "latex")
2440                         v.push_back("pdflatex");
2441         }
2442         v.push_back("text");
2443         v.push_back("lyx");
2444         return v;
2445 }
2446
2447
2448 bool Buffer::readFileHelper(FileName const & s)
2449 {
2450         // File information about normal file
2451         if (!s.exists()) {
2452                 docstring const file = makeDisplayPath(s.absFilename(), 50);
2453                 docstring text = bformat(_("The specified document\n%1$s"
2454                                                      "\ncould not be read."), file);
2455                 Alert::error(_("Could not read document"), text);
2456                 return false;
2457         }
2458
2459         // Check if emergency save file exists and is newer.
2460         FileName const e(s.absFilename() + ".emergency");
2461
2462         if (e.exists() && s.exists() && e.lastModified() > s.lastModified()) {
2463                 docstring const file = makeDisplayPath(s.absFilename(), 20);
2464                 docstring const text =
2465                         bformat(_("An emergency save of the document "
2466                                   "%1$s exists.\n\n"
2467                                                "Recover emergency save?"), file);
2468                 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
2469                                       _("&Recover"),  _("&Load Original"),
2470                                       _("&Cancel")))
2471                 {
2472                 case 0:
2473                         // the file is not saved if we load the emergency file.
2474                         markDirty();
2475                         return readFile(e);
2476                 case 1:
2477                         break;
2478                 default:
2479                         return false;
2480                 }
2481         }
2482
2483         // Now check if autosave file is newer.
2484         FileName const a(onlyPath(s.absFilename()) + '#' + onlyFilename(s.absFilename()) + '#');
2485
2486         if (a.exists() && s.exists() && a.lastModified() > s.lastModified()) {
2487                 docstring const file = makeDisplayPath(s.absFilename(), 20);
2488                 docstring const text =
2489                         bformat(_("The backup of the document "
2490                                   "%1$s is newer.\n\nLoad the "
2491                                                "backup instead?"), file);
2492                 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
2493                                       _("&Load backup"), _("Load &original"),
2494                                       _("&Cancel") ))
2495                 {
2496                 case 0:
2497                         // the file is not saved if we load the autosave file.
2498                         markDirty();
2499                         return readFile(a);
2500                 case 1:
2501                         // Here we delete the autosave
2502                         unlink(a);
2503                         break;
2504                 default:
2505                         return false;
2506                 }
2507         }
2508         return readFile(s);
2509 }
2510
2511
2512 bool Buffer::loadLyXFile(FileName const & s)
2513 {
2514         if (s.isReadable()) {
2515                 if (readFileHelper(s)) {
2516                         lyxvc().file_found_hook(s);
2517                         if (!s.isWritable())
2518                                 setReadonly(true);
2519                         return true;
2520                 }
2521         } else {
2522                 docstring const file = makeDisplayPath(s.absFilename(), 20);
2523                 // Here we probably should run
2524                 if (LyXVC::file_not_found_hook(s)) {
2525                         docstring const text =
2526                                 bformat(_("Do you want to retrieve the document"
2527                                                        " %1$s from version control?"), file);
2528                         int const ret = Alert::prompt(_("Retrieve from version control?"),
2529                                 text, 0, 1, _("&Retrieve"), _("&Cancel"));
2530
2531                         if (ret == 0) {
2532                                 // How can we know _how_ to do the checkout?
2533                                 // With the current VC support it has to be,
2534                                 // a RCS file since CVS do not have special ,v files.
2535                                 RCS::retrieve(s);
2536                                 return loadLyXFile(s);
2537                         }
2538                 }
2539         }
2540         return false;
2541 }
2542
2543
2544 void Buffer::bufferErrors(TeXErrors const & terr, ErrorList & errorList) const
2545 {
2546         TeXErrors::Errors::const_iterator cit = terr.begin();
2547         TeXErrors::Errors::const_iterator end = terr.end();
2548
2549         for (; cit != end; ++cit) {
2550                 int id_start = -1;
2551                 int pos_start = -1;
2552                 int errorRow = cit->error_in_line;
2553                 bool found = texrow().getIdFromRow(errorRow, id_start,
2554                                                        pos_start);
2555                 int id_end = -1;
2556                 int pos_end = -1;
2557                 do {
2558                         ++errorRow;
2559                         found = texrow().getIdFromRow(errorRow, id_end, pos_end);
2560                 } while (found && id_start == id_end && pos_start == pos_end);
2561
2562                 errorList.push_back(ErrorItem(cit->error_desc,
2563                         cit->error_text, id_start, pos_start, pos_end));
2564         }
2565 }
2566
2567 } // namespace lyx