3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
17 #include "BufferParams.h"
18 #include "BufferView.h"
20 #include "CutAndPaste.h"
21 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
28 #include "LyXAction.h"
29 #include "OutputParams.h"
30 #include "ParagraphParameters.h"
31 #include "Paragraph.h"
32 #include "TextClass.h"
34 #include "support/docstream.h"
35 #include "support/FileName.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/TempFile.h"
43 using namespace lyx::support;
47 InsetERT::InsetERT(Buffer * buf, CollapseStatus status)
48 : InsetCollapsible(buf)
54 // Do not copy the temp file on purpose: If a copy of an inset which is
55 // currently being edited is made, then we simply copy the current contents.
56 InsetERT::InsetERT(InsetERT const & that) : InsetCollapsible(that)
60 InsetERT & InsetERT::operator=(InsetERT const & that)
69 void InsetERT::write(ostream & os) const
72 InsetCollapsible::write(os);
76 int InsetERT::plaintext(odocstringstream & os,
77 OutputParams const & rp, size_t max_length) const
80 // do not output TeX code
83 ParagraphList::const_iterator par = paragraphs().begin();
84 ParagraphList::const_iterator end = paragraphs().end();
86 while (par != end && os.str().size() <= max_length) {
87 pos_type siz = par->size();
88 for (pos_type i = 0; i < siz; ++i) {
89 char_type const c = par->getChar(i);
90 // output the active characters
107 int InsetERT::docbook(odocstream & os, OutputParams const &) const
109 // FIXME can we do the same thing here as for LaTeX?
110 ParagraphList::const_iterator par = paragraphs().begin();
111 ParagraphList::const_iterator end = paragraphs().end();
115 pos_type siz = par->size();
116 for (pos_type i = 0; i < siz; ++i)
117 os.put(par->getChar(i));
129 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
131 switch (cmd.action()) {
132 case LFUN_INSET_EDIT: {
134 text().selectAll(cur);
135 string const format =
136 cur.buffer()->params().documentClass().outputFormat();
137 string const ext = theFormats().extension(format);
138 tempfile_.reset(new TempFile("ert_editXXXXXX." + ext));
139 FileName const tempfilename = tempfile_->name();
140 string const name = tempfilename.toFilesystemEncoding();
141 ofdocstream os(name.c_str());
142 os << cur.selectionAsString(false);
144 // Since we lock the inset while the external file is edited,
145 // we need to move the cursor outside and clear any selection inside
146 cur.clearSelection();
148 cur.leaveInset(*this);
149 theFormats().edit(buffer(), tempfilename, format);
152 case LFUN_INSET_END_EDIT: {
153 FileName const tempfilename = tempfile_->name();
154 docstring const s = tempfilename.fileContents("UTF-8");
155 cur.recordUndoInset(this);
157 text().selectAll(cur);
158 cap::replaceSelection(cur);
159 cur.text()->insertStringAsLines(cur, s, cur.current_font);
160 // FIXME it crashes without this
166 case LFUN_INSET_MODIFY:
167 if (cmd.getArg(0) == "ert") {
168 cur.recordUndoInset(this);
169 setStatus(cur, string2params(to_utf8(cmd.argument())));
174 InsetCollapsible::doDispatch(cur, cmd);
181 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
182 FuncStatus & status) const
184 switch (cmd.action()) {
185 case LFUN_INSET_EDIT:
186 status.setEnabled(tempfile_ == 0);
188 case LFUN_INSET_END_EDIT:
189 status.setEnabled(tempfile_ != 0);
191 case LFUN_INSET_INSERT:
192 status.setEnabled(false);
194 case LFUN_INSET_MODIFY:
195 if (cmd.getArg(0) == "ert") {
196 status.setEnabled(true);
202 return InsetCollapsible::getStatus(cur, cmd, status);
207 bool InsetERT::editable() const
211 return InsetCollapsible::editable();
215 bool InsetERT::descendable(BufferView const & bv) const
219 return InsetCollapsible::descendable(bv);
223 docstring const InsetERT::buttonLabel(BufferView const & bv) const
225 if (decoration() == InsetLayout::CLASSIC)
226 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
228 return getNewLabel(_("ERT"));
232 InsetCollapsible::CollapseStatus InsetERT::string2params(string const & in)
236 istringstream data(in);
239 lex.setContext("InsetERT::string2params");
243 return static_cast<CollapseStatus>(s);
247 string InsetERT::params2string(CollapseStatus status)
250 data << "ert" << ' ' << status;
255 docstring InsetERT::xhtml(XHTMLStream &, OutputParams const &) const