]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_1_4.py
merge booktabs branch
[lyx.git] / lib / lyx2lyx / lyx_1_4.py
index 26f174c5b57592a06815935c079b0a49344bf87c..28a7743d32888b89de09a98c9c1dbab0fa5070f3 100644 (file)
@@ -23,7 +23,8 @@ from os import access, F_OK
 import os.path
 from parser_tools import find_token, find_end_of_inset, get_next_paragraph, \
                          get_paragraph, get_value, del_token, is_nonempty_line,\
-                        find_tokens, find_end_of, find_token2, find_re
+                         find_tokens, find_end_of, find_token_exact, find_tokens_exact,\
+                         find_re, get_layout
 from sys import stdin
 from string import replace, split, find, strip, join
 
@@ -56,6 +57,46 @@ def rm_end_header(file):
     del file.header[i]
 
 
+def convert_amsmath(file):
+    i = find_token(file.header, "\\use_amsmath", 0)
+    if i == -1:
+        file.warning("Malformed LyX file: Missing '\\use_amsmath'.")
+        return
+    tokens = split(file.header[i])
+    if len(tokens) != 2:
+        file.warning("Malformed LyX file: Could not parse line '%s'." % file.header[i])
+        use_amsmath = '0'
+    else:
+        use_amsmath = tokens[1]
+    # old: 0 == off, 1 == on
+    # new: 0 == off, 1 == auto, 2 == on
+    # translate off -> auto, since old format 'off' means auto in reality
+    if use_amsmath == '0':
+        file.header[i] = "\\use_amsmath 1"
+    else:
+        file.header[i] = "\\use_amsmath 2"
+
+
+def revert_amsmath(file):
+    i = find_token(file.header, "\\use_amsmath", 0)
+    if i == -1:
+        file.warning("Malformed LyX file: Missing '\\use_amsmath'.")
+        return
+    tokens = split(file.header[i])
+    if len(tokens) != 2:
+        file.warning("Malformed LyX file: Could not parse line '%s'." % file.header[i])
+        use_amsmath = '0'
+    else:
+        use_amsmath = tokens[1]
+    # old: 0 == off, 1 == on
+    # new: 0 == off, 1 == auto, 2 == on
+    # translate auto -> off, since old format 'off' means auto in reality
+    if use_amsmath == '2':
+        file.header[i] = "\\use_amsmath 1"
+    else:
+        file.header[i] = "\\use_amsmath 0"
+
+
 ##
 # \SpecialChar ~ -> \InsetSpace ~
 #
@@ -65,8 +106,38 @@ def convert_spaces(file):
 
 
 def revert_spaces(file):
+    regexp = re.compile(r'(.*)(\\InsetSpace\s+)(\S+)')
+    i = 0
+    while 1:
+        i = find_re(file.body, regexp, i)
+        if i == -1:
+            break
+        space = regexp.match(file.body[i]).group(3)
+        prepend = regexp.match(file.body[i]).group(1)
+        if space == '~':
+            file.body[i] = regexp.sub(prepend + '\\SpecialChar ~', file.body[i])
+            i = i + 1
+        else:
+            file.body[i] = regexp.sub(prepend, file.body[i])
+            file.body[i+1:i+1] = ''
+            if space == "\\space":
+                space = "\\ "
+            i = insert_ert(file.body, i+1, 'Collapsed', space, file.format - 1, file.default_layout)
+
+##
+# \InsetSpace \, -> \InsetSpace \thinspace{}
+# \InsetSpace \space -> \InsetSpace \space{}
+#
+def rename_spaces(file):
+    for i in range(len(file.body)):
+        file.body[i] = replace(file.body[i],"\\InsetSpace \\space","\\InsetSpace \\space{}")
+        file.body[i] = replace(file.body[i],"\\InsetSpace \,","\\InsetSpace \\thinspace{}")
+
+
+def revert_space_names(file):
     for i in range(len(file.body)):
-        file.body[i] = replace(file.body[i],"\\InsetSpace ~", "\\SpecialChar ~")
+        file.body[i] = replace(file.body[i],"\\InsetSpace \\space{}","\\InsetSpace \\space")
+        file.body[i] = replace(file.body[i],"\\InsetSpace \\thinspace{}","\\InsetSpace \\,")
 
 
 ##
