]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInclude.cpp
mainly cosmetics
[lyx.git] / src / insets / InsetInclude.cpp
1 /**
2  * \file InsetInclude.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetInclude.h"
14
15 #include "Buffer.h"
16 #include "buffer_funcs.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "debug.h"
22 #include "DispatchResult.h"
23 #include "Exporter.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "LaTeXFeatures.h"
28 #include "LyX.h"
29 #include "LyXRC.h"
30 #include "Lexer.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33 #include "TocBackend.h"
34
35 #include "frontends/alert.h"
36 #include "frontends/Painter.h"
37
38 #include "graphics/PreviewImage.h"
39 #include "graphics/PreviewLoader.h"
40
41 #include "insets/RenderPreview.h"
42 #include "insets/InsetListingsParams.h"
43
44 #include "support/filetools.h"
45 #include "support/lstrings.h" // contains
46 #include "support/lyxalgo.h"
47 #include "support/lyxlib.h"
48 #include "support/convert.h"
49
50 #include <boost/bind.hpp>
51
52
53 namespace lyx {
54
55 // Implementation is in LyX.cpp
56 extern void dispatch(FuncRequest const & action);
57
58 using support::addName;
59 using support::absolutePath;
60 using support::bformat;
61 using support::changeExtension;
62 using support::contains;
63 using support::copy;
64 using support::DocFileName;
65 using support::FileName;
66 using support::getFileContents;
67 using support::getVectorFromString;
68 using support::isLyXFilename;
69 using support::isValidLaTeXFilename;
70 using support::latex_path;
71 using support::makeAbsPath;
72 using support::makeDisplayPath;
73 using support::makeRelPath;
74 using support::onlyFilename;
75 using support::onlyPath;
76 using support::prefixIs;
77 using support::subst;
78 using support::sum;
79
80 using std::endl;
81 using std::find;
82 using std::string;
83 using std::istringstream;
84 using std::ostream;
85 using std::ostringstream;
86 using std::vector;
87
88 namespace Alert = frontend::Alert;
89
90
91 namespace {
92
93 docstring const uniqueID()
94 {
95         static unsigned int seed = 1000;
96         return "file" + convert<docstring>(++seed);
97 }
98
99
100 bool isListings(InsetCommandParams const & params)
101 {
102         return params.getCmdName() == "lstinputlisting";
103 }
104
105 } // namespace anon
106
107
108 InsetInclude::InsetInclude(InsetCommandParams const & p)
109         : params_(p), include_label(uniqueID()),
110           preview_(new RenderMonitoredPreview(this)),
111           set_label_(false)
112 {
113         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
114 }
115
116
117 InsetInclude::InsetInclude(InsetInclude const & other)
118         : Inset(other),
119           params_(other.params_),
120           include_label(other.include_label),
121           preview_(new RenderMonitoredPreview(this)),
122           set_label_(false)
123 {
124         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
125 }
126
127
128 InsetInclude::~InsetInclude()
129 {
130         InsetIncludeMailer(*this).hideDialog();
131 }
132
133
134 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
135 {
136         switch (cmd.action) {
137
138         case LFUN_INSET_MODIFY: {
139                 InsetCommandParams p(INCLUDE_CODE);
140                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p);
141                 if (!p.getCmdName().empty()) {
142                         if (isListings(p)){
143                                 InsetListingsParams par_old(params().getOptions());
144                                 InsetListingsParams par_new(p.getOptions());
145                                 if (par_old.getParamValue("label") !=
146                                     par_new.getParamValue("label")
147                                     && !par_new.getParamValue("label").empty())
148                                         cur.bv().buffer().changeRefsIfUnique(
149                                                 from_utf8(par_old.getParamValue("label")),
150                                                 from_utf8(par_new.getParamValue("label")),
151                                                 REF_CODE);
152                         }
153                         set(p, cur.buffer());
154                         cur.buffer().updateBibfilesCache();
155                 } else
156                         cur.noUpdate();
157                 break;
158         }
159
160         case LFUN_INSET_DIALOG_UPDATE:
161                 InsetIncludeMailer(*this).updateDialog(&cur.bv());
162                 break;
163
164         case LFUN_MOUSE_RELEASE:
165                 if (!cur.selection())
166                         InsetIncludeMailer(*this).showDialog(&cur.bv());
167                 break;
168
169         default:
170                 Inset::doDispatch(cur, cmd);
171                 break;
172         }
173 }
174
175
176 bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
177                 FuncStatus & flag) const
178 {
179         switch (cmd.action) {
180
181         case LFUN_INSET_MODIFY:
182         case LFUN_INSET_DIALOG_UPDATE:
183                 flag.enabled(true);
184                 return true;
185
186         default:
187                 return Inset::getStatus(cur, cmd, flag);
188         }
189 }
190
191
192 InsetCommandParams const & InsetInclude::params() const
193 {
194         return params_;
195 }
196
197
198 namespace {
199
200 /// the type of inclusion
201 enum Types {
202         INCLUDE = 0,
203         VERB = 1,
204         INPUT = 2,
205         VERBAST = 3,
206         LISTINGS = 4,
207 };
208
209
210 Types type(InsetCommandParams const & params)
211 {
212         string const command_name = params.getCmdName();
213
214         if (command_name == "input")
215                 return INPUT;
216         if  (command_name == "verbatiminput")
217                 return VERB;
218         if  (command_name == "verbatiminput*")
219                 return VERBAST;
220         if  (command_name == "lstinputlisting")
221                 return LISTINGS;
222         return INCLUDE;
223 }
224
225
226 bool isVerbatim(InsetCommandParams const & params)
227 {
228         string const command_name = params.getCmdName();
229         return command_name == "verbatiminput" ||
230                 command_name == "verbatiminput*";
231 }
232
233
234 bool isInputOrInclude(InsetCommandParams const & params)
235 {
236         Types const t = type(params);
237         return (t == INPUT) || (t == INCLUDE);
238 }
239
240
241 string const masterFilename(Buffer const & buffer)
242 {
243         return buffer.masterBuffer()->absFileName();
244 }
245
246
247 string const parentFilename(Buffer const & buffer)
248 {
249         return buffer.absFileName();
250 }
251
252
253 FileName const includedFilename(Buffer const & buffer,
254                               InsetCommandParams const & params)
255 {
256         return makeAbsPath(to_utf8(params["filename"]),
257                            onlyPath(parentFilename(buffer)));
258 }
259
260
261 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
262
263 } // namespace anon
264
265
266 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
267 {
268         params_ = p;
269         set_label_ = false;
270
271         if (preview_->monitoring())
272                 preview_->stopMonitoring();
273
274         if (type(params_) == INPUT)
275                 add_preview(*preview_, *this, buffer);
276 }
277
278
279 Inset * InsetInclude::clone() const
280 {
281         return new InsetInclude(*this);
282 }
283
284
285 void InsetInclude::write(Buffer const &, ostream & os) const
286 {
287         write(os);
288 }
289
290
291 void InsetInclude::write(ostream & os) const
292 {
293         os << "Include " << to_utf8(params_.getCommand()) << '\n'
294            << "preview " << convert<string>(params_.preview()) << '\n';
295 }
296
297
298 void InsetInclude::read(Buffer const &, Lexer & lex)
299 {
300         read(lex);
301 }
302
303
304 void InsetInclude::read(Lexer & lex)
305 {
306         if (lex.isOK()) {
307                 lex.eatLine();
308                 string const command = lex.getString();
309                 params_.scanCommand(command);
310         }
311         string token;
312         while (lex.isOK()) {
313                 lex.next();
314                 token = lex.getString();
315                 if (token == "\\end_inset")
316                         break;
317                 if (token == "preview") {
318                         lex.next();
319                         params_.preview(lex.getBool());
320                 } else
321                         lex.printError("Unknown parameter name `$$Token' for command " + params_.getCmdName());
322         }
323         if (token != "\\end_inset") {
324                 lex.printError("Missing \\end_inset at this point. "
325                                "Read: `$$Token'");
326         }
327 }
328
329
330 docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
331 {
332         docstring temp;
333
334         switch (type(params_)) {
335                 case INPUT:
336                         temp = buf.B_("Input");
337                         break;
338                 case VERB:
339                         temp = buf.B_("Verbatim Input");
340                         break;
341                 case VERBAST:
342                         temp = buf.B_("Verbatim Input*");
343                         break;
344                 case INCLUDE:
345                         temp = buf.B_("Include");
346                         break;
347                 case LISTINGS: {
348                         temp = listings_label_;
349                 }
350         }
351
352         temp += ": ";
353
354         if (params_["filename"].empty())
355                 temp += "???";
356         else
357                 temp += from_utf8(onlyFilename(to_utf8(params_["filename"])));
358
359         return temp;
360 }
361
362
363 namespace {
364
365 /// return the child buffer if the file is a LyX doc and is loaded
366 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
367 {
368         if (isVerbatim(params) || isListings(params))
369                 return 0;
370
371         string const included_file = includedFilename(buffer, params).absFilename();
372         if (!isLyXFilename(included_file))
373                 return 0;
374
375         Buffer * childBuffer = theBufferList().getBuffer(included_file);
376
377         //FIXME RECURSIVE INCLUDES
378         if (childBuffer == & buffer)
379                 return 0;
380         else
381                 return childBuffer;
382 }
383
384 } // namespace anon
385
386
387 /// return true if the file is or got loaded.
388 Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params)
389 {
390         if (isVerbatim(params) || isListings(params))
391                 return 0;
392
393         string const parent_filename = parent.absFileName();
394         FileName const included_file = makeAbsPath(to_utf8(params["filename"]),
395                            onlyPath(parent_filename));
396
397         if (!isLyXFilename(included_file.absFilename()))
398                 return 0;
399
400         Buffer * child = theBufferList().getBuffer(included_file.absFilename());
401         if (!child) {
402                 // the readonly flag can/will be wrong, not anymore I think.
403                 if (!included_file.exists())
404                         return 0;
405
406                 child = theBufferList().newBuffer(included_file.absFilename());
407                 if (!loadLyXFile(child, included_file)) {
408                         //close the buffer we just opened
409                         theBufferList().close(child, false);
410                         return 0;
411                 }
412         }
413         child->setParentName(parent_filename);
414         return child;
415 }
416
417
418 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
419                         OutputParams const & runparams) const
420 {
421         string incfile(to_utf8(params_["filename"]));
422
423         // Do nothing if no file name has been specified
424         if (incfile.empty())
425                 return 0;
426
427         FileName const included_file = includedFilename(buffer, params_);
428
429         //Check we're not trying to include ourselves.
430         //FIXME RECURSIVE INCLUDE
431         //This isn't sufficient, as the inclusion could be downstream.
432         //But it'll have to do for now.
433         if (isInputOrInclude(params_) &&
434                 buffer.absFileName() == included_file.absFilename())
435         {
436                 Alert::error(_("Recursive input"),
437                                bformat(_("Attempted to include file %1$s in itself! "
438                                "Ignoring inclusion."), from_utf8(incfile)));
439                 return 0;
440         }
441
442         Buffer const * const masterBuffer = buffer.masterBuffer();
443
444         // if incfile is relative, make it relative to the master
445         // buffer directory.
446         if (!absolutePath(incfile)) {
447                 // FIXME UNICODE
448                 incfile = to_utf8(makeRelPath(from_utf8(included_file.absFilename()),
449                                               from_utf8(masterBuffer->filePath())));
450         }
451
452         // write it to a file (so far the complete file)
453         string const exportfile = changeExtension(incfile, ".tex");
454         string const mangled =
455                 DocFileName(changeExtension(included_file.absFilename(),".tex")).
456                         mangledFilename();
457         FileName const writefile(makeAbsPath(mangled, masterBuffer->temppath()));
458
459         if (!runparams.nice)
460                 incfile = mangled;
461         else if (!isValidLaTeXFilename(incfile)) {
462                 frontend::Alert::warning(_("Invalid filename"),
463                                          _("The following filename is likely to cause trouble "
464                                            "when running the exported file through LaTeX: ") +
465                                             from_utf8(incfile));
466         }
467         LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
468         LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
469         LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
470
471         if (runparams.inComment || runparams.dryrun) {
472                 //Don't try to load or copy the file if we're
473                 //in a comment or doing a dryrun
474         } else if (isInputOrInclude(params_) &&
475                  isLyXFilename(included_file.absFilename())) {
476                 //if it's a LyX file and we're inputting or including,
477                 //try to load it so we can write the associated latex
478                 if (!loadIfNeeded(buffer, params_))
479                         return false;
480
481                 Buffer * tmp = theBufferList().getBuffer(included_file.absFilename());
482
483                 if (tmp->params().getBaseClass() != masterBuffer->params().getBaseClass()) {
484                         // FIXME UNICODE
485                         docstring text = bformat(_("Included file `%1$s'\n"
486                                                 "has textclass `%2$s'\n"
487                                                              "while parent file has textclass `%3$s'."),
488                                               makeDisplayPath(included_file.absFilename()),
489                                               from_utf8(tmp->params().getTextClass().name()),
490                                               from_utf8(masterBuffer->params().getTextClass().name()));
491                         Alert::warning(_("Different textclasses"), text);
492                         //return 0;
493                 }
494
495                 // Make sure modules used in child are all included in master
496                 //FIXME It might be worth loading the children's modules into the master
497                 //over in BufferParams rather than doing this check.
498                 vector<string> const masterModules = masterBuffer->params().getModules();
499                 vector<string> const childModules = tmp->params().getModules();
500                 vector<string>::const_iterator it = childModules.begin();
501                 vector<string>::const_iterator end = childModules.end();
502                 for (; it != end; ++it) {
503                         string const module = *it;
504                         vector<string>::const_iterator found =
505                                 find(masterModules.begin(), masterModules.end(), module);
506                         if (found != masterModules.end()) {
507                                 docstring text = bformat(_("Included file `%1$s'\n"
508                                                         "uses module `%2$s'\n"
509                                                         "which is not used in parent file."),
510                                        makeDisplayPath(included_file.absFilename()), from_utf8(module));
511                                 Alert::warning(_("Module not found"), text);
512                         }
513                 }
514
515                 tmp->markDepClean(masterBuffer->temppath());
516
517 // FIXME: handle non existing files
518 // FIXME: Second argument is irrelevant!
519 // since only_body is true, makeLaTeXFile will not look at second
520 // argument. Should we set it to string(), or should makeLaTeXFile
521 // make use of it somehow? (JMarc 20031002)
522                 // The included file might be written in a different encoding
523                 Encoding const * const oldEnc = runparams.encoding;
524                 runparams.encoding = &tmp->params().encoding();
525                 tmp->makeLaTeXFile(writefile,
526                                    onlyPath(masterFilename(buffer)),
527                                    runparams, false);
528                 runparams.encoding = oldEnc;
529         } else {
530                 // In this case, it's not a LyX file, so we copy the file
531                 // to the temp dir, so that .aux files etc. are not created
532                 // in the original dir. Files included by this file will be
533                 // found via input@path, see ../Buffer.cpp.
534                 unsigned long const checksum_in  = sum(included_file);
535                 unsigned long const checksum_out = sum(writefile);
536
537                 if (checksum_in != checksum_out) {
538                         if (!copy(included_file, writefile)) {
539                                 // FIXME UNICODE
540                                 LYXERR(Debug::LATEX)
541                                         << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
542                                                                   "into the temporary directory."),
543                                                    from_utf8(included_file.absFilename())))
544                                         << endl;
545                                 return 0;
546                         }
547                 }
548         }
549
550         string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
551                         "latex" : "pdflatex";
552         if (isVerbatim(params_)) {
553                 incfile = latex_path(incfile);
554                 // FIXME UNICODE
555                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
556                    << from_utf8(incfile) << '}';
557         } else if (type(params_) == INPUT) {
558                 runparams.exportdata->addExternalFile(tex_format, writefile,
559                                                       exportfile);
560
561                 // \input wants file with extension (default is .tex)
562                 if (!isLyXFilename(included_file.absFilename())) {
563                         incfile = latex_path(incfile);
564                         // FIXME UNICODE
565                         os << '\\' << from_ascii(params_.getCmdName())
566                            << '{' << from_utf8(incfile) << '}';
567                 } else {
568                 incfile = changeExtension(incfile, ".tex");
569                 incfile = latex_path(incfile);
570                         // FIXME UNICODE
571                         os << '\\' << from_ascii(params_.getCmdName())
572                            << '{' << from_utf8(incfile) <<  '}';
573                 }
574         } else if (type(params_) == LISTINGS) {
575                 os << '\\' << from_ascii(params_.getCmdName());
576                 string opt = params_.getOptions();
577                 // opt is set in QInclude dialog and should have passed validation.
578                 InsetListingsParams params(opt);
579                 if (!params.params().empty())
580                         os << "[" << from_utf8(params.params()) << "]";
581                 os << '{'  << from_utf8(incfile) << '}';
582         } else {
583                 runparams.exportdata->addExternalFile(tex_format, writefile,
584                                                       exportfile);
585
586                 // \include don't want extension and demands that the
587                 // file really have .tex
588                 incfile = changeExtension(incfile, string());
589                 incfile = latex_path(incfile);
590                 // FIXME UNICODE
591                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
592                    << from_utf8(incfile) << '}';
593         }
594
595         return 0;
596 }
597
598
599 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
600                             OutputParams const &) const
601 {
602         if (isVerbatim(params_) || isListings(params_)) {
603                 os << '[' << getScreenLabel(buffer) << '\n';
604                 // FIXME: We don't know the encoding of the file
605                 docstring const str =
606                      from_utf8(getFileContents(includedFilename(buffer, params_)));
607                 os << str;
608                 os << "\n]";
609                 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
610         } else {
611                 docstring const str = '[' + getScreenLabel(buffer) + ']';
612                 os << str;
613                 return str.size();
614         }
615 }
616
617
618 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
619                           OutputParams const & runparams) const
620 {
621         string incfile = to_utf8(params_["filename"]);
622
623         // Do nothing if no file name has been specified
624         if (incfile.empty())
625                 return 0;
626
627         string const included_file = includedFilename(buffer, params_).absFilename();
628
629         //Check we're not trying to include ourselves.
630         //FIXME RECURSIVE INCLUDE
631         //This isn't sufficient, as the inclusion could be downstream.
632         //But it'll have to do for now.
633         if (buffer.absFileName() == included_file) {
634                 Alert::error(_("Recursive input"),
635                                bformat(_("Attempted to include file %1$s in itself! "
636                                "Ignoring inclusion."), from_utf8(incfile)));
637                 return 0;
638         }
639
640         // write it to a file (so far the complete file)
641         string const exportfile = changeExtension(incfile, ".sgml");
642         DocFileName writefile(changeExtension(included_file, ".sgml"));
643
644         if (loadIfNeeded(buffer, params_)) {
645                 Buffer * tmp = theBufferList().getBuffer(included_file);
646
647                 string const mangled = writefile.mangledFilename();
648                 writefile = makeAbsPath(mangled,
649                                         buffer.masterBuffer()->temppath());
650                 if (!runparams.nice)
651                         incfile = mangled;
652
653                 LYXERR(Debug::LATEX) << "incfile:" << incfile << endl;
654                 LYXERR(Debug::LATEX) << "exportfile:" << exportfile << endl;
655                 LYXERR(Debug::LATEX) << "writefile:" << writefile << endl;
656
657                 tmp->makeDocBookFile(writefile, runparams, true);
658         }
659
660         runparams.exportdata->addExternalFile("docbook", writefile,
661                                               exportfile);
662         runparams.exportdata->addExternalFile("docbook-xml", writefile,
663                                               exportfile);
664
665         if (isVerbatim(params_) || isListings(params_)) {
666                 os << "<inlinegraphic fileref=\""
667                    << '&' << include_label << ';'
668                    << "\" format=\"linespecific\">";
669         } else
670                 os << '&' << include_label << ';';
671
672         return 0;
673 }
674
675
676 void InsetInclude::validate(LaTeXFeatures & features) const
677 {
678         string incfile(to_utf8(params_["filename"]));
679         string writefile;
680
681         Buffer const & buffer = features.buffer();
682
683         string const included_file = includedFilename(buffer, params_).absFilename();
684
685         if (isLyXFilename(included_file))
686                 writefile = changeExtension(included_file, ".sgml");
687         else
688                 writefile = included_file;
689
690         if (!features.runparams().nice && !isVerbatim(params_) && !isListings(params_)) {
691                 incfile = DocFileName(writefile).mangledFilename();
692                 writefile = makeAbsPath(incfile,
693                                         buffer.masterBuffer()->temppath()).absFilename();
694         }
695
696         features.includeFile(include_label, writefile);
697
698         if (isVerbatim(params_))
699                 features.require("verbatim");
700         else if (isListings(params_))
701                 features.require("listings");
702
703         // Here we must do the fun stuff...
704         // Load the file in the include if it needs
705         // to be loaded:
706         if (loadIfNeeded(buffer, params_)) {
707                 // a file got loaded
708                 Buffer * const tmp = theBufferList().getBuffer(included_file);
709                 // make sure the buffer isn't us
710                 // FIXME RECURSIVE INCLUDES
711                 // This is not sufficient, as recursive includes could be
712                 // more than a file away. But it will do for now.
713                 if (tmp && tmp != & buffer) {
714                         // We must temporarily change features.buffer,
715                         // otherwise it would always be the master buffer,
716                         // and nested includes would not work.
717                         features.setBuffer(*tmp);
718                         tmp->validate(features);
719                         features.setBuffer(buffer);
720                 }
721         }
722 }
723
724
725 void InsetInclude::getLabelList(Buffer const & buffer,
726                                 std::vector<docstring> & list) const
727 {
728         if (isListings(params_)) {
729                 InsetListingsParams params(params_.getOptions());
730                 string label = params.getParamValue("label");
731                 if (!label.empty())
732                         list.push_back(from_utf8(label));
733         }
734         else if (loadIfNeeded(buffer, params_)) {
735                 string const included_file = includedFilename(buffer, params_).absFilename();
736                 Buffer * tmp = theBufferList().getBuffer(included_file);
737                 tmp->setParentName("");
738                 tmp->getLabelList(list);
739                 tmp->setParentName(parentFilename(buffer));
740         }
741 }
742
743
744 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
745                 BiblioInfo & keys, InsetIterator const & /*di*/) const
746 {
747         if (loadIfNeeded(buffer, params_)) {
748                 string const included_file = includedFilename(buffer, params_).absFilename();
749                 Buffer * tmp = theBufferList().getBuffer(included_file);
750                 //FIXME This is kind of a dirty hack and should be made reasonable.
751                 tmp->setParentName("");
752                 keys.fillWithBibKeys(tmp);
753                 tmp->setParentName(parentFilename(buffer));
754         }
755 }
756
757
758 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
759 {
760         Buffer * const tmp = getChildBuffer(buffer, params_);
761         if (tmp) {
762                 tmp->setParentName("");
763                 tmp->updateBibfilesCache();
764                 tmp->setParentName(parentFilename(buffer));
765         }
766 }
767
768
769 std::vector<FileName> const &
770 InsetInclude::getBibfilesCache(Buffer const & buffer) const
771 {
772         Buffer * const tmp = getChildBuffer(buffer, params_);
773         if (tmp) {
774                 tmp->setParentName("");
775                 std::vector<FileName> const & cache = tmp->getBibfilesCache();
776                 tmp->setParentName(parentFilename(buffer));
777                 return cache;
778         }
779         static std::vector<FileName> const empty;
780         return empty;
781 }
782
783
784 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
785 {
786         BOOST_ASSERT(mi.base.bv);
787
788         bool use_preview = false;
789         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
790                 graphics::PreviewImage const * pimage =
791                         preview_->getPreviewImage(mi.base.bv->buffer());
792                 use_preview = pimage && pimage->image();
793         }
794
795         if (use_preview) {
796                 preview_->metrics(mi, dim);
797         } else {
798                 if (!set_label_) {
799                         set_label_ = true;
800                         button_.update(getScreenLabel(mi.base.bv->buffer()),
801                                        true);
802                 }
803                 button_.metrics(mi, dim);
804         }
805
806         Box b(0, dim.wid, -dim.asc, dim.des);
807         button_.setBox(b);
808 }
809
810
811 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
812 {
813         BOOST_ASSERT(pi.base.bv);
814
815         bool use_preview = false;
816         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
817                 graphics::PreviewImage const * pimage =
818                         preview_->getPreviewImage(pi.base.bv->buffer());
819                 use_preview = pimage && pimage->image();
820         }
821
822         if (use_preview)
823                 preview_->draw(pi, x, y);
824         else
825                 button_.draw(pi, x, y);
826 }
827
828
829 Inset::DisplayType InsetInclude::display() const
830 {
831         return type(params_) == INPUT ? Inline : AlignCenter;
832 }
833
834
835
836 //
837 // preview stuff
838 //
839
840 void InsetInclude::fileChanged() const
841 {
842         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
843         if (!buffer_ptr)
844                 return;
845
846         Buffer const & buffer = *buffer_ptr;
847         preview_->removePreview(buffer);
848         add_preview(*preview_.get(), *this, buffer);
849         preview_->startLoading(buffer);
850 }
851
852
853 namespace {
854
855 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
856 {
857         FileName const included_file = includedFilename(buffer, params);
858
859         return type(params) == INPUT && params.preview() &&
860                 included_file.isFileReadable();
861 }
862
863
864 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
865 {
866         odocstringstream os;
867         // We don't need to set runparams.encoding since this will be done
868         // by latex() anyway.
869         OutputParams runparams(0);
870         runparams.flavor = OutputParams::LATEX;
871         inset.latex(buffer, os, runparams);
872
873         return os.str();
874 }
875
876
877 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
878                  Buffer const & buffer)
879 {
880         InsetCommandParams const & params = inset.params();
881         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
882             preview_wanted(params, buffer)) {
883                 renderer.setAbsFile(includedFilename(buffer, params));
884                 docstring const snippet = latex_string(inset, buffer);
885                 renderer.addPreview(snippet, buffer);
886         }
887 }
888
889 } // namespace anon
890
891
892 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
893 {
894         Buffer const & buffer = ploader.buffer();
895         if (preview_wanted(params(), buffer)) {
896                 preview_->setAbsFile(includedFilename(buffer, params()));
897                 docstring const snippet = latex_string(*this, buffer);
898                 preview_->addPreview(snippet, ploader);
899         }
900 }
901
902
903 void InsetInclude::addToToc(TocList & toclist, Buffer const & buffer, ParConstIterator const & pit) const
904 {
905         if (isListings(params_)) {
906                 InsetListingsParams params(params_.getOptions());
907                 string caption = params.getParamValue("caption");
908                 if (!caption.empty()) {
909                         Toc & toc = toclist["listing"];
910                         docstring const str = convert<docstring>(toc.size() + 1)
911                                 + ". " +  from_utf8(caption);
912                         // This inset does not have a valid ParConstIterator
913                         // so it has to use the iterator of its parent paragraph
914                         toc.push_back(TocItem(pit, 0, str));
915                 }
916                 return;
917         }
918         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
919         if (!childbuffer)
920                 return;
921
922         TocList const & childtoclist = childbuffer->tocBackend().tocs();
923         TocList::const_iterator it = childtoclist.begin();
924         TocList::const_iterator const end = childtoclist.end();
925         for(; it != end; ++it)
926                 toclist[it->first].insert(toclist[it->first].end(),
927                                 it->second.begin(), it->second.end());
928 }
929
930
931 void InsetInclude::updateLabels(Buffer const & buffer,
932                                 ParIterator const &)
933 {
934         Buffer const * const childbuffer = getChildBuffer(buffer, params_);
935         if (childbuffer)
936                 lyx::updateLabels(*childbuffer, true);
937         else if (isListings(params_)) {
938                 InsetListingsParams const par = params_.getOptions();
939                 if (par.getParamValue("caption").empty())
940                         listings_label_.clear();
941                 else {
942                         Counters & counters = buffer.params().getTextClass().counters();
943                         docstring const cnt = from_ascii("listing");
944                         if (counters.hasCounter(cnt)) {
945                                 counters.step(cnt);
946                                 listings_label_ = buffer.B_("Program Listing ") + convert<docstring>(counters.value(cnt));
947                         } else
948                                 listings_label_ = buffer.B_("Program Listing");
949                 }
950         }
951 }
952
953
954 void InsetInclude::registerEmbeddedFiles(Buffer const & buffer,
955         EmbeddedFiles & files) const
956 {
957         // include and input are temprarily not considered.
958         if (isVerbatim(params_) || isListings(params_))
959                 files.registerFile(includedFilename(buffer, params_).absFilename(),
960                         false, this);
961 }
962
963
964 string const InsetIncludeMailer::name_("include");
965
966 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
967         : inset_(inset)
968 {}
969
970
971 string const InsetIncludeMailer::inset2string(Buffer const &) const
972 {
973         return params2string(inset_.params());
974 }
975
976
977 void InsetIncludeMailer::string2params(string const & in,
978                                        InsetCommandParams & params)
979 {
980         params.clear();
981         if (in.empty())
982                 return;
983
984         istringstream data(in);
985         Lexer lex(0,0);
986         lex.setStream(data);
987
988         string name;
989         lex >> name;
990         if (!lex || name != name_)
991                 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
992
993         // This is part of the inset proper that is usually swallowed
994         // by Text::readInset
995         string id;
996         lex >> id;
997         if (!lex || id != "Include")
998                 return print_mailer_error("InsetIncludeMailer", in, 2, "Include");
999
1000         InsetInclude inset(params);
1001         inset.read(lex);
1002         params = inset.params();
1003 }
1004
1005
1006 string const
1007 InsetIncludeMailer::params2string(InsetCommandParams const & params)
1008 {
1009         InsetInclude inset(params);
1010         ostringstream data;
1011         data << name_ << ' ';
1012         inset.write(data);
1013         data << "\\end_inset\n";
1014         return data.str();
1015 }
1016
1017
1018 } // namespace lyx