#include "InsetListings.h"
#include "InsetCaption.h"
+#include "Buffer.h"
+#include "BufferParams.h"
+#include "Counters.h"
#include "Language.h"
#include "gettext.h"
#include "DispatchResult.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
+#include "MetricsInfo.h"
#include "Cursor.h"
#include "support/lstrings.h"
namespace lyx {
using support::token;
+using support::contains;
+using support::subst;
using std::auto_ptr;
using std::istringstream;
using std::ostringstream;
using std::string;
+char const lstinline_delimiters[] = "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
void InsetListings::init()
{
setButtonLabel();
- // FIXME: define Color::listing?
Font font(Font::ALL_SANE);
font.decSize();
font.decSize();
- font.setColor(Color::foreground);
+ font.setColor(Color::none);
setLabelFont(font);
- // FIXME: english_language?
- text_.current_font.setLanguage(english_language);
- text_.real_current_font.setLanguage(english_language);
- // FIXME: why I can not make text of source code black with the following two lines?
- text_.current_font.setColor(Color::foreground);
- text_.real_current_font.setColor(Color::foreground);
+ text_.current_font.setLanguage(latex_language);
+ text_.real_current_font.setLanguage(latex_language);
}
}
+void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
+{
+ Counters & cnts = buf.params().getTextClass().counters();
+ string const saveflt = cnts.current_float();
+
+ // Tell to captions what the current float is
+ cnts.current_float("listing");
+
+ InsetCollapsable::updateLabels(buf, it);
+
+ //reset afterwards
+ cnts.current_float(saveflt);
+}
+
+
void InsetListings::write(Buffer const & buf, ostream & os) const
{
os << "listings" << "\n";
break;
}
}
- InsetCollapsable::read(buf, lex);
+ InsetERT::read(buf, lex);
}
docstring const InsetListings::editMessage() const
{
- return _("Opened Listings Inset");
+ return _("Opened Listing Inset");
}
int InsetListings::latex(Buffer const & buf, odocstream & os,
OutputParams const & runparams) const
{
- string param_string = params().encodedString();
+ string param_string = params().params();
// NOTE: I use {} to quote text, which is an experimental feature
// of the listings package (see page 25 of the manual)
int lines = 0;
- bool lstinline = params().isInline();
- if (lstinline) {
- if (param_string.empty())
- os << "\\lstinline{";
- else
- os << "\\lstinline[" << from_ascii(param_string) << "]{";
- } else {
- docstring const caption = getCaption(buf, runparams);
- if (param_string.empty() && caption.empty())
- os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
- else {
- os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
- if (!caption.empty()) {
- os << "caption={" << caption << '}';
- if (!param_string.empty())
- os << ',';
- }
- os << from_ascii(param_string) << "]\n";
- }
- lines += 4;
- }
+ bool isInline = params().isInline();
+ // get the paragraphs. We can not output them directly to given odocstream
+ // because we can not yet determine the delimiter character of \lstinline
+ docstring code;
ParagraphList::const_iterator par = paragraphs().begin();
ParagraphList::const_iterator end = paragraphs().end();
// ignore all struck out text and (caption) insets
if (par->isDeleted(i) || par->isInset(i))
continue;
- os.put(par->getChar(i));
+ code += par->getChar(i);
}
++par;
// for the inline case, if there are multiple paragraphs
// they are simply joined. Otherwise, expect latex errors.
- if (par != end && !lstinline && !captionline) {
- os << "\n";
+ if (par != end && !isInline && !captionline) {
+ code += "\n";
++lines;
}
}
- if (lstinline)
- os << "}";
- else {
- os << "\n\\end{lstlisting}\n\\endgroup\n";
+ if (isInline) {
+ char const * delimiter = lstinline_delimiters;
+ for (; delimiter != '\0'; ++delimiter)
+ if (!contains(code, *delimiter))
+ break;
+ // This code piece contains all possible special character? !!!
+ // Replace ! with a warning message and use ! as delimiter.
+ if (*delimiter == '\0') {
+ code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
+ delimiter = lstinline_delimiters;
+ }
+ if (param_string.empty())
+ os << "\\lstinline" << *delimiter;
+ else
+ os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
+ os << code
+ << *delimiter;
+ } else {
+ docstring const caption = getCaption(buf, runparams);
+ if (param_string.empty() && caption.empty())
+ os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
+ else {
+ os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
+ if (!caption.empty()) {
+ os << "caption={" << caption << '}';
+ if (!param_string.empty())
+ os << ',';
+ }
+ os << from_utf8(param_string) << "]\n";
+ }
+ lines += 4;
+ os << code
+ << "\n\\end{lstlisting}\n\\endgroup\n";
lines += 3;
}
}
case LFUN_INSET_DIALOG_UPDATE:
InsetListingsMailer(*this).updateDialog(&cur.bv());
- break;
+ break;
case LFUN_MOUSE_RELEASE: {
if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
InsetListingsMailer(*this).showDialog(&cur.bv());
void InsetListings::setButtonLabel()
{
// FIXME UNICODE
- setLabel(isOpen() ? _("Listings") : getNewLabel(_("Listings")));
+ setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
+}
+
+
+bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+ Font tmpfont = mi.base.font;
+ getDrawFont(mi.base.font);
+ mi.base.font.realize(tmpfont);
+ InsetCollapsable::metrics(mi, dim);
+ mi.base.font = tmpfont;
+ bool const changed = dim_ != dim;
+ dim_ = dim;
+ return changed;
+}
+
+
+void InsetListings::draw(PainterInfo & pi, int x, int y) const
+{
+ Font tmpfont = pi.base.font;
+ getDrawFont(pi.base.font);
+ pi.base.font.realize(tmpfont);
+ InsetCollapsable::draw(pi, x, y);
+ pi.base.font = tmpfont;
}
void InsetListings::getDrawFont(Font & font) const
{
- font = Font(Font::ALL_INHERIT, english_language);
+ font = Font(Font::ALL_INHERIT, latex_language);
font.setFamily(Font::TYPEWRITER_FAMILY);
+ // FIXME: define Color::listing?
font.setColor(Color::foreground);
}