@@ -99,7 +170,7 @@ def revert_eqref(file):
             break
         eqref = lyx_support_escape(regexp.sub("", file.body[i]))
         file.body[i:i+1] = ["\\begin_inset ERT", "status Collapsed", "",
-                            "\\layout Standard", "", "\\backslash ",
+                            '\\layout %s' % file.default_layout, "", "\\backslash ",
                             "eqref" + eqref]
         i = i + 7
 
@@ -221,15 +292,15 @@ def convert_comment(file):
         if i == -1:
             return
 
-        file.body[i:i+1] = ["\\layout Standard","","",
+        file.body[i:i+1] = ['\\layout %s' % file.default_layout,"","",
                         "\\begin_inset Comment",
                         "collapsed true","",
-                        "\\layout Standard"]
+                        '\\layout %s' % file.default_layout]
         i = i + 7
 
         while 1:
                 old_i = i
-               i = find_token(file.body, "\\layout", i)
+                i = find_token(file.body, "\\layout", i)
                 if i == -1:
                     i = len(file.body) - 1
                     file.body[i:i] = ["\\end_inset","",""]
@@ -273,7 +344,7 @@ def convert_comment(file):
                     file.body[i:i] = ["\\end_inset"]
                     i = i + 1
                     break
-                file.body[i:i+1] = ["\\layout Standard"]
+                file.body[i:i+1] = ['\\layout %s' % file.default_layout]
                 i = i + 1
 
 
@@ -540,7 +611,7 @@ def convert_breaks(file):
         i = find_token(file.body, "\\begin_layout", i)
         if i == -1:
             return
-        layout = split(file.body[i])[1]
+        layout = get_layout(file.body[i], file.default_layout)
         i = i + 1
 
         # Merge all paragraph parameters into a single line
@@ -567,7 +638,8 @@ def convert_breaks(file):
         # We want to avoid new paragraphs if possible becauase we want to
         # inherit font sizes.
         nonstandard = 0
-        if (layout != "Standard" or find(file.body[i],"\\align") != -1 or
+        if (not file.is_default_layout(layout) or
+            find(file.body[i],"\\align") != -1 or
             find(file.body[i],"\\labelwidthstring") != -1 or
             find(file.body[i],"\\noindent") != -1):
             nonstandard = 1
@@ -610,7 +682,7 @@ def convert_breaks(file):
             paragraph_above = list()
             if nonstandard:
                 # We need to create an extra paragraph for nonstandard environments
-                paragraph_above = ['\\begin_layout Standard', '']
+                paragraph_above = ['\\begin_layout %s' % file.default_layout, '']
 
             if pb_top != -1:
                 paragraph_above.extend(['\\newpage ',''])
@@ -625,7 +697,7 @@ def convert_breaks(file):
                 # We can't use the vspace inset because it does not know \parskip.
                 paragraph_above.extend(['\\lyxline ', '', ''])
                 insert_ert(paragraph_above, len(paragraph_above) - 1, 'Collapsed',
-                           '\\vspace{-1\\parskip}\n', file.format + 1)
+                           '\\vspace{-1\\parskip}\n', file.format + 1, file.default_layout)
                 paragraph_above.extend([''])
 
             if nonstandard:
@@ -662,7 +734,7 @@ def convert_breaks(file):
             paragraph_below = list()
             if nonstandard:
                 # We need to create an extra paragraph for nonstandard environments
-                paragraph_below = ['', '\\begin_layout Standard', '']
+                paragraph_below = ['', '\\begin_layout %s' % file.default_layout, '']
             else:
                 for a in range(len(font_attributes)):
                     if find_token(file.body, font_attributes[a], i, k) != -1:
@@ -755,7 +827,7 @@ def revert_box(file):
 def convert_collapsable(file):
     i = 0
     while 1:
