]> git.lyx.org Git - lyx.git/blob - src/insets/insetinclude.C
More unicode fixes for docbook.
[lyx.git] / src / insets / insetinclude.C
1 /**
2  * \file insetinclude.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 "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_main.h"
29 #include "lyxrc.h"
30 #include "lyxlex.h"
31 #include "metricsinfo.h"
32 #include "outputparams.h"
33
34 #include "frontends/Alert.h"
35 #include "frontends/Painter.h"
36
37 #include "graphics/PreviewImage.h"
38 #include "graphics/PreviewLoader.h"
39
40 #include "insets/render_preview.h"
41
42 #include "support/filename.h"
43 #include "support/filetools.h"
44 #include "support/lstrings.h" // contains
45 #include "support/lyxalgo.h"
46 #include "support/lyxlib.h"
47 #include "support/convert.h"
48
49 #include <boost/bind.hpp>
50 #include <boost/filesystem/operations.hpp>
51
52 #include "support/std_ostream.h"
53
54 #include <sstream>
55
56
57 namespace lyx {
58
59 using support::addName;
60 using support::absolutePath;
61 using support::bformat;
62 using support::changeExtension;
63 using support::contains;
64 using support::copy;
65 using support::FileName;
66 using support::getFileContents;
67 using support::isFileReadable;
68 using support::isLyXFilename;
69 using support::latex_path;
70 using support::makeAbsPath;
71 using support::makeDisplayPath;
72 using support::makeRelPath;
73 using support::onlyFilename;
74 using support::onlyPath;
75 using support::subst;
76 using support::sum;
77
78 using std::endl;
79 using std::string;
80 using std::auto_ptr;
81 using std::istringstream;
82 using std::ostream;
83 using std::ostringstream;
84
85 namespace Alert = frontend::Alert;
86 namespace fs = boost::filesystem;
87
88
89 namespace {
90
91 docstring const uniqueID()
92 {
93         static unsigned int seed = 1000;
94         return "file" + convert<docstring>(++seed);
95 }
96
97 } // namespace anon
98
99
100 InsetInclude::InsetInclude(InsetCommandParams const & p)
101         : params_(p), include_label(uniqueID()),
102           preview_(new RenderMonitoredPreview(this)),
103           set_label_(false)
104 {
105         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
106 }
107
108
109 InsetInclude::InsetInclude(InsetInclude const & other)
110         : InsetOld(other),
111           params_(other.params_),
112           include_label(other.include_label),
113           preview_(new RenderMonitoredPreview(this)),
114           set_label_(false)
115 {
116         preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
117 }
118
119
120 InsetInclude::~InsetInclude()
121 {
122         InsetIncludeMailer(*this).hideDialog();
123 }
124
125
126 void InsetInclude::doDispatch(LCursor & cur, FuncRequest & cmd)
127 {
128         switch (cmd.action) {
129
130         case LFUN_INSET_MODIFY: {
131                 InsetCommandParams p("include");
132                 InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p);
133                 if (!p.getCmdName().empty()) {
134                         set(p, cur.buffer());
135                         cur.buffer().updateBibfilesCache();
136                 } else
137                         cur.noUpdate();
138                 break;
139         }
140
141         case LFUN_INSET_DIALOG_UPDATE:
142                 InsetIncludeMailer(*this).updateDialog(&cur.bv());
143                 break;
144
145         case LFUN_MOUSE_RELEASE:
146                 InsetIncludeMailer(*this).showDialog(&cur.bv());
147                 break;
148
149         default:
150                 InsetBase::doDispatch(cur, cmd);
151                 break;
152         }
153 }
154
155
156 bool InsetInclude::getStatus(LCursor & cur, FuncRequest const & cmd,
157                 FuncStatus & flag) const
158 {
159         switch (cmd.action) {
160
161         case LFUN_INSET_MODIFY:
162         case LFUN_INSET_DIALOG_UPDATE:
163                 flag.enabled(true);
164                 return true;
165
166         default:
167                 return InsetBase::getStatus(cur, cmd, flag);
168         }
169 }
170
171
172 InsetCommandParams const & InsetInclude::params() const
173 {
174         return params_;
175 }
176
177
178 namespace {
179
180 /// the type of inclusion
181 enum Types {
182         INCLUDE = 0,
183         VERB = 1,
184         INPUT = 2,
185         VERBAST = 3
186 };
187
188
189 Types type(InsetCommandParams const & params)
190 {
191         string const command_name = params.getCmdName();
192
193         if (command_name == "input")
194                 return INPUT;
195         if  (command_name == "verbatiminput")
196                 return VERB;
197         if  (command_name == "verbatiminput*")
198                 return VERBAST;
199         return INCLUDE;
200 }
201
202
203 bool isVerbatim(InsetCommandParams const & params)
204 {
205         string const command_name = params.getCmdName();
206         return command_name == "verbatiminput" ||
207                 command_name == "verbatiminput*";
208 }
209
210
211 string const masterFilename(Buffer const & buffer)
212 {
213         return buffer.getMasterBuffer()->fileName();
214 }
215
216
217 string const parentFilename(Buffer const & buffer)
218 {
219         return buffer.fileName();
220 }
221
222
223 string const includedFilename(Buffer const & buffer,
224                               InsetCommandParams const & params)
225 {
226         return makeAbsPath(to_utf8(params["filename"]),
227                            onlyPath(parentFilename(buffer)));
228 }
229
230
231 void add_preview(RenderMonitoredPreview &, InsetInclude const &, Buffer const &);
232
233 } // namespace anon
234
235
236 void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
237 {
238         params_ = p;
239         set_label_ = false;
240
241         if (preview_->monitoring())
242                 preview_->stopMonitoring();
243
244         if (type(params_) == INPUT)
245                 add_preview(*preview_, *this, buffer);
246 }
247
248
249 auto_ptr<InsetBase> InsetInclude::doClone() const
250 {
251         return auto_ptr<InsetBase>(new InsetInclude(*this));
252 }
253
254
255 void InsetInclude::write(Buffer const &, ostream & os) const
256 {
257         write(os);
258 }
259
260
261 void InsetInclude::write(ostream & os) const
262 {
263         os << "Include " << to_utf8(params_.getCommand()) << '\n'
264            << "preview " << convert<string>(params_.preview()) << '\n';
265 }
266
267
268 void InsetInclude::read(Buffer const &, LyXLex & lex)
269 {
270         read(lex);
271 }
272
273
274 void InsetInclude::read(LyXLex & lex)
275 {
276         params_.read(lex);
277 }
278
279
280 docstring const InsetInclude::getScreenLabel(Buffer const &) const
281 {
282         docstring temp;
283
284         switch (type(params_)) {
285                 case INPUT:
286                         temp += _("Input");
287                         break;
288                 case VERB:
289                         temp += _("Verbatim Input");
290                         break;
291                 case VERBAST:
292                         temp += _("Verbatim Input*");
293                         break;
294                 case INCLUDE:
295                         temp += _("Include");
296                         break;
297         }
298
299         temp += ": ";
300
301         if (params_["filename"].empty())
302                 temp += "???";
303         else
304                 // FIXME: We don't know the encoding of the filename
305                 temp += from_ascii(onlyFilename(to_utf8(params_["filename"])));
306
307         return temp;
308 }
309
310
311 namespace {
312
313 /// return the child buffer if the file is a LyX doc and is loaded
314 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
315 {
316         if (isVerbatim(params))
317                 return 0;
318
319         string const included_file = includedFilename(buffer, params);
320         if (!isLyXFilename(included_file))
321                 return 0;
322
323         return theBufferList().getBuffer(included_file);
324 }
325
326
327 /// return true if the file is or got loaded.
328 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
329 {
330         if (isVerbatim(params))
331                 return false;
332
333         string const included_file = includedFilename(buffer, params);
334         if (!isLyXFilename(included_file))
335                 return false;
336
337         Buffer * buf = theBufferList().getBuffer(included_file);
338         if (!buf) {
339                 // the readonly flag can/will be wrong, not anymore I think.
340                 if (!fs::exists(included_file))
341                         return false;
342                 buf = theBufferList().newBuffer(included_file);
343                 if (!loadLyXFile(buf, included_file))
344                         return false;
345         }
346         if (buf)
347                 buf->setParentName(parentFilename(buffer));
348         return buf != 0;
349 }
350
351
352 } // namespace anon
353
354
355 int InsetInclude::latex(Buffer const & buffer, odocstream & os,
356                         OutputParams const & runparams) const
357 {
358         string incfile(to_utf8(params_["filename"]));
359
360         // Do nothing if no file name has been specified
361         if (incfile.empty())
362                 return 0;
363
364         string const included_file = includedFilename(buffer, params_);
365         Buffer const * const m_buffer = buffer.getMasterBuffer();
366
367         // if incfile is relative, make it relative to the master
368         // buffer directory.
369         if (!absolutePath(incfile)) {
370                 incfile = makeRelPath(included_file,
371                                       m_buffer->filePath());
372         }
373
374         // write it to a file (so far the complete file)
375         string const exportfile = changeExtension(incfile, ".tex");
376         string const mangled = FileName(changeExtension(included_file,
377                                                         ".tex")).mangledFilename();
378         string const writefile = makeAbsPath(mangled, m_buffer->temppath());
379
380         if (!runparams.nice)
381                 incfile = mangled;
382         lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
383         lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
384         lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
385
386         if (runparams.inComment || runparams.dryrun)
387                 // Don't try to load or copy the file
388                 ;
389         else if (loadIfNeeded(buffer, params_)) {
390                 Buffer * tmp = theBufferList().getBuffer(included_file);
391
392                 if (tmp->params().textclass != m_buffer->params().textclass) {
393                         // FIXME UNICODE
394                         docstring text = bformat(_("Included file `%1$s'\n"
395                                                 "has textclass `%2$s'\n"
396                                                              "while parent file has textclass `%3$s'."),
397                                               makeDisplayPath(included_file),
398                                               from_utf8(tmp->params().getLyXTextClass().name()),
399                                               from_utf8(m_buffer->params().getLyXTextClass().name()));
400                         Alert::warning(_("Different textclasses"), text);
401                         //return 0;
402                 }
403
404                 tmp->markDepClean(m_buffer->temppath());
405
406 #ifdef WITH_WARNINGS
407 #warning handle non existing files
408 #warning Second argument is irrelevant!
409 // since only_body is true, makeLaTeXFile will not look at second
410 // argument. Should we set it to string(), or should makeLaTeXFile
411 // make use of it somehow? (JMarc 20031002)
412 #endif
413                 tmp->makeLaTeXFile(writefile,
414                                    onlyPath(masterFilename(buffer)),
415                                    runparams, false);
416         } else {
417                 // Copy the file to the temp dir, so that .aux files etc.
418                 // are not created in the original dir. Files included by
419                 // this file will be found via input@path, see ../buffer.C.
420                 unsigned long const checksum_in  = sum(included_file);
421                 unsigned long const checksum_out = sum(writefile);
422
423                 if (checksum_in != checksum_out) {
424                         if (!copy(included_file, writefile)) {
425                                 // FIXME UNICODE
426                                 lyxerr[Debug::LATEX]
427                                         << to_utf8(bformat(_("Could not copy the file\n%1$s\n"
428                                                                   "into the temporary directory."),
429                                                    from_utf8(included_file)))
430                                         << endl;
431                                 return 0;
432                         }
433                 }
434         }
435
436         string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
437                         "latex" : "pdflatex";
438         if (isVerbatim(params_)) {
439                 incfile = latex_path(incfile);
440                 // FIXME UNICODE
441                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
442                    << from_utf8(incfile) << '}';
443         } else if (type(params_) == INPUT) {
444                 runparams.exportdata->addExternalFile(tex_format, writefile,
445                                                       exportfile);
446
447                 // \input wants file with extension (default is .tex)
448                 if (!isLyXFilename(included_file)) {
449                         incfile = latex_path(incfile);
450                         // FIXME UNICODE
451                         os << '\\' << from_ascii(params_.getCmdName())
452                            << '{' << from_utf8(incfile) << '}';
453                 } else {
454                 incfile = changeExtension(incfile, ".tex");
455                 incfile = latex_path(incfile);
456                         // FIXME UNICODE
457                         os << '\\' << from_ascii(params_.getCmdName())
458                            << '{' << from_utf8(incfile) <<  '}';
459                 }
460         } else {
461                 runparams.exportdata->addExternalFile(tex_format, writefile,
462                                                       exportfile);
463
464                 // \include don't want extension and demands that the
465                 // file really have .tex
466                 incfile = changeExtension(incfile, string());
467                 incfile = latex_path(incfile);
468                 // FIXME UNICODE
469                 os << '\\' << from_ascii(params_.getCmdName()) << '{'
470                    << from_utf8(incfile) << '}';
471         }
472
473         return 0;
474 }
475
476
477 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
478                         OutputParams const &) const
479 {
480         if (isVerbatim(params_)) {
481                 // FIXME: We don't know the encoding of the file
482                 docstring const str = from_utf8(
483                         getFileContents(includedFilename(buffer, params_)));
484                 os << str;
485                 // Return how many newlines we issued.
486                 return int(lyx::count(str.begin(), str.end(), '\n'));
487         }
488         return 0;
489 }
490
491
492 int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
493                           OutputParams const & runparams) const
494 {
495         string incfile = to_utf8(params_["filename"]);
496
497         // Do nothing if no file name has been specified
498         if (incfile.empty())
499                 return 0;
500
501         string const included_file = includedFilename(buffer, params_);
502
503         // write it to a file (so far the complete file)
504         string const exportfile = changeExtension(incfile, ".sgml");
505         string writefile = changeExtension(included_file, ".sgml");
506
507         if (loadIfNeeded(buffer, params_)) {
508                 Buffer * tmp = theBufferList().getBuffer(included_file);
509
510                 string const mangled = FileName(writefile).mangledFilename();
511                 writefile = makeAbsPath(mangled,
512                                         buffer.getMasterBuffer()->temppath());
513                 if (!runparams.nice)
514                         incfile = mangled;
515
516                 lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
517                 lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
518                 lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
519
520                 tmp->makeDocBookFile(writefile, runparams, true);
521         }
522
523         runparams.exportdata->addExternalFile("docbook", writefile,
524                                               exportfile);
525         runparams.exportdata->addExternalFile("docbook-xml", writefile,
526                                               exportfile);
527
528         // FIXME UNICODE
529         if (isVerbatim(params_)) {
530                 os << "<inlinegraphic fileref=\""
531                    << '&' << include_label << ';'
532                    << "\" format=\"linespecific\">";
533         } else
534                 os << '&' << include_label << ';';
535
536         return 0;
537 }
538
539
540 void InsetInclude::validate(LaTeXFeatures & features) const
541 {
542         string incfile(to_utf8(params_["filename"]));
543         string writefile;
544
545         Buffer const & buffer = features.buffer();
546
547         string const included_file = includedFilename(buffer, params_);
548
549         if (isLyXFilename(included_file))
550                 writefile = changeExtension(included_file, ".sgml");
551         else
552                 writefile = included_file;
553
554         if (!features.runparams().nice && !isVerbatim(params_)) {
555                 incfile = FileName(writefile).mangledFilename();
556                 writefile = makeAbsPath(incfile,
557                                         buffer.getMasterBuffer()->temppath());
558         }
559
560         features.includeFile(include_label, writefile);
561
562         if (isVerbatim(params_))
563                 features.require("verbatim");
564
565         // Here we must do the fun stuff...
566         // Load the file in the include if it needs
567         // to be loaded:
568         if (loadIfNeeded(buffer, params_)) {
569                 // a file got loaded
570                 Buffer * const tmp = theBufferList().getBuffer(included_file);
571                 if (tmp) {
572                         // We must temporarily change features.buffer,
573                         // otherwise it would always be the master buffer,
574                         // and nested includes would not work.
575                         features.setBuffer(*tmp);
576                         tmp->validate(features);
577                         features.setBuffer(buffer);
578                 }
579         }
580 }
581
582
583 void InsetInclude::getLabelList(Buffer const & buffer,
584                                 std::vector<docstring> & list) const
585 {
586         if (loadIfNeeded(buffer, params_)) {
587                 string const included_file = includedFilename(buffer, params_);
588                 Buffer * tmp = theBufferList().getBuffer(included_file);
589                 tmp->setParentName("");
590                 tmp->getLabelList(list);
591                 tmp->setParentName(parentFilename(buffer));
592         }
593 }
594
595
596 void InsetInclude::fillWithBibKeys(Buffer const & buffer,
597                                    std::vector<std::pair<string,string> > & keys) const
598 {
599         if (loadIfNeeded(buffer, params_)) {
600                 string const included_file = includedFilename(buffer, params_);
601                 Buffer * tmp = theBufferList().getBuffer(included_file);
602                 tmp->setParentName("");
603                 tmp->fillWithBibKeys(keys);
604                 tmp->setParentName(parentFilename(buffer));
605         }
606 }
607
608
609 void InsetInclude::updateBibfilesCache(Buffer const & buffer)
610 {
611         Buffer * const tmp = getChildBuffer(buffer, params_);
612         if (tmp) {
613                 tmp->setParentName("");
614                 tmp->updateBibfilesCache();
615                 tmp->setParentName(parentFilename(buffer));
616         }
617 }
618
619
620 std::vector<string> const &
621 InsetInclude::getBibfilesCache(Buffer const & buffer) const
622 {
623         Buffer * const tmp = getChildBuffer(buffer, params_);
624         if (tmp) {
625                 tmp->setParentName("");
626                 std::vector<string> const & cache = tmp->getBibfilesCache();
627                 tmp->setParentName(parentFilename(buffer));
628                 return cache;
629         }
630         static std::vector<string> const empty;
631         return empty;
632 }
633
634
635 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
636 {
637         BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
638
639         bool use_preview = false;
640         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
641                 graphics::PreviewImage const * pimage =
642                         preview_->getPreviewImage(*mi.base.bv->buffer());
643                 use_preview = pimage && pimage->image();
644         }
645
646         if (use_preview) {
647                 preview_->metrics(mi, dim);
648         } else {
649                 if (!set_label_) {
650                         set_label_ = true;
651                         button_.update(getScreenLabel(*mi.base.bv->buffer()),
652                                        true);
653                 }
654                 button_.metrics(mi, dim);
655         }
656
657         Box b(0, dim.wid, -dim.asc, dim.des);
658         button_.setBox(b);
659
660         dim_ = dim;
661 }
662
663
664 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
665 {
666         setPosCache(pi, x, y);
667
668         BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
669
670         bool use_preview = false;
671         if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
672                 graphics::PreviewImage const * pimage =
673                         preview_->getPreviewImage(*pi.base.bv->buffer());
674                 use_preview = pimage && pimage->image();
675         }
676
677         if (use_preview)
678                 preview_->draw(pi, x, y);
679         else
680                 button_.draw(pi, x, y);
681 }
682
683 bool InsetInclude::display() const
684 {
685         return type(params_) != INPUT;
686 }
687
688
689
690 //
691 // preview stuff
692 //
693
694 void InsetInclude::fileChanged() const
695 {
696         Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
697         if (!buffer_ptr)
698                 return;
699
700         Buffer const & buffer = *buffer_ptr;
701         preview_->removePreview(buffer);
702         add_preview(*preview_.get(), *this, buffer);
703         preview_->startLoading(buffer);
704 }
705
706
707 namespace {
708
709 bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
710 {
711         string const included_file = includedFilename(buffer, params);
712
713         return type(params) == INPUT && params.preview() &&
714                 isFileReadable(included_file);
715 }
716
717
718 docstring const latex_string(InsetInclude const & inset, Buffer const & buffer)
719 {
720         odocstringstream os;
721         OutputParams runparams;
722         runparams.flavor = OutputParams::LATEX;
723         inset.latex(buffer, os, runparams);
724
725         return os.str();
726 }
727
728
729 void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
730                  Buffer const & buffer)
731 {
732         InsetCommandParams const & params = inset.params();
733         if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
734             preview_wanted(params, buffer)) {
735                 renderer.setAbsFile(includedFilename(buffer, params));
736                 docstring const snippet = latex_string(inset, buffer);
737                 renderer.addPreview(snippet, buffer);
738         }
739 }
740
741 } // namespace anon
742
743
744 void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
745 {
746         Buffer const & buffer = ploader.buffer();
747         if (preview_wanted(params(), buffer)) {
748                 preview_->setAbsFile(includedFilename(buffer, params()));
749                 docstring const snippet = latex_string(*this, buffer);
750                 preview_->addPreview(snippet, ploader);
751         }
752 }
753
754
755 string const InsetIncludeMailer::name_("include");
756
757 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
758         : inset_(inset)
759 {}
760
761
762 string const InsetIncludeMailer::inset2string(Buffer const &) const
763 {
764         return params2string(inset_.params());
765 }
766
767
768 void InsetIncludeMailer::string2params(string const & in,
769                                        InsetCommandParams & params)
770 {
771         params.clear();
772         if (in.empty())
773                 return;
774
775         istringstream data(in);
776         LyXLex lex(0,0);
777         lex.setStream(data);
778
779         string name;
780         lex >> name;
781         if (!lex || name != name_)
782                 return print_mailer_error("InsetIncludeMailer", in, 1, name_);
783
784         // This is part of the inset proper that is usually swallowed
785         // by LyXText::readInset
786         string id;
787         lex >> id;
788         if (!lex || id != "Include")
789                 return print_mailer_error("InsetIncludeMailer", in, 2, "Include");
790
791         InsetInclude inset(params);
792         inset.read(lex);
793         params = inset.params();
794 }
795
796
797 string const
798 InsetIncludeMailer::params2string(InsetCommandParams const & params)
799 {
800         InsetInclude inset(params);
801         ostringstream data;
802         data << name_ << ' ';
803         inset.write(data);
804         data << "\\end_inset\n";
805         return data.str();
806 }
807
808
809 } // namespace lyx