]> git.lyx.org Git - lyx.git/blob - src/buffer.C
remove part of old texted<->mathed interface
[lyx.git] / src / buffer.C
1 /**
2  * \file buffer.C
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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "buffer.h"
14
15 #include "author.h"
16 #include "buffer_funcs.h"
17 #include "bufferlist.h"
18 #include "bufferparams.h"
19 #include "counters.h"
20 #include "Bullet.h"
21 #include "Chktex.h"
22 #include "debug.h"
23 #include "errorlist.h"
24 #include "exporter.h"
25 #include "format.h"
26 #include "funcrequest.h"
27 #include "gettext.h"
28 #include "iterators.h"
29 #include "language.h"
30 #include "LaTeX.h"
31 #include "LaTeXFeatures.h"
32 #include "LyXAction.h"
33 #include "lyxlex.h"
34 #include "lyxtext.h"
35 #include "lyxrc.h"
36 #include "lyxvc.h"
37 #include "lyx_main.h"
38 #include "messages.h"
39 #include "output.h"
40 #include "output_docbook.h"
41 #include "output_latex.h"
42 #include "output_linuxdoc.h"
43 #include "paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "PosIterator.h"
47 #include "sgml.h"
48 #include "texrow.h"
49 #include "undo.h"
50 #include "version.h"
51
52 #include "insets/insetbibitem.h"
53 #include "insets/insetbibtex.h"
54 #include "insets/insetinclude.h"
55 #include "insets/insettext.h"
56
57 #include "frontends/Alert.h"
58
59 #include "graphics/Previews.h"
60
61 #include "support/FileInfo.h"
62 #include "support/filetools.h"
63 #include "support/gzstream.h"
64 #include "support/lyxlib.h"
65 #include "support/os.h"
66 #include "support/path.h"
67 #include "support/textutils.h"
68 #include "support/tostr.h"
69
70 #include <boost/bind.hpp>
71
72 #include "support/std_sstream.h"
73
74 #include <iomanip>
75 #include <stack>
76
77 #include <utime.h>
78
79 #ifdef HAVE_LOCALE
80 #endif
81
82 using lyx::pos_type;
83
84 using lyx::support::AddName;
85 using lyx::support::atoi;
86 using lyx::support::bformat;
87 using lyx::support::ChangeExtension;
88 using lyx::support::cmd_ret;
89 using lyx::support::createBufferTmpDir;
90 using lyx::support::destroyDir;
91 using lyx::support::FileInfo;
92 using lyx::support::FileInfo;
93 using lyx::support::getExtFromContents;
94 using lyx::support::IsDirWriteable;
95 using lyx::support::IsFileWriteable;
96 using lyx::support::LibFileSearch;
97 using lyx::support::ltrim;
98 using lyx::support::MakeAbsPath;
99 using lyx::support::MakeDisplayPath;
100 using lyx::support::MakeLatexName;
101 using lyx::support::OnlyFilename;
102 using lyx::support::OnlyPath;
103 using lyx::support::Path;
104 using lyx::support::QuoteName;
105 using lyx::support::removeAutosaveFile;
106 using lyx::support::rename;
107 using lyx::support::RunCommand;
108 using lyx::support::split;
109 using lyx::support::strToInt;
110 using lyx::support::subst;
111 using lyx::support::tempName;
112 using lyx::support::trim;
113
114 namespace os = lyx::support::os;
115
116 using std::endl;
117 using std::for_each;
118 using std::make_pair;
119
120 using std::ifstream;
121 using std::ios;
122 using std::ostream;
123 using std::ostringstream;
124 using std::ofstream;
125 using std::pair;
126 using std::stack;
127 using std::vector;
128 using std::string;
129
130
131 // all these externs should eventually be removed.
132 extern BufferList bufferlist;
133
134 namespace {
135
136 const int LYX_FORMAT = 230;
137
138 } // namespace anon
139
140
141 typedef std::map<string, bool> DepClean;
142
143 struct Buffer::Impl
144 {
145         Impl(Buffer & parent, string const & file, bool readonly);
146
147         limited_stack<Undo> undostack;
148         limited_stack<Undo> redostack;
149         BufferParams params;
150         LyXVC lyxvc;
151         string temppath;
152         TexRow texrow;
153
154         /// need to regenerate .tex?
155         DepClean dep_clean;
156
157         /// is save needed?
158         mutable bool lyx_clean;
159
160         /// is autosave needed?
161         mutable bool bak_clean;
162
163         /// is this a unnamed file (New...)?
164         bool unnamed;
165
166         /// buffer is r/o
167         bool read_only;
168
169         /// name of the file the buffer is associated with.
170         string filename;
171
172         /// The path to the document file.
173         string filepath;
174
175         boost::scoped_ptr<Messages> messages;
176
177         /** Set to true only when the file is fully loaded.
178          *  Used to prevent the premature generation of previews
179          *  and by the citation inset.
180          */
181         bool file_fully_loaded;
182
183         /// our LyXText that should be wrapped in an InsetText
184         InsetText inset;
185 };
186
187
188 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
189         : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
190           filename(file), filepath(OnlyPath(file)), file_fully_loaded(false),
191                 inset(params)
192 {
193         lyxvc.buffer(&parent);
194         temppath = createBufferTmpDir();
195         // FIXME: And now do something if temppath == string(), because we
196         // assume from now on that temppath points to a valid temp dir.
197         // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
198 }
199
200
201 Buffer::Buffer(string const & file, bool ronly)
202         : pimpl_(new Impl(*this, file, ronly))
203 {
204         lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
205         lyxerr << "Buffer::Buffer()" << endl;
206 }
207
208
209 Buffer::~Buffer()
210 {
211         lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
212         // here the buffer should take care that it is
213         // saved properly, before it goes into the void.
214
215         closing();
216
217         if (!temppath().empty() && destroyDir(temppath()) != 0) {
218                 Alert::warning(_("Could not remove temporary directory"),
219                         bformat(_("Could not remove the temporary directory %1$s"), temppath()));
220         }
221
222         // Remove any previewed LaTeX snippets associated with this buffer.
223         lyx::graphics::Previews::get().removeLoader(*this);
224 }
225
226
227 LyXText & Buffer::text() const
228 {
229         return const_cast<LyXText &>(pimpl_->inset.text_);
230 }
231
232
233 InsetBase & Buffer::inset() const
234 {
235         return const_cast<InsetText &>(pimpl_->inset);
236 }
237
238
239 limited_stack<Undo> & Buffer::undostack()
240 {
241         return pimpl_->undostack;
242 }
243
244
245 limited_stack<Undo> const & Buffer::undostack() const
246 {
247         return pimpl_->undostack;
248 }
249
250
251 limited_stack<Undo> & Buffer::redostack()
252 {
253         return pimpl_->redostack;
254 }
255
256
257 limited_stack<Undo> const & Buffer::redostack() const
258 {
259         return pimpl_->redostack;
260 }
261
262
263 BufferParams & Buffer::params()
264 {
265         return pimpl_->params;
266 }
267
268
269 BufferParams const & Buffer::params() const
270 {
271         return pimpl_->params;
272 }
273
274
275 ParagraphList & Buffer::paragraphs()
276 {
277         return text().paragraphs();
278 }
279
280
281 ParagraphList const & Buffer::paragraphs() const
282 {
283         return text().paragraphs();
284 }
285
286
287 LyXVC & Buffer::lyxvc()
288 {
289         return pimpl_->lyxvc;
290 }
291
292
293 LyXVC const & Buffer::lyxvc() const
294 {
295         return pimpl_->lyxvc;
296 }
297
298
299 string const & Buffer::temppath() const
300 {
301         return pimpl_->temppath;
302 }
303
304
305 TexRow & Buffer::texrow()
306 {
307         return pimpl_->texrow;
308 }
309
310
311 TexRow const & Buffer::texrow() const
312 {
313         return pimpl_->texrow;
314 }
315
316
317 string const Buffer::getLatexName(bool no_path) const
318 {
319         string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
320         return no_path ? OnlyFilename(name) : name;
321 }
322
323
324 pair<Buffer::LogType, string> const Buffer::getLogName() const
325 {
326         string const filename = getLatexName(false);
327
328         if (filename.empty())
329                 return make_pair(Buffer::latexlog, string());
330
331         string const path = temppath();
332
333         string const fname = AddName(path,
334                                      OnlyFilename(ChangeExtension(filename,
335                                                                   ".log")));
336         string const bname =
337                 AddName(path, OnlyFilename(
338                         ChangeExtension(filename,
339                                         formats.extension("literate") + ".out")));
340
341         // If no Latex log or Build log is newer, show Build log
342
343         FileInfo const f_fi(fname);
344         FileInfo const b_fi(bname);
345
346         if (b_fi.exist() &&
347             (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
348                 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
349                 return make_pair(Buffer::buildlog, bname);
350         }
351         lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
352         return make_pair(Buffer::latexlog, fname);
353 }
354
355
356 void Buffer::setReadonly(bool flag)
357 {
358         if (pimpl_->read_only != flag) {
359                 pimpl_->read_only = flag;
360                 readonly(flag);
361         }
362 }
363
364
365 void Buffer::setFileName(string const & newfile)
366 {
367         pimpl_->filename = MakeAbsPath(newfile);
368         pimpl_->filepath = OnlyPath(pimpl_->filename);
369         setReadonly(IsFileWriteable(pimpl_->filename) == 0);
370         updateTitles();
371 }
372
373
374 // We'll remove this later. (Lgb)
375 namespace {
376
377 void unknownClass(string const & unknown)
378 {
379         Alert::warning(_("Unknown document class"),
380                 bformat(_("Using the default document class, because the "
381                         "class %1$s is unknown."), unknown));
382 }
383
384 } // anon
385
386
387 int Buffer::readHeader(LyXLex & lex)
388 {
389         int unknown_tokens = 0;
390
391         while (lex.isOK()) {
392                 lex.nextToken();
393                 string const token = lex.getString();
394
395                 if (token.empty())
396                         continue;
397
398                 if (token == "\\end_header")
399                         break;
400
401                 lyxerr[Debug::PARSER] << "Handling header token: `"
402                                       << token << '\'' << endl;
403
404
405                 string unknown = params().readToken(lex, token);
406                 if (!unknown.empty()) {
407                         if (unknown[0] != '\\') {
408                                 unknownClass(unknown);
409                         } else {
410                                 ++unknown_tokens;
411                                 string const s = bformat(_("Unknown token: "
412                                                            "%1$s %2$s\n"),
413                                                          token,
414                                                          lex.getString());
415                                 error(ErrorItem(_("Header error"), s,
416                                                 -1, 0, 0));
417                         }
418                 }
419         }
420         return unknown_tokens;
421 }
422
423
424 // Uwe C. Schroeder
425 // changed to be public and have one parameter
426 // Returns false if "\end_document" is not read (Asger)
427 bool Buffer::readBody(LyXLex & lex)
428 {
429         bool the_end_read = false;
430
431         if (paragraphs().empty()) {
432                 readHeader(lex);
433                 if (!params().getLyXTextClass().load()) {
434                         string theclass = params().getLyXTextClass().name();
435                         Alert::error(_("Can't load document class"), bformat(
436                                         "Using the default document class, because the "
437                                         " class %1$s could not be loaded.", theclass));
438                         params().textclass = 0;
439                 }
440         } else {
441                 // We don't want to adopt the parameters from the
442                 // document we insert, so read them into a temporary buffer
443                 // and then discard it
444
445                 Buffer tmpbuf("", false);
446                 tmpbuf.readHeader(lex);
447         }
448
449         if (text().read(*this, lex))
450                 the_end_read = true;
451
452         return the_end_read;
453 }
454
455
456 // needed to insert the selection
457 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
458                                  LyXFont const & fn, string const & str)
459 {
460         LyXLayout_ptr const & layout = par->layout();
461
462         LyXFont font = fn;
463
464         par->checkInsertChar(font);
465         // insert the string, don't insert doublespace
466         bool space_inserted = true;
467         bool autobreakrows = !par->inInset() ||
468                 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
469         for(string::const_iterator cit = str.begin();
470             cit != str.end(); ++cit) {
471                 if (*cit == '\n') {
472                         if (autobreakrows && (!par->empty() || par->allowEmpty())) {
473                                 breakParagraph(params(), paragraphs(), par, pos,
474                                                layout->isEnvironment());
475                                 ++par;
476                                 pos = 0;
477                                 space_inserted = true;
478                         } else {
479                                 continue;
480                         }
481                         // do not insert consecutive spaces if !free_spacing
482                 } else if ((*cit == ' ' || *cit == '\t') &&
483                            space_inserted && !par->isFreeSpacing()) {
484                         continue;
485                 } else if (*cit == '\t') {
486                         if (!par->isFreeSpacing()) {
487                                 // tabs are like spaces here
488                                 par->insertChar(pos, ' ', font);
489                                 ++pos;
490                                 space_inserted = true;
491                         } else {
492                                 const pos_type n = 8 - pos % 8;
493                                 for (pos_type i = 0; i < n; ++i) {
494                                         par->insertChar(pos, ' ', font);
495                                         ++pos;
496                                 }
497                                 space_inserted = true;
498                         }
499                 } else if (!IsPrintable(*cit)) {
500                         // Ignore unprintables
501                         continue;
502                 } else {
503                         // just insert the character
504                         par->insertChar(pos, *cit, font);
505                         ++pos;
506                         space_inserted = (*cit == ' ');
507                 }
508
509         }
510 }
511
512
513 bool Buffer::readFile(string const & filename)
514 {
515         // Check if the file is compressed.
516         string const format = getExtFromContents(filename);
517         if (format == "gzip" || format == "zip" || format == "compress") {
518                 params().compressed = true;
519         }
520
521         // remove dummy empty par
522         paragraphs().clear();
523         bool ret = readFile(filename, paragraphs().end());
524
525         // After we have read a file, we must ensure that the buffer
526         // language is set and used in the gui.
527         // If you know of a better place to put this, please tell me. (Lgb)
528         updateDocLang(params().language);
529
530         return ret;
531 }
532
533
534 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
535 {
536         LyXLex lex(0, 0);
537         lex.setFile(filename);
538         return readFile(lex, filename, pit);
539 }
540
541
542 bool Buffer::fully_loaded() const
543 {
544         return pimpl_->file_fully_loaded;
545 }
546
547
548 void Buffer::fully_loaded(bool value)
549 {
550         pimpl_->file_fully_loaded = value;
551 }
552
553
554 bool Buffer::readFile(LyXLex & lex, string const & filename,
555                       ParagraphList::iterator pit)
556 {
557         BOOST_ASSERT(!filename.empty());
558
559         if (!lex.isOK()) {
560                 Alert::error(_("Document could not be read"),
561                              bformat(_("%1$s could not be read."), filename));
562                 return false;
563         }
564
565         lex.next();
566         string const token(lex.getString());
567
568         if (!lex.isOK()) {
569                 Alert::error(_("Document could not be read"),
570                              bformat(_("%1$s could not be read."), filename));
571                 return false;
572         }
573
574         // the first token _must_ be...
575         if (token != "\\lyxformat") {
576                 lyxerr << "Token: " << token << endl;
577
578                 Alert::error(_("Document format failure"),
579                              bformat(_("%1$s is not a LyX document."),
580                                        filename));
581                 return false;
582         }
583
584         lex.eatLine();
585         string tmp_format = lex.getString();
586         //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
587         // if present remove ".," from string.
588         string::size_type dot = tmp_format.find_first_of(".,");
589         //lyxerr << "           dot found at " << dot << endl;
590         if (dot != string::npos)
591                         tmp_format.erase(dot, 1);
592         int file_format = strToInt(tmp_format);
593         //lyxerr << "format: " << file_format << endl;
594
595         if (file_format > LYX_FORMAT) {
596                 Alert::warning(_("Document format failure"),
597                                bformat(_("%1$s was created with a newer"
598                                          " version of LyX. This is likely to"
599                                          " cause problems."),
600                                          filename));
601         } else if (file_format < LYX_FORMAT) {
602                 string const tmpfile = tempName();
603                 if (tmpfile.empty()) {
604                         Alert::error(_("Conversion failed"),
605                                      bformat(_("%1$s is from an earlier"
606                                               " version of LyX, but a temporary"
607                                               " file for converting it could"
608                                               " not be created."),
609                                               filename));
610                         return false;
611                 }
612                 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
613                 if (command.empty()) {
614                         Alert::error(_("Conversion script not found"),
615                                      bformat(_("%1$s is from an earlier"
616                                                " version of LyX, but the"
617                                                " conversion script lyx2lyx"
618                                                " could not be found."),
619                                                filename));
620                         return false;
621                 }
622                 command += " -t"
623                         + tostr(LYX_FORMAT)
624                         + " -o " + tmpfile + ' '
625                         + QuoteName(filename);
626                 lyxerr[Debug::INFO] << "Running '"
627                                     << command << '\''
628                                     << endl;
629                 cmd_ret const ret = RunCommand(command);
630                 if (ret.first != 0) {
631                         Alert::error(_("Conversion script failed"),
632                                      bformat(_("%1$s is from an earlier version"
633                                               " of LyX, but the lyx2lyx script"
634                                               " failed to convert it."),
635                                               filename));
636                         return false;
637                 } else {
638                         bool ret = readFile(tmpfile, pit);
639                         // Do stuff with tmpfile name and buffer name here.
640                         return ret;
641                 }
642
643         }
644
645         bool the_end = readBody(lex);
646         params().setPaperStuff();
647
648 #warning Look here!
649 #if 0
650         if (token == "\\end_document")
651                 the_end_read = true;
652
653         if (!the_end) {
654                 Alert::error(_("Document format failure"),
655                              bformat(_("%1$s ended unexpectedly, which means"
656                                        " that it is probably corrupted."),
657                                        filename));
658         }
659 #endif 
660         pimpl_->file_fully_loaded = true;
661         return true;
662 }
663
664
665 // Should probably be moved to somewhere else: BufferView? LyXView?
666 bool Buffer::save() const
667 {
668         // We don't need autosaves in the immediate future. (Asger)
669         resetAutosaveTimers();
670
671         // make a backup
672         string s;
673         if (lyxrc.make_backup) {
674                 s = fileName() + '~';
675                 if (!lyxrc.backupdir_path.empty())
676                         s = AddName(lyxrc.backupdir_path,
677                                     subst(os::slashify_path(s),'/','!'));
678
679                 // Rename is the wrong way of making a backup,
680                 // this is the correct way.
681                 /* truss cp fil fil2:
682                    lstat("LyXVC3.lyx", 0xEFFFF898)                 Err#2 ENOENT
683                    stat("LyXVC.lyx", 0xEFFFF688)                   = 0
684                    open("LyXVC.lyx", O_RDONLY)                     = 3
685                    open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
686                    fstat(4, 0xEFFFF508)                            = 0
687                    fstat(3, 0xEFFFF508)                            = 0
688                    read(3, " # T h i s   f i l e   w".., 8192)     = 5579
689                    write(4, " # T h i s   f i l e   w".., 5579)    = 5579
690                    read(3, 0xEFFFD4A0, 8192)                       = 0
691                    close(4)                                        = 0
692                    close(3)                                        = 0
693                    chmod("LyXVC3.lyx", 0100644)                    = 0
694                    lseek(0, 0, SEEK_CUR)                           = 46440
695                    _exit(0)
696                 */
697
698                 // Should probably have some more error checking here.
699                 // Doing it this way, also makes the inodes stay the same.
700                 // This is still not a very good solution, in particular we
701                 // might loose the owner of the backup.
702                 FileInfo finfo(fileName());
703                 if (finfo.exist()) {
704                         mode_t fmode = finfo.getMode();
705                         struct utimbuf times = {
706                                 finfo.getAccessTime(),
707                                 finfo.getModificationTime() };
708
709                         ifstream ifs(fileName().c_str());
710                         ofstream ofs(s.c_str(), ios::out|ios::trunc);
711                         if (ifs && ofs) {
712                                 ofs << ifs.rdbuf();
713                                 ifs.close();
714                                 ofs.close();
715                                 ::chmod(s.c_str(), fmode);
716
717                                 if (::utime(s.c_str(), &times)) {
718                                         lyxerr << "utime error." << endl;
719                                 }
720                         } else {
721                                 lyxerr << "LyX was not able to make "
722                                         "backup copy. Beware." << endl;
723                         }
724                 }
725         }
726
727         if (writeFile(fileName())) {
728                 markClean();
729                 removeAutosaveFile(fileName());
730         } else {
731                 // Saving failed, so backup is not backup
732                 if (lyxrc.make_backup)
733                         rename(s, fileName());
734                 return false;
735         }
736         return true;
737 }
738
739
740 bool Buffer::writeFile(string const & fname) const
741 {
742         if (pimpl_->read_only && fname == fileName())
743                 return false;
744
745         FileInfo finfo(fname);
746         if (finfo.exist() && !finfo.writable())
747                 return false;
748
749         bool retval;
750
751         if (params().compressed) {
752                 gz::ogzstream ofs(fname.c_str());
753                 if (!ofs)
754                         return false;
755
756                 retval = do_writeFile(ofs);
757
758         } else {
759                 ofstream ofs(fname.c_str());
760                 if (!ofs)
761                         return false;
762
763                 retval = do_writeFile(ofs);
764         }
765
766         return retval;
767 }
768
769
770 bool Buffer::do_writeFile(ostream & ofs) const
771 {
772 #ifdef HAVE_LOCALE
773         // Use the standard "C" locale for file output.
774         ofs.imbue(std::locale::classic());
775 #endif
776
777         // The top of the file should not be written by params().
778
779         // write out a comment in the top of the file
780         ofs << "#LyX " << lyx_version
781             << " created this file. For more info see http://www.lyx.org/\n"
782             << "\\lyxformat " << LYX_FORMAT << "\n";
783
784         // now write out the buffer parameters.
785         params().writeFile(ofs);
786
787         ofs << "\\end_header\n";
788
789         // write the text
790         text().write(*this, ofs);
791
792         // Write marker that shows file is complete
793         ofs << "\n\\end_document" << endl;
794
795         // Shouldn't really be needed....
796         //ofs.close();
797
798         // how to check if close went ok?
799         // Following is an attempt... (BE 20001011)
800
801         // good() returns false if any error occured, including some
802         //        formatting error.
803         // bad()  returns true if something bad happened in the buffer,
804         //        which should include file system full errors.
805
806         bool status = true;
807         if (!ofs) {
808                 status = false;
809                 lyxerr << "File was not closed properly." << endl;
810         }
811
812         return status;
813 }
814
815
816 void Buffer::makeLaTeXFile(string const & fname,
817                            string const & original_path,
818                            OutputParams const & runparams,
819                            bool output_preamble, bool output_body)
820 {
821         lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
822
823         ofstream ofs;
824         if (!openFileWrite(ofs, fname))
825                 return;
826
827         makeLaTeXFile(ofs, original_path,
828                       runparams, output_preamble, output_body);
829
830         ofs.close();
831         if (ofs.fail())
832                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
833 }
834
835
836 void Buffer::makeLaTeXFile(ostream & os,
837                            string const & original_path,
838                            OutputParams const & runparams_in,
839                            bool output_preamble, bool output_body)
840 {
841         OutputParams runparams = runparams_in;
842
843         // validate the buffer.
844         lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
845         LaTeXFeatures features(*this, params(), runparams.nice);
846         validate(features);
847         lyxerr[Debug::LATEX] << "  Buffer validation done." << endl;
848
849         texrow().reset();
850         // The starting paragraph of the coming rows is the
851         // first paragraph of the document. (Asger)
852         texrow().start(paragraphs().begin()->id(), 0);
853
854         if (output_preamble && runparams.nice) {
855                 os << "%% LyX " << lyx_version << " created this file.  "
856                         "For more info, see http://www.lyx.org/.\n"
857                         "%% Do not edit unless you really know what "
858                         "you are doing.\n";
859                 texrow().newline();
860                 texrow().newline();
861         }
862         lyxerr[Debug::INFO] << "lyx header finished" << endl;
863         // There are a few differences between nice LaTeX and usual files:
864         // usual is \batchmode and has a
865         // special input@path to allow the including of figures
866         // with either \input or \includegraphics (what figinsets do).
867         // input@path is set when the actual parameter
868         // original_path is set. This is done for usual tex-file, but not
869         // for nice-latex-file. (Matthias 250696)
870         if (output_preamble) {
871                 if (!runparams.nice) {
872                         // code for usual, NOT nice-latex-file
873                         os << "\\batchmode\n"; // changed
874                         // from \nonstopmode
875                         texrow().newline();
876                 }
877                 if (!original_path.empty()) {
878                         string inputpath = os::external_path(original_path);
879                         subst(inputpath, "~", "\\string~");
880                         os << "\\makeatletter\n"
881                             << "\\def\\input@path{{"
882                             << inputpath << "/}}\n"
883                             << "\\makeatother\n";
884                         texrow().newline();
885                         texrow().newline();
886                         texrow().newline();
887                 }
888
889                 // Write the preamble
890                 runparams.use_babel = params().writeLaTeX(os, features, texrow());
891
892                 if (!output_body)
893                         return;
894
895                 // make the body.
896                 os << "\\begin{document}\n";
897                 texrow().newline();
898         } // output_preamble
899         lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
900
901         if (!lyxrc.language_auto_begin) {
902                 os << subst(lyxrc.language_command_begin, "$$lang",
903                              params().language->babel())
904                     << endl;
905                 texrow().newline();
906         }
907
908         latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
909
910         // add this just in case after all the paragraphs
911         os << endl;
912         texrow().newline();
913
914         if (!lyxrc.language_auto_end) {
915                 os << subst(lyxrc.language_command_end, "$$lang",
916                              params().language->babel())
917                     << endl;
918                 texrow().newline();
919         }
920
921         if (output_preamble) {
922                 os << "\\end{document}\n";
923                 texrow().newline();
924
925                 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
926         } else {
927                 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
928                                      << endl;
929         }
930
931         // Just to be sure. (Asger)
932         texrow().newline();
933
934         lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
935         lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
936                             << '.' << endl;
937 }
938
939
940 bool Buffer::isLatex() const
941 {
942         return params().getLyXTextClass().outputType() == LATEX;
943 }
944
945
946 bool Buffer::isLinuxDoc() const
947 {
948         return params().getLyXTextClass().outputType() == LINUXDOC;
949 }
950
951
952 bool Buffer::isLiterate() const
953 {
954         return params().getLyXTextClass().outputType() == LITERATE;
955 }
956
957
958 bool Buffer::isDocBook() const
959 {
960         return params().getLyXTextClass().outputType() == DOCBOOK;
961 }
962
963
964 bool Buffer::isSGML() const
965 {
966         LyXTextClass const & tclass = params().getLyXTextClass();
967
968         return tclass.outputType() == LINUXDOC ||
969                tclass.outputType() == DOCBOOK;
970 }
971
972
973 void Buffer::makeLinuxDocFile(string const & fname,
974                               OutputParams const & runparams,
975                               bool body_only)
976 {
977         ofstream ofs;
978         if (!openFileWrite(ofs, fname))
979                 return;
980
981         LaTeXFeatures features(*this, params(), runparams.nice);
982         validate(features);
983
984         texrow().reset();
985
986         LyXTextClass const & tclass = params().getLyXTextClass();
987
988         string top_element = tclass.latexname();
989
990         if (!body_only) {
991                 ofs << tclass.class_header();
992
993                 string preamble = params().preamble;
994                 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
995                          : fname;
996                 preamble += features.getIncludedFiles(name);
997                 preamble += features.getLyXSGMLEntities();
998
999                 if (!preamble.empty()) {
1000                         ofs << " [ " << preamble << " ]";
1001                 }
1002                 ofs << ">\n\n";
1003
1004                 if (params().options.empty())
1005                         sgml::openTag(ofs, 0, false, top_element);
1006                 else {
1007                         string top = top_element;
1008                         top += ' ';
1009                         top += params().options;
1010                         sgml::openTag(ofs, 0, false, top);
1011                 }
1012         }
1013
1014         ofs << "<!-- LyX "  << lyx_version
1015             << " created this file. For more info see http://www.lyx.org/"
1016             << " -->\n";
1017
1018         linuxdocParagraphs(*this, paragraphs(), ofs, runparams);
1019
1020         if (!body_only) {
1021                 ofs << "\n\n";
1022                 sgml::closeTag(ofs, 0, false, top_element);
1023         }
1024
1025         ofs.close();
1026         if (ofs.fail())
1027                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1028 }
1029
1030
1031 void Buffer::makeDocBookFile(string const & fname,
1032                              OutputParams const & runparams,
1033                              bool only_body)
1034 {
1035         ofstream ofs;
1036         if (!openFileWrite(ofs, fname))
1037                 return;
1038
1039         LaTeXFeatures features(*this, params(), runparams.nice);
1040         validate(features);
1041
1042         texrow().reset();
1043
1044         LyXTextClass const & tclass = params().getLyXTextClass();
1045         string top_element = tclass.latexname();
1046
1047         if (!only_body) {
1048                 ofs << subst(tclass.class_header(), "#", top_element);
1049
1050                 string preamble = params().preamble;
1051                 string const name = runparams.nice ? ChangeExtension(pimpl_->filename, ".sgml")
1052                          : fname;
1053                 preamble += features.getIncludedFiles(name);
1054                 preamble += features.getLyXSGMLEntities();
1055
1056                 if (!preamble.empty()) {
1057                         ofs << "\n [ " << preamble << " ]";
1058                 }
1059                 ofs << ">\n\n";
1060         }
1061
1062         string top = top_element;
1063         top += " lang=\"";
1064         top += params().language->code();
1065         top += '"';
1066
1067         if (!params().options.empty()) {
1068                 top += ' ';
1069                 top += params().options;
1070         }
1071         sgml::openTag(ofs, 0, false, top);
1072
1073         ofs << "<!-- SGML/XML file was created by LyX " << lyx_version
1074             << "\n  See http://www.lyx.org/ for more information -->\n";
1075
1076         params().getLyXTextClass().counters().reset();
1077         docbookParagraphs(*this, paragraphs(), ofs, runparams);
1078
1079         ofs << "\n\n";
1080         sgml::closeTag(ofs, 0, false, top_element);
1081
1082         ofs.close();
1083         if (ofs.fail())
1084                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1085 }
1086
1087
1088 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1089 // Other flags: -wall -v0 -x
1090 int Buffer::runChktex()
1091 {
1092         busy(true);
1093
1094         // get LaTeX-Filename
1095         string const name = getLatexName();
1096         string const path = temppath();
1097         string const org_path = filePath();
1098
1099         Path p(path); // path to LaTeX file
1100         message(_("Running chktex..."));
1101
1102         // Generate the LaTeX file if neccessary
1103         OutputParams runparams;
1104         runparams.flavor = OutputParams::LATEX;
1105         runparams.nice = false;
1106         makeLaTeXFile(name, org_path, runparams);
1107
1108         TeXErrors terr;
1109         Chktex chktex(lyxrc.chktex_command, name, filePath());
1110         int res = chktex.run(terr); // run chktex
1111
1112         if (res == -1) {
1113                 Alert::error(_("chktex failure"),
1114                              _("Could not run chktex successfully."));
1115         } else if (res > 0) {
1116                 // Insert all errors as errors boxes
1117                 bufferErrors(*this, terr);
1118         }
1119
1120         busy(false);
1121
1122         return res;
1123 }
1124
1125
1126 void Buffer::validate(LaTeXFeatures & features) const
1127 {
1128         LyXTextClass const & tclass = params().getLyXTextClass();
1129
1130         if (params().tracking_changes) {
1131                 features.require("dvipost");
1132                 features.require("color");
1133         }
1134
1135         // AMS Style is at document level
1136         if (params().use_amsmath == BufferParams::AMS_ON
1137             || tclass.provides(LyXTextClass::amsmath))
1138                 features.require("amsmath");
1139
1140         for_each(paragraphs().begin(), paragraphs().end(),
1141                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1142
1143         // the bullet shapes are buffer level not paragraph level
1144         // so they are tested here
1145         for (int i = 0; i < 4; ++i) {
1146                 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1147                         int const font = params().user_defined_bullet(i).getFont();
1148                         if (font == 0) {
1149                                 int const c = params()
1150                                         .user_defined_bullet(i)
1151                                         .getCharacter();
1152                                 if (c == 16
1153                                    || c == 17
1154                                    || c == 25
1155                                    || c == 26
1156                                    || c == 31) {
1157                                         features.require("latexsym");
1158                                 }
1159                         } else if (font == 1) {
1160                                 features.require("amssymb");
1161                         } else if ((font >= 2 && font <= 5)) {
1162                                 features.require("pifont");
1163                         }
1164                 }
1165         }
1166
1167         if (lyxerr.debugging(Debug::LATEX)) {
1168                 features.showStruct();
1169         }
1170 }
1171
1172
1173 void Buffer::getLabelList(std::vector<string> & list) const
1174 {
1175         /// if this is a child document and the parent is already loaded
1176         /// Use the parent's list instead  [ale990407]
1177         if (!params().parentname.empty()
1178             && bufferlist.exists(params().parentname)) {
1179                 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1180                 if (tmp) {
1181                         tmp->getLabelList(list);
1182                         return;
1183                 }
1184         }
1185
1186         for (inset_iterator it = inset_const_iterator_begin();
1187              it != inset_const_iterator_end(); ++it) {
1188                 it->getLabelList(*this, list);
1189         }
1190 }
1191
1192
1193 // This is also a buffer property (ale)
1194 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys)
1195         const
1196 {
1197         /// if this is a child document and the parent is already loaded
1198         /// use the parent's list instead  [ale990412]
1199         if (!params().parentname.empty() &&
1200             bufferlist.exists(params().parentname)) {
1201                 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
1202                 if (tmp) {
1203                         tmp->fillWithBibKeys(keys);
1204                         return;
1205                 }
1206         }
1207
1208         for (inset_iterator it = inset_const_iterator_begin();
1209                 it != inset_const_iterator_end(); ++it) {
1210                 if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
1211                         InsetBibtex const & inset =
1212                                 dynamic_cast<InsetBibtex const &>(*it);
1213                         inset.fillWithBibKeys(*this, keys);
1214                 } else if (it->lyxCode() == InsetOld::INCLUDE_CODE) {
1215                         InsetInclude const & inset =
1216                                 dynamic_cast<InsetInclude const &>(*it);
1217                         inset.fillWithBibKeys(*this, keys);
1218                 } else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
1219                         InsetBibitem const & inset =
1220                                 dynamic_cast<InsetBibitem const &>(*it);
1221                         string const key = inset.getContents();
1222                         string const opt = inset.getOptions();
1223                         string const ref; // = pit->asString(this, false);
1224                         string const info = opt + "TheBibliographyRef" + ref;
1225                         keys.push_back(pair<string, string>(key, info));
1226                 }
1227         }
1228 }
1229
1230
1231 bool Buffer::isDepClean(string const & name) const
1232 {
1233         DepClean::const_iterator it = pimpl_->dep_clean.find(name);
1234         if (it == pimpl_->dep_clean.end())
1235                 return true;
1236         return it->second;
1237 }
1238
1239
1240 void Buffer::markDepClean(string const & name)
1241 {
1242         pimpl_->dep_clean[name] = true;
1243 }
1244
1245
1246 bool Buffer::dispatch(string const & command, bool * result)
1247 {
1248         return dispatch(lyxaction.lookupFunc(command), result);
1249 }
1250
1251
1252 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1253 {
1254         bool dispatched = true;
1255
1256         switch (func.action) {
1257                 case LFUN_EXPORT: {
1258                         bool const tmp = Exporter::Export(this, func.argument, false);
1259                         if (result)
1260                                 *result = tmp;
1261                         break;
1262                 }
1263
1264                 default:
1265                         dispatched = false;
1266         }
1267         return dispatched;
1268 }
1269
1270
1271 void Buffer::changeLanguage(Language const * from, Language const * to)
1272 {
1273         lyxerr << "Changing Language!" << endl;
1274
1275         // Take care of l10n/i18n
1276         updateDocLang(to);
1277
1278         ParIterator end = par_iterator_end();
1279         for (ParIterator it = par_iterator_begin(); it != end; ++it)
1280                 it->changeLanguage(params(), from, to);
1281 }
1282
1283
1284 void Buffer::updateDocLang(Language const * nlang)
1285 {
1286         pimpl_->messages.reset(new Messages(nlang->code()));
1287 }
1288
1289
1290 bool Buffer::isMultiLingual() const
1291 {
1292         ParConstIterator end = par_iterator_end();
1293         for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1294                 if (it->isMultiLingual(params()))
1295                         return true;
1296
1297         return false;
1298 }
1299
1300
1301 void Buffer::inset_iterator::setParagraph()
1302 {
1303         while (pit != pend) {
1304                 it = pit->insetlist.begin();
1305                 if (it != pit->insetlist.end())
1306                         return;
1307                 ++pit;
1308         }
1309 }
1310
1311
1312 ParIterator Buffer::getParFromID(int id) const
1313 {
1314 #warning FIXME: const correctness! (Andre)
1315         ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
1316         ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
1317
1318 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
1319         if (id < 0) {
1320                 // John says this is called with id == -1 from undo
1321                 lyxerr << "getParFromID(), id: " << id << endl;
1322                 return end;
1323         }
1324
1325         for (; it != end; ++it)
1326                 if (it->id() == id)
1327                         return it;
1328
1329         return end;
1330 }
1331
1332
1333 bool Buffer::hasParWithID(int id) const
1334 {
1335         ParConstIterator it = par_iterator_begin();
1336         ParConstIterator end = par_iterator_end();
1337
1338         if (id < 0) {
1339                 // John says this is called with id == -1 from undo
1340                 lyxerr << "hasParWithID(), id: " << id << endl;
1341                 return 0;
1342         }
1343
1344         for (; it != end; ++it)
1345                 if (it->id() == id)
1346                         return true;
1347
1348         return false;
1349 }
1350
1351
1352 PosIterator Buffer::pos_iterator_begin()
1353 {
1354         return PosIterator(&paragraphs(), paragraphs().begin(), 0);
1355 }
1356
1357
1358 PosIterator Buffer::pos_iterator_end()
1359 {
1360         return PosIterator(&paragraphs(), paragraphs().end(), 0);
1361 }
1362
1363
1364 ParIterator Buffer::par_iterator_begin()
1365 {
1366         return ParIterator(paragraphs().begin(), paragraphs());
1367 }
1368
1369
1370 ParIterator Buffer::par_iterator_end()
1371 {
1372         return ParIterator(paragraphs().end(), paragraphs());
1373 }
1374
1375
1376 ParConstIterator Buffer::par_iterator_begin() const
1377 {
1378         ParagraphList const & pars = paragraphs();
1379         return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
1380 }
1381
1382
1383 ParConstIterator Buffer::par_iterator_end() const
1384 {
1385         ParagraphList const & pars = paragraphs();
1386         return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
1387 }
1388
1389
1390 Language const * Buffer::getLanguage() const
1391 {
1392         return params().language;
1393 }
1394
1395
1396 string const Buffer::B_(string const & l10n) const
1397 {
1398         if (pimpl_->messages.get()) {
1399                 return pimpl_->messages->get(l10n);
1400         }
1401
1402         return _(l10n);
1403 }
1404
1405
1406 bool Buffer::isClean() const
1407 {
1408         return pimpl_->lyx_clean;
1409 }
1410
1411
1412 bool Buffer::isBakClean() const
1413 {
1414         return pimpl_->bak_clean;
1415 }
1416
1417
1418 void Buffer::markClean() const
1419 {
1420         if (!pimpl_->lyx_clean) {
1421                 pimpl_->lyx_clean = true;
1422                 updateTitles();
1423         }
1424         // if the .lyx file has been saved, we don't need an
1425         // autosave
1426         pimpl_->bak_clean = true;
1427 }
1428
1429
1430 void Buffer::markBakClean()
1431 {
1432         pimpl_->bak_clean = true;
1433 }
1434
1435
1436 void Buffer::setUnnamed(bool flag)
1437 {
1438         pimpl_->unnamed = flag;
1439 }
1440
1441
1442 bool Buffer::isUnnamed() const
1443 {
1444         return pimpl_->unnamed;
1445 }
1446
1447
1448 void Buffer::markDirty()
1449 {
1450         if (pimpl_->lyx_clean) {
1451                 pimpl_->lyx_clean = false;
1452                 updateTitles();
1453         }
1454         pimpl_->bak_clean = false;
1455
1456         DepClean::iterator it = pimpl_->dep_clean.begin();
1457         DepClean::const_iterator const end = pimpl_->dep_clean.end();
1458
1459         for (; it != end; ++it) {
1460                 it->second = false;
1461         }
1462 }
1463
1464
1465 string const & Buffer::fileName() const
1466 {
1467         return pimpl_->filename;
1468 }
1469
1470
1471 string const & Buffer::filePath() const
1472 {
1473         return pimpl_->filepath;
1474 }
1475
1476
1477 bool Buffer::isReadonly() const
1478 {
1479         return pimpl_->read_only;
1480 }
1481
1482
1483 void Buffer::setParentName(string const & name)
1484 {
1485         params().parentname = name;
1486 }
1487
1488
1489 Buffer::inset_iterator::inset_iterator()
1490         : pit(), pend()
1491 {}
1492
1493
1494 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
1495         : pit(p), pend(e)
1496 {
1497         setParagraph();
1498 }
1499
1500
1501 Buffer::inset_iterator Buffer::inset_iterator_begin()
1502 {
1503         return inset_iterator(paragraphs().begin(), paragraphs().end());
1504 }
1505
1506
1507 Buffer::inset_iterator Buffer::inset_iterator_end()
1508 {
1509         return inset_iterator(paragraphs().end(), paragraphs().end());
1510 }
1511
1512
1513 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
1514 {
1515         ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1516         return inset_iterator(pars.begin(), pars.end());
1517 }
1518
1519
1520 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
1521 {
1522         ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
1523         return inset_iterator(pars.end(), pars.end());
1524 }
1525
1526
1527 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
1528 {
1529         if (pit != pend) {
1530                 ++it;
1531                 if (it == pit->insetlist.end()) {
1532                         ++pit;
1533                         setParagraph();
1534                 }
1535         }
1536         return *this;
1537 }
1538
1539
1540 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
1541 {
1542         inset_iterator tmp = *this;
1543         ++*this;
1544         return tmp;
1545 }
1546
1547
1548 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
1549 {
1550         return *it->inset;
1551 }
1552
1553
1554 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
1555 {
1556         return it->inset;
1557 }
1558
1559
1560 ParagraphList::iterator Buffer::inset_iterator::getPar() const
1561 {
1562         return pit;
1563 }
1564
1565
1566 lyx::pos_type Buffer::inset_iterator::getPos() const
1567 {
1568         return it->pos;
1569 }
1570
1571
1572 bool operator==(Buffer::inset_iterator const & iter1,
1573                 Buffer::inset_iterator const & iter2)
1574 {
1575         return iter1.pit == iter2.pit
1576                 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
1577 }
1578
1579
1580 bool operator!=(Buffer::inset_iterator const & iter1,
1581                 Buffer::inset_iterator const & iter2)
1582 {
1583         return !(iter1 == iter2);
1584 }