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