-        i = find_tokens(file.body, ["\\begin_inset Box",
+        i = find_tokens_exact(file.body, ["\\begin_inset Box",
                                 "\\begin_inset Branch",
                                 "\\begin_inset CharStyle",
                                 "\\begin_inset Float",
@@ -789,7 +861,7 @@ def convert_collapsable(file):
 def revert_collapsable(file):
     i = 0
     while 1:
-        i = find_tokens(file.body, ["\\begin_inset Box",
+        i = find_tokens_exact(file.body, ["\\begin_inset Box",
                                 "\\begin_inset Branch",
                                 "\\begin_inset CharStyle",
                                 "\\begin_inset Float",
@@ -915,10 +987,10 @@ def convert_minipage(file):
 
         # convert the inner_position
         if file.body[i][:14] == "inner_position":
-            file.body[i] = 'inner_pos "%s"' %  inner_pos[int(file.body[i][15])]
+            innerpos = inner_pos[int(file.body[i][15])]
+            del file.body[i]    
         else:
-            file.body.insert('inner_pos "%s"' % inner_pos[0])
-        i = i + 1
+            innerpos = inner_pos[0]
 
         # We need this since the new file format has a height and width
         # in a different order.
@@ -939,13 +1011,19 @@ def convert_minipage(file):
 
         if file.body[i][:9] == "collapsed":
             if file.body[i][9:] == "true":
-               status = "collapsed"
+                status = "collapsed"
             else:
-               status = "open"
+                status = "open"
             del file.body[i]
         else:
-           status = "collapsed"
+            status = "collapsed"
 
+        # Handle special default case:
+        if height == ' "1pt"' and innerpos == 'c':
+            innerpos = 't'
+
+        file.body.insert(i, 'inner_pos "' + innerpos + '"')
+        i = i + 1
         file.body.insert(i, 'use_parbox 0')
         i = i + 1
         file.body.insert(i, 'width' + width)
@@ -963,21 +1041,21 @@ def convert_minipage(file):
 # -------------------------------------------------------------------------------------------
 # Convert backslashes and '\n' into valid ERT code, append the converted
 # text to body[i] and return the (maybe incremented) line index i
-def convert_ertbackslash(body, i, ert, format):
+def convert_ertbackslash(body, i, ert, format, default_layout):
     for c in ert:
-       if c == '\\':
-           body[i] = body[i] + '\\backslash '
-           i = i + 1
-           body.insert(i, '')
-       elif c == '\n':
+        if c == '\\':
+            body[i] = body[i] + '\\backslash '
+            i = i + 1
+            body.insert(i, '')
+        elif c == '\n':
             if format <= 240:
                 body[i+1:i+1] = ['\\newline ', '']
                 i = i + 2
             else:
-                body[i+1:i+1] = ['\\end_layout', '', '\\begin_layout Standard', '']
+                body[i+1:i+1] = ['\\end_layout', '', '\\begin_layout %s' % default_layout, '']
                 i = i + 4
-       else:
-           body[i] = body[i] + c
+        else:
+            body[i] = body[i] + c
     return i
 
 
@@ -1020,7 +1098,7 @@ def get_par_params(lines, i):
     params = ''
     while lines[i][:1] == '\\' and split(lines[i][1:])[0] in par_params:
         params = params + ' ' + strip(lines[i])
-       i = i + 1
+        i = i + 1
     return strip(params)
 
 
@@ -1283,7 +1361,7 @@ def revert_breaks(file):
         # The others are converted in the next loop runs (if they exist)
         if insets[0] == "vspace":
             file.body[i:i+1] = ['\\begin_inset ERT', 'status Collapsed', '',
-                                '\\layout Standard', '', '\\backslash ']
+                                '\\layout %s' % file.default_layout, '', '\\backslash ']
             i = i + 6
             if spaceamount[0][-1] == '*':
                 spaceamount[0] = spaceamount[0][:-1]
@@ -1315,7 +1393,7 @@ def revert_breaks(file):
                     file.body.insert(i, 'vspace*{')
                 else:
                     file.body.insert(i, 'vspace{')
-                i = convert_ertbackslash(file.body, i, spaceamount[0], file.format - 1)
+                i = convert_ertbackslash(file.body, i, spaceamount[0], file.format - 1, file.default_layout)
                 file.body[i] = file.body[i] + '}'
             i = i + 1
         elif insets[0] == "lyxline":
@@ -1326,7 +1404,7 @@ def revert_breaks(file):
                 latexsize = '\\normalsize'
             i = insert_ert(file.body, i, 'Collapsed',
                            '\\lyxline{%s}' % latexsize,
-                           file.format - 1)
+                           file.format - 1, file.default_layout)
             # We use \providecommand so that we don't get an error if native
             # lyxlines are used (LyX writes first its own preamble and then
             # the user specified one)
@@ -1338,7 +1416,7 @@ def revert_breaks(file):
         elif insets[0] == "newpage":
             file.body[i] = ''
             i = insert_ert(file.body, i, 'Collapsed', '\\newpage{}',
-                           file.format - 1)
+                           file.format - 1, file.default_layout)
 
 
 # Convert a LyX length into a LaTeX length
@@ -1349,44 +1427,44 @@ def convert_len(len, special):
 
     # Convert special lengths
     if special != 'none':
-       len = '%f\\' % len2value(len) + special
+        len = '%f\\' % len2value(len) + special
 
     # Convert LyX units to LaTeX units
     for unit in units.keys():
-       if find(len, unit) != -1:
-           len = '%f' % (len2value(len) / 100) + units[unit]
-           break
+        if find(len, unit) != -1:
+            len = '%f' % (len2value(len) / 100) + units[unit]
+            break
 
     return len
 
 
 # Convert a LyX length into valid ERT code and append it to body[i]
 # Return the (maybe incremented) line index i
-def convert_ertlen(body, i, len, special, format):
+def convert_ertlen(body, i, len, special, format, default_layout):
     # Convert backslashes and insert the converted length into body
-    return convert_ertbackslash(body, i, convert_len(len, special), format)
+    return convert_ertbackslash(body, i, convert_len(len, special), format, default_layout)
 
 
 # Return the value of len without the unit in numerical form
 def len2value(len):
     result = re.search('([+-]?[0-9.]+)', len)
     if result:
-       return float(result.group(1))
+        return float(result.group(1))
     # No number means 1.0
     return 1.0
 
 
 # Convert text to ERT and insert it at body[i]
 # Return the index of the line after the inserted ERT
-def insert_ert(body, i, status, text, format):
+def insert_ert(body, i, status, text, format, default_layout):
     body[i:i] = ['\\begin_inset ERT', 'status ' + status, '']
     i = i + 3
     if format <= 224:
-        body[i:i] = ['\\layout Standard', '']
+        body[i:i] = ['\\layout %s' % default_layout, '']
     else:
-        body[i:i] = ['\\begin_layout Standard', '']
+        body[i:i] = ['\\begin_layout %s' % default_layout, '']
     i = i + 1       # i points now to the just created empty line
-    i = convert_ertbackslash(body, i, text, format) + 1
+    i = convert_ertbackslash(body, i, text, format, default_layout) + 1
     if format > 224:
         body[i:i] = ['\\end_layout']
         i = i + 1
@@ -1412,50 +1490,50 @@ def convert_frameless_box(file):
         i = find_token(file.body, '\\begin_inset Frameless', i)
         if i == -1:
             return
-       j = find_end_of_inset(file.body, i)
-       if j == -1:
-           file.warning("Malformed LyX file: Missing '\\end_inset'.")
-           i = i + 1
-           continue
-       del file.body[i]
-       j = j - 1
-
-       # Gather parameters
-       params = {'position':0, 'hor_pos':'c', 'has_inner_box':'1',
+        j = find_end_of_inset(file.body, i)
+        if j == -1:
+            file.warning("Malformed LyX file: Missing '\\end_inset'.")
+            i = i + 1
+            continue
+        del file.body[i]
+        j = j - 1
+
+        # Gather parameters
+        params = {'position':0, 'hor_pos':'c', 'has_inner_box':'1',
                   'inner_pos':1, 'use_parbox':'0', 'width':'100col%',
-                 'special':'none', 'height':'1in',
-                 'height_special':'totalheight', 'collapsed':'false'}
-       for key in params.keys():
-           value = replace(get_value(file.body, key, i, j), '"', '')
-           if value != "":
-               if key == 'position':
-                   # convert new to old position: 'position "t"' -> 0
-                   value = find_token(pos, value, 0)
-                   if value != -1:
-                       params[key] = value
-               elif key == 'inner_pos':
-                   # convert inner position
-                   value = find_token(inner_pos, value, 0)
-                   if value != -1:
-                       params[key] = value
-               else:
-                   params[key] = value
-               j = del_token(file.body, key, i, j)
-       i = i + 1
-
-       # Convert to minipage or ERT?
-       # Note that the inner_position and height parameters of a minipage
-       # inset are ignored and not accessible for the user, although they
-       # are present in the file format and correctly read in and written.
-       # Therefore we convert to ERT if they do not have their LaTeX
-       # defaults. These are:
-       # - the value of "position" for "inner_pos"
-       # - "\totalheight"          for "height"
-       if (params['use_parbox'] != '0' or
-           params['has_inner_box'] != '1' or
-           params['special'] != 'none' or
-           params['height_special'] != 'totalheight' or
-           len2value(params['height']) != 1.0):
+                  'special':'none', 'height':'1in',
+                  'height_special':'totalheight', 'collapsed':'false'}
+        for key in params.keys():
+            value = replace(get_value(file.body, key, i, j), '"', '')
+            if value != "":
+                if key == 'position':
+                    # convert new to old position: 'position "t"' -> 0
+                    value = find_token(pos, value, 0)
+                    if value != -1:
+                        params[key] = value
+                elif key == 'inner_pos':
+                    # convert inner position
+                    value = find_token(inner_pos, value, 0)
+                    if value != -1:
+                        params[key] = value
+                else:
+                    params[key] = value
+                j = del_token(file.body, key, i, j)
+        i = i + 1
+
+        # Convert to minipage or ERT?
+        # Note that the inner_position and height parameters of a minipage
+        # inset are ignored and not accessible for the user, although they
+        # are present in the file format and correctly read in and written.
+        # Therefore we convert to ERT if they do not have their LaTeX
+        # defaults. These are:
+        # - the value of "position" for "inner_pos"
+        # - "\totalheight"          for "height"
+        if (params['use_parbox'] != '0' or
+            params['has_inner_box'] != '1' or
+            params['special'] != 'none' or
+            params['height_special'] != 'totalheight' or
+            len2value(params['height']) != 1.0):
 
             # Here we know that this box is not supported in file format 224.
             # Therefore we need to convert it to ERT. We can't simply convert
@@ -1527,7 +1605,7 @@ def convert_frameless_box(file):
             ert = ert + '\\let\\endminipage\\endlyxtolyxminipage%\n'
 
             old_i = i
-            i = insert_ert(file.body, i, 'Collapsed', ert, file.format + 1)
+            i = insert_ert(file.body, i, 'Collapsed', ert, file.format - 1, file.default_layout)
             j = j + i - old_i - 1
 
             file.body[i:i] = ['\\begin_inset Minipage',
@@ -1542,24 +1620,24 @@ def convert_frameless_box(file):
             # Restore the original minipage environment since we may have
             # minipages inside this box.
             # Start a new paragraph because the following may be nonstandard
-            file.body[i:i] = ['\\layout Standard', '', '']
+            file.body[i:i] = ['\\layout %s' % file.default_layout, '', '']
             i = i + 2
             j = j + 3
             ert = '\\let\\minipage\\lyxtolyxrealminipage%\n'
             ert = ert + '\\let\\endminipage\\lyxtolyxrealendminipage%'
             old_i = i
-            i = insert_ert(file.body, i, 'Collapsed', ert, file.format + 1)
+            i = insert_ert(file.body, i, 'Collapsed', ert, file.format - 1, file.default_layout)
             j = j + i - old_i - 1
 
             # Redefine the minipage end before the inset end.
             # Start a new paragraph because the previous may be nonstandard
-            file.body[j:j] = ['\\layout Standard', '', '']
+            file.body[j:j] = ['\\layout %s' % file.default_layout, '', '']
             j = j + 2
             ert = '\\let\\endminipage\\endlyxtolyxminipage'
-            j = insert_ert(file.body, j, 'Collapsed', ert, file.format + 1)
-           j = j + 1
+            j = insert_ert(file.body, j, 'Collapsed', ert, file.format - 1, file.default_layout)
+            j = j + 1
             file.body.insert(j, '')
-           j = j + 1
+            j = j + 1
 
             # LyX writes '%\n' after each box. Therefore we need to end our
             # ERT with '%\n', too, since this may swallow a following space.
@@ -1567,21 +1645,66 @@ def convert_frameless_box(file):
                 ert = '}%\n'
             else:
                 ert = '\\end{lyxtolyxrealminipage}%\n'
-            j = insert_ert(file.body, j, 'Collapsed', ert, file.format + 1)
+            j = insert_ert(file.body, j, 'Collapsed', ert, file.format - 1, file.default_layout)
 
             # We don't need to restore the original minipage after the inset
             # end because the scope of the redefinition is the original box.
 
-       else:
+        else:
+
+            # Convert to minipage
+            file.body[i:i] = ['\\begin_inset Minipage',
+                              'position %d' % params['position'],
+                              'inner_position %d' % params['inner_pos'],
+                              'height "' + params['height'] + '"',
+                              'width "' + params['width'] + '"',
+                              'collapsed ' + params['collapsed']]
+            i = i + 6
+
+
+def remove_branches(file):
+    i = 0
+    while 1:
+        i = find_token(file.header, "\\branch", i)
+        if i == -1:
+            break
+        file.warning("Removing branch %s." % split(file.header[i])[1])
+        j = find_token(file.header, "\\end_branch", i)
+        if j == -1:
+            file.warning("Malformed LyX file: Missing '\\end_branch'.")
+            break
+        del file.header[i:j+1]
+
+    i = 0
+    while 1:
+        i = find_token(file.body, "\\begin_inset Branch", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(file.body, i)
+        if j == -1:
+            file.warning("Malformed LyX file: Missing '\\end_inset'.")
+            i = i + 1
+            continue
+        del file.body[i]
+        del file.body[j - 1]
+        # Seach for a line starting 'collapsed'
+        # If, however, we find a line starting '\layout'
+        # (_always_ present) then break with a warning message
+        collapsed_found = 0
+        while 1:
+            if (file.body[i][:9] == "collapsed"):
+                del file.body[i]
+                collapsed_found = 1
+                continue
+            elif (file.body[i][:7] == "\\layout"):
+                if collapsed_found == 0:
+                    file.warning("Malformed LyX file: Missing 'collapsed'.")
+                # Delete this new paragraph, since it would not appear in
+                # .tex output. This avoids also empty paragraphs.
+                del file.body[i]
+                break
+            i = i + 1
 
-           # Convert to minipage
-           file.body[i:i] = ['\\begin_inset Minipage',
-                             'position %d' % params['position'],
-                             'inner_position %d' % params['inner_pos'],
-                             'height "' + params['height'] + '"',
-                             'width "' + params['width'] + '"',
-                             'collapsed ' + params['collapsed']]
-           i = i + 6
 
 ##
 # Convert jurabib
@@ -1635,7 +1758,7 @@ def revert_bibtopic(file):
 def convert_float(file):
     i = 0
     while 1:
-        i = find_token(file.body, '\\begin_inset Float', i)
+        i = find_token_exact(file.body, '\\begin_inset Float', i)
         if i == -1:
             return
         # Seach for a line starting 'wide'
@@ -1656,7 +1779,7 @@ def convert_float(file):
 def revert_float(file):
     i = 0
     while 1:
-        i = find_token(file.body, '\\begin_inset Float', i)
+        i = find_token_exact(file.body, '\\begin_inset Float', i)
         if i == -1:
             return
         j = find_end_of_inset(file.body, i)
@@ -1682,31 +1805,31 @@ def convert_graphics(file):
         if i == -1:
             return
 
-       j = find_token2(file.body, "filename", i)
+        j = find_token_exact(file.body, "filename", i)
         if j == -1:
             return
         i = i + 1
-       filename = split(file.body[j])[1]
-       absname = os.path.normpath(os.path.join(file.dir, filename))
-       if file.input == stdin and not os.path.isabs(filename):
-           # We don't know the directory and cannot check the file.
-           # We could use a heuristic and take the current directory,
-           # and we could try to find out if filename has an extension,
-           # but that would be just guesses and could be wrong.
-           file.warning("""Warning: Can not determine whether file
+        filename = split(file.body[j])[1]
+        absname = os.path.normpath(os.path.join(file.dir, filename))
+        if file.input == stdin and not os.path.isabs(filename):
+            # We don't know the directory and cannot check the file.
+            # We could use a heuristic and take the current directory,
+            # and we could try to find out if filename has an extension,
+            # but that would be just guesses and could be wrong.
+            file.warning("""Warning: Can not determine whether file
          %s
          needs an extension when reading from standard input.
          You may need to correct the file manually or run
          lyx2lyx again with the .lyx file as commandline argument.""" % filename)
-           continue
-       # This needs to be the same algorithm as in pre 233 insetgraphics
-       if access(absname, F_OK):
-           continue
-       if access(absname + ".ps", F_OK):
-           file.body[j] = replace(file.body[j], filename, filename + ".ps")
-           continue
-       if access(absname + ".eps", F_OK):
-           file.body[j] = replace(file.body[j], filename, filename + ".eps")
+            continue
+        # This needs to be the same algorithm as in pre 233 insetgraphics
+        if access(absname, F_OK):
+            continue
+        if access(absname + ".ps", F_OK):
+            file.body[j] = replace(file.body[j], filename, filename + ".ps")
+            continue
+        if access(absname + ".eps", F_OK):
+            file.body[j] = replace(file.body[j], filename, filename + ".eps")
 
 
 ##
@@ -1766,7 +1889,7 @@ def convert_names(file):
                           "\\begin_inset CharStyle Firstname",
                           "status inlined",
                           "",
-                          "\\begin_layout Standard",
+                          '\\begin_layout %s' % file.default_layout,
                           "",
                           "%s" % firstname,
                           "\end_layout",
@@ -1777,7 +1900,7 @@ def convert_names(file):
                           "\\begin_inset CharStyle Surname",
                           "status inlined",
                           "",
-                          "\\begin_layout Standard",
+                          '\\begin_layout %s' % file.default_layout,
                           "",
                           "%s" % surname,
                           "\\end_layout",
@@ -2104,7 +2227,7 @@ def convert_ert_paragraphs(file):
             k = find_token(file.body, "\\begin_layout", k, j)
             if k == -1:
                 break
-            file.body[k] = "\\begin_layout Standard"
+            file.body[k] = '\\begin_layout %s' % file.default_layout
             k = k + 1
 
         # remove all paragraph parameters and font settings
@@ -2121,14 +2244,14 @@ def convert_ert_paragraphs(file):
         k = i
         first_pagraph = 1
         while 1:
-            k = find_token(file.body, "\\begin_layout Standard", k, j)
+            k = find_token(file.body, "\\begin_layout", k, j)
             if k == -1:
                 break
             if first_pagraph:
                 first_pagraph = 0
                 k = k + 1
                 continue
-            file.body[k:k] = ["\\begin_layout Standard", "",
+            file.body[k:k] = ['\\begin_layout %s' % file.default_layout, "",
                               "\\end_layout", ""]
             k = k + 5
             j = j + 4
@@ -2139,9 +2262,14 @@ def convert_ert_paragraphs(file):
             k = find_token(file.body, "\\newline", k, j)
             if k == -1:
                 break
-            file.body[k:k+1] = ["\\end_layout", "", "\\begin_layout Standard"]
+            file.body[k:k+1] = ["\\end_layout", "", '\\begin_layout %s' % file.default_layout]
             k = k + 4
             j = j + 3
+            # We need an empty line if file.default_layout == ''
+            if file.body[k-1] != '':
+                file.body.insert(k-1, '')
+                k = k + 1
+                j = j + 1
         i = i + 1
 
 
@@ -2182,9 +2310,14 @@ def revert_ert_paragraphs(file):
                 l = l + 1
             if strip(file.body[l]) and split(file.body[l])[0] == "\\newline":
                 file.body[k:l+1] = ["\\end_layout", "",
-                                    "\\begin_layout Standard"]
+                                    '\\begin_layout %s' % file.default_layout]
                 j = j - l + k + 2
                 k = k + 3
+                # We need an empty line if file.default_layout == ''
+                if file.body[l+1] != '':
+                    file.body.insert(l+1, '')
+                    k = k + 1
+                    j = j + 1
             else:
                 k = k + 1
         i = i + 1
@@ -2215,24 +2348,54 @@ def remove_paperpackage(file):
 
     paperpackage = split(file.header[i])[1]
 
-    if paperpackage in ("a4", "a4wide", "widemarginsa4"):
-        conv = {"a4":"\\usepackage{a4}","a4wide": "\\usepackage{a4wide}",
-                "widemarginsa4": "\\usepackage[widemargins]{a4}"}
-        # for compatibility we ensure it is the first entry in preamble
-        file.preamble[0:0] = [conv[paperpackage]]
-
     del file.header[i]
 
+    if paperpackage not in ("a4", "a4wide", "widemarginsa4"):
+        return
+
+    conv = {"a4":"\\usepackage{a4}","a4wide": "\\usepackage{a4wide}",
+            "widemarginsa4": "\\usepackage[widemargins]{a4}"}
+    # for compatibility we ensure it is the first entry in preamble
+    file.preamble[0:0] = [conv[paperpackage]]
+
     i = find_token(file.header, '\\papersize', 0)
     if i != -1:
         file.header[i] = "\\papersize default"
 
 
+def remove_quotestimes(file):
+    i = find_token(file.header, '\\quotes_times', 0)
+    if i == -1:
+        return
+    del file.header[i]
+
+
+##
+# Convert SGML paragraphs
+#
+def convert_sgml_paragraphs(file):
+    if file.backend != "docbook":
+        return
+
+    i = 0
+    while 1:
+        i = find_token(file.body, "\\begin_layout SGML", i)
+
+        if i == -1:
+            return
+
+        file.body[i] = "\\begin_layout Standard"
+        j = find_token(file.body, "\\end_layout", i)
+
+        file.body[j+1:j+1] = ['','\\end_inset','','','\\end_layout']
+        file.body[i+1:i+1] = ['\\begin_inset ERT','status inlined','','\\begin_layout Standard','']
+
+        i = i + 10
 ##
 # Convertion hub
 #
 
-convert = [[222, [insert_tracking_changes, add_end_header]],
+convert = [[222, [insert_tracking_changes, add_end_header, convert_amsmath]],
            [223, [remove_color_default, convert_spaces, convert_bibtex, remove_insetparent]],
            [224, [convert_external, convert_comment]],
            [225, [add_end_layout, layout2begin_layout, convert_end_document,
@@ -2255,9 +2418,13 @@ convert = [[222, [insert_tracking_changes, add_end_header]],
            [240, [convert_output_changes]],
            [241, [convert_ert_paragraphs]],
            [242, [convert_french]],
-           [243, [remove_paperpackage]]]
+           [243, [remove_paperpackage]],
+           [244, [rename_spaces]],
+           [245, [remove_quotestimes, convert_sgml_paragraphs]]]
 
-revert =  [[242, []],
+revert =  [[244, []],
+           [243, [revert_space_names]],
+           [242, []],
            [241, []],
            [240, [revert_ert_paragraphs]],
            [239, [revert_output_changes]],
@@ -2277,10 +2444,11 @@ revert =  [[242, []],
            [226, [revert_box, revert_external_2]],
            [225, [revert_note]],
            [224, [rm_end_layout, begin_layout2layout, revert_end_document,
-                  revert_valignment_middle, revert_breaks, convert_frameless_box]],
+                  revert_valignment_middle, revert_breaks, convert_frameless_box,
+                  remove_branches]],
            [223, [revert_external_2, revert_comment, revert_eqref]],
            [222, [revert_spaces, revert_bibtex]],
-           [221, [rm_end_header, rm_tracking_changes, rm_body_changes]]]
+           [221, [revert_amsmath, rm_end_header, rm_tracking_changes, rm_body_changes]]]
 
 
 if __name__ == "__main__":