Hi!

Attached find a lyx2lyx for patch 1820. It's not perfect, though --- it actually corrupts some files which shouldn't be changed at all; if anyone wants to try and fix that, be my guest, I've just spent the past six hours on this and I'm not sure that it's doable.

Also, this has only convinced me even more that it's wrong to apply a lyx2lyx fix for this patch: this is a bug fix, there were previously .lyx files which were ok, but which lyx was generating incorrect latex for, and patch 1820 fixes that. If there are lyx documents out there which patch 1820 affects, it can only do them good. All this lyx2lyx patch does is to explicitly set the language to the wrong values in all kinds of situations.

I would urge for what Martin suggested a few days ago: have an empty format change to go along with patch 1820, just to mark the fix, but not to apply any lyx2lyx for now. We have this patch in store, if anyone decides that it really *is* necessary, we'll always be able to apply it retroactively, since the format change was marked.

Dov
Index: lyx-devel/lib/lyx2lyx/lyx_1_5.py
===================================================================
--- lyx-devel.orig/lib/lyx2lyx/lyx_1_5.py       2007-07-17 22:38:36.000000000 
+0300
+++ lyx-devel/lib/lyx2lyx/lyx_1_5.py    2007-07-18 03:04:40.000000000 +0300
@@ -42,9 +42,122 @@
     "Find beginning of layout, where lines[i] is included."
     return find_beginning_of(lines, i, "\\begin_layout", "\\end_layout")
 
+def isRTL(language):
+    from lyx2lyx_lang import lang
+    return lang[language][2] == "true"
+
 # End of helper functions
 ####################################################################
 
