Vincent van Ravesteijn - TNW wrote:
> >My copy of LyX 1.6.3 errors out on indexing because when
> >LyX exports to LaTeX, it outputs normal range type indexing
> >entries (the ones ending in a pipe symbol and a parenthese)
> >with something like this:
> >
> >\index{mysection|(@mysection\textbar(}
> >
> >It should have looked like this:
> >
> >\index{mysection|(}
> 
> Steve,
> 
> In case you didn't notice this mail (devel-list), I will repeat it here.
> 
> 
> What's your Tools->Preferences->Output->LaTeX->Tex Encoding option ?
> 
> If this doesn't read T1, put it in there and I think this problem will
> go away
> 
> At least, I can reproduce it also when setting the document language to
> greek. Then this 'trick' with '|)' doesn't work any longer. That's a bug
> too.

The attached series of patches address this problem. The first basic patch 
(idx.diff) assures that the active characters ("|", "!", and "@") are output 
in plaintext even if they are in an ERT. Our sorting algorithm relies on this. 

The other two patches provide two alternative ways that deal with Steve's 
original problem:

* idx+a.diff assures that lyx2lyx wraps "|" characters in ERT. This is the 
more coherent approach IMHO, since "|" is not natively supported, but plain 
LaTeX. Additionally, some documentation would be needed.

* idx+b.diff implements what Steve suggested: "|" are always output verbatim 
inside index insets. This is more intuitive to users probably, but it does the 
"LyX"/"LaTeX"-kind of magic we'd actually like to avoid.

Opinions?

Jürgen
Index: src/insets/InsetERT.cpp
===================================================================
--- src/insets/InsetERT.cpp	(Revision 31242)
+++ src/insets/InsetERT.cpp	(Arbeitskopie)
@@ -25,6 +25,7 @@
 #include "Lexer.h"
 #include "LyXAction.h"
 #include "MetricsInfo.h"
+#include "OutputParams.h"
 #include "ParagraphParameters.h"
 #include "Paragraph.h"
 #include "TextClass.h"
@@ -63,9 +64,33 @@
 }
 
 
-int InsetERT::plaintext(odocstream &, OutputParams const &) const
+int InsetERT::plaintext(odocstream & os, OutputParams const & rp) const
 {
-	return 0; // do not output TeX code
+	if (!rp.inIndexEntry)
+		// do not output TeX code
+		return 0;
+
+	ParagraphList::const_iterator par = paragraphs().begin();
+	ParagraphList::const_iterator end = paragraphs().end();
+
+	while (par != end) {
+		pos_type siz = par->size();
+		for (pos_type i = 0; i < siz; ++i) {
+			char_type const c = par->getChar(i);
+			// output the active characters
+			switch (c) {
+			case '|':
+			case '!':
+			case '@':
+				os.put(c);
+				break;
+			default:
+				break;
+			}
+		}
+		++par;
+	}
+	return 0;
 }
 
 
Index: src/insets/InsetIndex.cpp
===================================================================
--- src/insets/InsetIndex.cpp	(Revision 31250)
+++ src/insets/InsetIndex.cpp	(Arbeitskopie)
@@ -54,8 +54,11 @@
 
 
 int InsetIndex::latex(odocstream & os,
-		      OutputParams const & runparams) const
+		      OutputParams const & runparams_in) const
 {
+	OutputParams runparams(runparams_in);
+	runparams.inIndexEntry = true;
+
 	if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
 	    && params_.index != "idx") {
 		os << "\\sindex[";
@@ -88,9 +91,6 @@
 		// ...and erase that stuff from latexstr
 		latexstr = latexstr.erase(pos);
 		// ...and similarly from plainstr
-		// FIXME This utterly fails if the "|" is in ERT
-		// which is necessary with font encodings other
-		// than T1.
 		size_t ppos = plainstr.find(from_ascii("|"));
 		if (ppos < plainstr.size())
 			plainstr.erase(ppos);
@@ -100,7 +100,6 @@
 
 	// Separate the entires and subentries, i.e., split on "!"
 	// FIXME This would do the wrong thing with escaped ! characters
-	// and with "!" in ERT.
 	std::vector<docstring> const levels =
 		getVectorFromString(latexstr, from_ascii("!"), true);
 	std::vector<docstring> const levels_plain =
Index: src/OutputParams.h
===================================================================
--- src/OutputParams.h	(Revision 31242)
+++ src/OutputParams.h	(Arbeitskopie)
@@ -168,6 +168,11 @@
 	 */
 	Float inFloat;
 
+	/** Whether we are inside an index inset.
+	 *  ERT needs to know this, due to the active chars.
+	 */
+	bool inIndexEntry;
+
 	/** Whether we are inside an inset that is logically deleted.
 	 *  A value > 0 indicates a deleted inset.
          */
Index: src/OutputParams.cpp
===================================================================
--- src/OutputParams.cpp	(Revision 31242)
+++ src/OutputParams.cpp	(Arbeitskopie)
@@ -24,7 +24,8 @@
 	  use_indices(false), use_japanese(false), linelen(0), depth(0),
 	  exportdata(new ExportData),
 	  inComment(false), inTableCell(NO), inFloat(NONFLOAT),
-	  inDeletedInset(0), changeOfDeletedInset(Change::UNCHANGED),
+	  inIndexEntry(false), inDeletedInset(0),
+	  changeOfDeletedInset(Change::UNCHANGED),
 	  par_begin(0), par_end(0), isLastPar(false),
 	  dryrun(false), verbatim(false), disable_captions(false)
 {
Index: lib/lyx2lyx/lyx_1_6.py
===================================================================
--- lib/lyx2lyx/lyx_1_6.py	(Revision 31242)
+++ lib/lyx2lyx/lyx_1_6.py	(Arbeitskopie)
@@ -209,7 +209,7 @@
     return (line[:pos + 1], line[pos + 1:])
 
 
-def latex2ert(line):
+def latex2ert(line, isindex):
     '''Converts LaTeX commands into ERT. line may well be a multi-line
        string when it is returned.'''
     if not line:
@@ -246,6 +246,9 @@
     # put all remaining braces in ERT
     line = wrap_into_ert(line, '}', '}')
     line = wrap_into_ert(line, '{', '{')
+    if isindex:
+        # active character that is not available in all font encodings
+        line = wrap_into_ert(line, '|', '|')
     retval += line
     return retval
 
@@ -257,10 +260,12 @@
 #end up inside ERT. That routine could be modified so that it returned
 #a list of lines, and we could then skip ERT bits and only deal with
 #the other bits.
-def latex2lyx(data):
+def latex2lyx(data, isindex):
     '''Takes a string, possibly multi-line, and returns the result of
     converting LaTeX constructs into LyX constructs. Returns a list of
-    lines, suitable for insertion into document.body.'''
+    lines, suitable for insertion into document.body.
+    The bool isindex specifies whether we are in an index macro (which
+    has some specific active characters that need to be ERTed).'''
 
     if not data:
         return [""]
@@ -309,14 +314,14 @@
             g = m.group(3)
             if s:
                 # this is non-math!
-                s = latex2ert(s)
+                s = latex2ert(s, isindex)
                 subst = s.split('\n')
                 retval += subst
             retval.append("\\begin_inset Formula " + f)
             retval.append("\\end_inset")
             m = mathre.match(g)
         # Handle whatever is left, which is just text
-        g = latex2ert(g)
+        g = latex2ert(g, isindex)
         subst = g.split('\n')
         retval += subst
     return retval
@@ -1096,7 +1101,7 @@
             linelist = [""]
         else:
             fullcontent = m.group(1)
-            linelist = latex2lyx(fullcontent)
+            linelist = latex2lyx(fullcontent, True)
         #document.warning(fullcontent)
 
         linelist = ["\\begin_inset Index", "status collapsed", "\\begin_layout Standard", ""] + \
@@ -2176,7 +2181,7 @@
         addedLines -= 1
         subst = ['\\begin_inset Float figure', 'wide false', 'sideways false',
                  'status open', '', '\\begin_layout Plain Layout', '\\begin_inset Caption',
-                 '', '\\begin_layout Plain Layout'] + latex2lyx(caption) + \
+                 '', '\\begin_layout Plain Layout'] + latex2lyx(caption, False) + \
                  [ '\\end_layout', '', '\\end_inset', '',
                  '\\end_layout', '', '\\begin_layout Plain Layout']
         document.body[i : i] = subst
Index: src/Paragraph.cpp
===================================================================
--- src/Paragraph.cpp	(Revision 31242)
+++ src/Paragraph.cpp	(Arbeitskopie)
@@ -912,8 +912,13 @@
 		column += 13;
 		break;
 	case '|':
-		os << "\\textbar{}";
-		column += 9;
+		if (runparams.inIndexEntry) {
+			os.put(c);
+			column += 1;
+		} else {
+			os << "\\textbar{}";
+			column += 9;
+		}
 		break;
 	case '-':
 		os << '-';

Reply via email to