]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListings.cpp
XHTML output for InsetListings.
[lyx.git] / src / insets / InsetListings.cpp
index 95f98a5b560e026d9f5b62007846d347c5a1716d..b54b4106fd537bc4517e1c8538dd880d6ba4be49 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Bo Peng
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -23,7 +23,6 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "InsetCaption.h"
-#include "InsetList.h"
 #include "Language.h"
 #include "MetricsInfo.h"
 #include "output_latex.h"
@@ -53,8 +52,10 @@ char const lstinline_delimiters[] =
        "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
 
 InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
-       : InsetCollapsable(buf, par.status())
-{}
+       : InsetCollapsable(buf)
+{
+       status_ = par.status();
+}
 
 
 InsetListings::~InsetListings()
@@ -71,7 +72,7 @@ Inset::DisplayType InsetListings::display() const
 
 void InsetListings::updateLabels(ParIterator const & it)
 {
-       Counters & cnts = buffer().params().documentClass().counters();
+       Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
        string const saveflt = cnts.current_float();
 
        // Tell to captions what the current float is
@@ -134,7 +135,7 @@ int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
        // 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 isInline = params().isInline();
+       bool const 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;
@@ -236,7 +237,6 @@ int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
                OutputParams rp = runparams;
                rp.moving_arg = true;
                docstring const caption = getCaption(rp);
-               runparams.encoding = rp.encoding;
                if (param_string.empty() && caption.empty())
                        os << "\n\\begin{lstlisting}\n";
                else {
@@ -273,6 +273,37 @@ int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
 }
 
 
+docstring InsetListings::xhtml(odocstream & os, OutputParams const & rp) const
+{
+       odocstringstream out;
+
+       bool const isInline = params().isInline();
+       if (isInline) 
+               out << "<br />\n";
+       else {
+               out << "<div class='float float-listings'>\n";
+               docstring caption = getCaptionHTML(rp);
+               if (!caption.empty())
+                       out << "<div class='float-caption'>" << caption << "</div>\n";
+       }
+
+       out << "<pre>\n";
+       docstring def = InsetText::xhtml(out, rp);
+       out << "\n</pre>\n";
+
+       if (isInline) {
+               out << "<br />\n";
+               os << out.str();
+       } else {
+               out <<  "</div>";
+               // In this case, this needs to be deferred, but we'll put it
+               // before anything the text itself deferred.
+               def = out.str() + '\n' + def;
+       }
+       return def;
+}
+
+
 docstring InsetListings::contextMenu(BufferView const &, int, int) const
 {
        return from_ascii("context-listings");
@@ -287,17 +318,20 @@ void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
                InsetListings::string2params(to_utf8(cmd.argument()), params());
                break;
        }
+
        case LFUN_INSET_DIALOG_UPDATE:
                cur.bv().updateDialog("listings", params2string(params()));
                break;
-       case LFUN_CELL_FORWARD:
-               if (cur.selection()) {
-                       // If there is a selection, a tab is inserted at the
-                       // beginning of each paragraph.
+
+       case LFUN_TAB_INSERT: {
+               bool const multi_par_selection = cur.selection() &&
+                       cur.selBegin().pit() != cur.selEnd().pit();
+               if (multi_par_selection) {
+                       // If there is a multi-paragraph selection, a tab is inserted
+                       // at the beginning of each paragraph.
                        cur.recordUndoSelection();
                        pit_type const pit_end = cur.selEnd().pit();
                        for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
-                               LASSERT(pit < paragraphs().size(), /**/);
                                paragraphs()[pit].insertChar(0, '\t', 
                                        buffer().params().trackChanges);
                                // Update the selection pos to make sure the selection does not
@@ -311,19 +345,19 @@ void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
                } else {
                        // Maybe we shouldn't allow tabs within a line, because they
                        // are not (yet) aligned as one might do expect.
-                       cur.recordUndo();
-                       cur.insert(from_ascii("\t"));
-                       cur.finishUndo();
+                       FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
+                       dispatch(cur, cmd);     
                }
                break;
-       case LFUN_CELL_BACKWARD:
+       }
+
+       case LFUN_TAB_DELETE:
                if (cur.selection()) {
                        // If there is a selection, a tab (if present) is removed from
                        // the beginning of each paragraph.
                        cur.recordUndoSelection();
                        pit_type const pit_end = cur.selEnd().pit();
                        for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
-                               LASSERT( pit < paragraphs().size(), /**/ );
                                Paragraph & par = paragraphs()[pit];
                                if (par.getChar(0) == '\t') {
                                        if (cur.pit() == pit)
@@ -348,8 +382,6 @@ void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
                } else {
                        // If there is no selection, try to remove a tab or some spaces 
                        // before the position of the cursor.
-                       LASSERT(cur.pit() >= 0 && cur.pit() < paragraphs().size(), /**/);
-
                        Paragraph & par = paragraphs()[cur.pit()];
                        pos_type const pos = cur.pos();
 
@@ -389,8 +421,8 @@ bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
                case LFUN_CAPTION_INSERT:
                        status.setEnabled(!params().isInline());
                        return true;
-                       case LFUN_CELL_BACKWARD:
-                       case LFUN_CELL_FORWARD:
+                       case LFUN_TAB_INSERT:
+                       case LFUN_TAB_DELETE:
                                status.setEnabled(true);
                                return true;
                default:
@@ -399,13 +431,13 @@ bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetListings::setButtonLabel()
+docstring const InsetListings::buttonLabel(BufferView const & bv) const
 {
        // FIXME UNICODE
-       if (decoration() == InsetLayout::Classic)
-               setLabel(isOpen() ?  _("Listing") : getNewLabel(_("Listing")));
+       if (decoration() == InsetLayout::CLASSIC)
+               return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
        else
-               setLabel(getNewLabel(_("Listing")));
+               return getNewLabel(_("Listing"));
 }
 
 
@@ -432,37 +464,29 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const
        if (paragraphs().empty())
                return docstring();
 
-       ParagraphList::const_iterator pit = paragraphs().begin();
-       for (; pit != paragraphs().end(); ++pit) {
-               InsetList::const_iterator it = pit->insetList().begin();
-               for (; it != pit->insetList().end(); ++it) {
-                       Inset & inset = *it->inset;
-                       if (inset.lyxCode() == CAPTION_CODE) {
-                               odocstringstream ods;
-                               InsetCaption * ins =
-                                       static_cast<InsetCaption *>(it->inset);
-                               ins->getOptArg(ods, runparams);
-                               ins->getArgument(ods, runparams);
-                               // the caption may contain \label{} but the listings
-                               // package prefer caption={}, label={}
-                               docstring cap = ods.str();
-                               if (!contains(to_utf8(cap), "\\label{"))
-                                       return cap;
-                               // convert from
-                               //     blah1\label{blah2} blah3
-                               // to
-                               //     blah1 blah3},label={blah2
-                               // to form options
-                               //     caption={blah1 blah3},label={blah2}
-                               //
-                               // NOTE that } is not allowed in blah2.
-                               regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
-                               string const new_cap("\\1\\3},label={\\2");
-                               return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
-                       }
-               }
-       }
-       return docstring();
+       InsetCaption const * ins = getCaptionInset();
+       if (ins == 0)
+               return docstring();
+
+       odocstringstream ods;
+       ins->getOptArg(ods, runparams);
+       ins->getArgument(ods, runparams);
+       // the caption may contain \label{} but the listings
+       // package prefer caption={}, label={}
+       docstring cap = ods.str();
+       if (!contains(to_utf8(cap), "\\label{"))
+               return cap;
+       // convert from
+       //     blah1\label{blah2} blah3
+       // to
+       //     blah1 blah3},label={blah2
+       // to form options
+       //     caption={blah1 blah3},label={blah2}
+       //
+       // NOTE that } is not allowed in blah2.
+       regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
+       string const new_cap("\\1\\3},label={\\2");
+       return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
 }