+def recreate_bug1820(document):
+       """This function tries to undo the fix for bug 1820, so that documents
+are displayed wrongly, as they were before the fix for that bug.
+
+Specifically, intra-paragraph language switches are closed before openning
+an inset; and upon entering an inset, if in the new lyx the language is now 
+different than it would have been in the old lyx, then we now explicitly set
+it to what it would have been in old lyx."""
+
+       lang_re = re.compile(r"^\\lang\s(\S+)")
+       inset_re = re.compile(r"^\\begin_inset\s(\S+)")
+       inset_end_re = re.compile(r"^\\end_inset")
+       status_re = re.compile(r"^status")
+       layout_re = re.compile(r"^\\begin_layout")
+       layout_end_re = re.compile(r"^\\end_layout")
+
+       new_cur_lang = [document.language]
+       old_cur_lang = [document.language]
+       new_outer_lang = [document.language]
+       old_outer_lang = [document.language]
+       just_entered_inset = False
+       entered_inset_layout = False
+
+       # we'll only insert the necessary lines at the end, in order not to 
+       # interfere with the looping over the lines...
+       line_insertions = [] # (insert before line number, line to insert)
+
+       for i in range(len(document.body)):
+               # determine the current language
+               result = lang_re.match(document.body[i])
+               if result:
+                       new_cur_lang[-1] = result.group(1)
+                       old_cur_lang[-1] = result.group(1)
+                       continue
+               result = layout_re.match(document.body[i])
+               if result:
+                       new_cur_lang[-1] = new_outer_lang[-1]
+                       old_cur_lang[-1] = old_outer_lang[-1]
+                       if not just_entered_inset:
+                               continue
+               
+               # if we're openning an inset, and the current language is 
different
+               # than the paragraph language:
+               # old: close the current language
+               # new: close the current langauge, unless the RTL-ness of the 
two 
+               #               languages are different
+               if inset_re.match(document.body[i]):
+                       par_language = get_paragraph_language(document, i)
+                       
+                       if old_cur_lang[-1] != par_language:
+                               # write the closure to the file
+                               line_insertions.insert(0, (i, "\\lang 
%s"%par_language))
+                               
+                       # the second clause says that if we just closed the 
current
+                       # language, then as far as the new lyx is concerned, 
we're 
+                       # back to the paragraph language
+                       if isRTL(par_language) != isRTL(new_cur_lang[-1]) and \
+                                       not old_cur_lang[-1] != par_language:
+                               new_outer_lang.append(new_cur_lang[-1])
+                               new_cur_lang.append(new_cur_lang[-1])
+                       else:
+                               new_outer_lang.append(par_language)
+                               new_cur_lang.append(par_language)
+
+                       old_cur_lang.append(par_language)
+                       old_outer_lang.append(document.language)
+                       
+                       just_entered_inset = True
+                       continue
+
+               # at end of inset, just pop the current language
+               if inset_end_re.match(document.body[i]):
+                       del new_cur_lang[-1]
+                       del new_outer_lang[-1]
+                       del old_cur_lang[-1]
+                       del old_outer_lang[-1]
+                       continue
+
+               # if we just entered an inset, first of all skip to the first 
line in
+               # the first layout which is not empty
+               if just_entered_inset:
+                       if not entered_inset_layout and \
+                          not layout_re.match(document.body[i]):
+                               continue
+                       if layout_re.match(document.body[i]):
+                               entered_inset_layout = True
+                               continue
+                       # if we get here, we must have entered_inset_layout 
already
+                       if len(document.body[i]) > 0:
+                               # we won't continue processing this footnote in 
any special way
+                               # after this
+                               just_entered_inset = False
+                               entered_inset_layout = False
+                               # if it's an explicit language setting, just 
continue
+                               if lang_re.match(document.body[i]):
+                                       continue
+                               # otherwise, if the current langauge according 
to the old
+                               # lyx is different than the current language 
according to the
+                               # new lyx, set it explicitly to the old lyx 
language.
+                               if not lang_re.match(document.body[i]) and \
+                                               new_cur_lang[-1] != 
old_cur_lang[-1]:
+                                       line_insertions.insert(0, 
+                                               (i, "\\lang 
%s"%new_cur_lang[-1]))
+
+       # insert the lines into the document
+       for (line_num, inserted_line) in line_insertions:
+               document.body.insert(line_num, inserted_line)
+
+
 def fix_convert_multiencoding(document, forward, inset_types):
     """ Fix for the last remaining problem with bug 3613, which is a result
 of some language embedding rules which were missed in format change 249.
@@ -1232,7 +1345,7 @@
 def get_paragraph_language(document, i):
     """ Return the language of the paragraph in which line i of the document
     body is. If the first thing in the paragraph is a \\lang command, that
-    is the paragraph's langauge; otherwise, the paragraph's language is the 
+    is the paragraph's language; otherwise, the paragraph's language is the 
     document's language."""
 
     lines = document.body
@@ -2068,9 +2181,11 @@
            [275, [convert_graphics_rotation]],
            [276, [convert_arabic]],
            [277, [fix_footnote_encoding]],
+           [278, [recreate_bug1820]],
           ]
 
 revert =  [
+           [277, []],
            [276, [revert_footnote_encoding]],
            [275, [revert_arabic]],
            [274, [revert_graphics_rotation]],
Index: lyx-devel/development/FORMAT
===================================================================
--- lyx-devel.orig/development/FORMAT   2007-07-18 02:28:59.000000000 +0300
+++ lyx-devel/development/FORMAT        2007-07-18 02:32:31.000000000 +0300
@@ -1,6 +1,10 @@
 LyX file-format changes
 -----------------------
 
+2007-07-18 Dov Feldstern <[EMAIL PROTECTED]>
+
+       * format incremented to 278: Due to the changes made for fixing bug 1820
+
 2007-07-15 Dov Feldstern <[EMAIL PROTECTED]>
 
        * format incremented to 277: fix encodings in footnotes (part of bug 
3613)
Index: lyx-devel/lib/lyx2lyx/LyX.py
===================================================================
--- lyx-devel.orig/lib/lyx2lyx/LyX.py   2007-07-18 02:28:59.000000000 +0300
+++ lyx-devel/lib/lyx2lyx/LyX.py        2007-07-18 02:31:29.000000000 +0300
@@ -77,7 +77,7 @@
                    ("1_2",     [220], generate_minor_versions("1.2" , 4)),
                    ("1_3",     [221], generate_minor_versions("1.3" , 7)),
                    ("1_4", range(222,246), generate_minor_versions("1.4" , 4)),
-                   ("1_5", range(246,278), generate_minor_versions("1.5" , 0))]
+                   ("1_5", range(246,279), generate_minor_versions("1.5" , 0))]
 
 
 def formats_list():
Index: lyx-devel/src/Buffer.cpp
===================================================================
--- lyx-devel.orig/src/Buffer.cpp       2007-07-18 02:29:38.000000000 +0300
+++ lyx-devel/src/Buffer.cpp    2007-07-18 02:30:01.000000000 +0300
@@ -141,7 +141,7 @@
 
 namespace {
 
-int const LYX_FORMAT = 277;
+int const LYX_FORMAT = 278;
 
 } // namespace anon
 

Reply via email to