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