]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_2_3.py
When cleaning up before quitting, take care of exceptions
[lyx.git] / lib / lyx2lyx / lyx_2_3.py
index 5857b82a749a3a7163c0ffdb0e174d7a15238521..73ac45cf00c8d23c0db2c9c28b491e88034e0047 100644 (file)
@@ -1146,11 +1146,12 @@ def revert_noprefix(document):
             i += 1
             continue
         k = find_token(document.body, "LatexCommand labelonly", i, j)
-        if k == -1:
-            i = j
-            continue
-        noprefix = get_bool_value(document.body, "noprefix", i, j)
+        noprefix = False
+        if k != -1:
+            noprefix = get_bool_value(document.body, "noprefix", i, j)
         if not noprefix:
+            # either it was not a labelonly command, or else noprefix was not set.
+            # in that case, we just delete the option.
             del_token(document.body, "noprefix", i, j)
             i = j
             continue
@@ -1550,9 +1551,6 @@ command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
 def convert_literalparam(document):
     " Add param literal "
 
-    # These already had some sort of latexify method
-    latexified_insets = ["href", "index_print", "nomenclature"]
-
     for inset in command_insets:
         i = 0
         while True:
@@ -1566,7 +1564,8 @@ def convert_literalparam(document):
                 continue
             while i < j and document.body[i].strip() != '':
                 i += 1
-            if inset in latexified_insets:
+            # href is already fully latexified. Here we can switch off literal.
+            if inset == "href":
                 document.body.insert(i, "literal \"false\"")
             else:
                 document.body.insert(i, "literal \"true\"")
@@ -1984,7 +1983,7 @@ def convert_mathindent(document):
         document.header[i] = document.header[i].replace("fleqn,", "")
         j = find_re(document.header, regexp, 0)
         if i == j:
-            # then we have fleqn as the only option 
+            # then we have fleqn as the only option
             del document.header[i]
     else:
         document.header.insert(k, "\\is_math_indent 0")
@@ -2006,7 +2005,7 @@ def revert_mathindent(document):
         regexp = re.compile(r'(\\is_math_indent)')
         j = find_re(document.header, regexp, 0)
         del document.header[j]
-    else:    
+    else:
         k = find_token(document.header, "\\options", 0)
         if k != -1:
            document.header[k] = document.header[k].replace("\\options", "\\options fleqn,")
@@ -2074,7 +2073,7 @@ def revert_baselineskip(document):
         return
       else:
         document.body[hspaceLine: endInset + 1] = put_cmd_in_ert("\\hspace" + star + '{' + baselineskip + "\\baselineskip}")
-    
+
     i = i + 1
 
 
@@ -2101,7 +2100,7 @@ def revert_rotfloat(document):
     placement = document.body[i-2][beg+1:]
     # check if the option'H' is used
     if placement.find("H") != -1:
-      add_to_preamble(document, ["\\usepackage{float}"])  
+      add_to_preamble(document, ["\\usepackage{float}"])
     # now check if it is a starred type
     if document.body[i-1].find("wide true") != -1:
       star = '*'
@@ -2118,11 +2117,135 @@ def revert_rotfloat(document):
     else:
       document.body[endInset-2: endInset+1] = put_cmd_in_ert("\\end{sideways" + fType + star + '}')
       document.body[i-3: i+2] = put_cmd_in_ert("\\begin{sideways" + fType + star + "}[" + placement + ']')
-      add_to_preamble(document, ["\\usepackage{rotfloat}"])  
-    
+      add_to_preamble(document, ["\\usepackage{rotfloat}"])
+
     i = i + 1
 
 
+def convert_allowbreak(document):
+    " Zero widths Space-inset -> \SpecialChar allowbreak. "
+    body = "\n".join(document.body)
+    body = body.replace("\\begin_inset space \hspace{}\n"
+                        "\\length 0dd\n"
+                        "\\end_inset\n\n",
+                        "\\SpecialChar allowbreak\n")
+    document.body = body.split("\n")
+
+
+def revert_allowbreak(document):
+    " \SpecialChar allowbreak -> Zero widths Space-inset. "
+    body = "\n".join(document.body)
+    body = body.replace("\\SpecialChar allowbreak\n",
+                        "\\begin_inset space \hspace{}\n"
+                        "\\length 0dd\n"
+                        "\\end_inset\n\n")
+    document.body = body.split("\n")
+
+
+def convert_mathnumberpos(document):
+    " add the \\math_number_before tag "
+    # check if the document uses the class option "leqno"
+    k = find_token(document.header, "\\quotes_style", 0)
+    m = find_token(document.header, "\\options", 0)
+    regexp = re.compile(r'^.*leqno.*')
+    i = find_re(document.header, regexp, 0)
+    if i != -1 and i == m:
+        document.header.insert(k, "\\math_number_before 1")
+        # delete the found option
+        document.header[i] = document.header[i].replace(",leqno", "")
+        document.header[i] = document.header[i].replace(", leqno", "")
+        document.header[i] = document.header[i].replace("leqno,", "")
+        j = find_re(document.header, regexp, 0)
+        if i == j:
+            # then we have leqno as the only option
+            del document.header[i]
+    else:
+        document.header.insert(k, "\\math_number_before 0")
+
+
+def revert_mathnumberpos(document):
+    " add the document class option leqno"
+    regexp = re.compile(r'(\\math_number_before 1)')
+    i = find_re(document.header, regexp, 0)
+    if i == -1:
+        regexp = re.compile(r'(\\math_number_before)')
+        j = find_re(document.header, regexp, 0)
+        del document.header[j]
+    else:
+        k = find_token(document.header, "\\options", 0)
+        if k != -1:
+           document.header[k] = document.header[k].replace("\\options", "\\options leqno,")
+           del document.header[i]
+        else:
+            l = find_token(document.header, "\\use_default_options", 0)
+            document.header.insert(l, "\\options leqno")
+            del document.header[i + 1]
+
+
+def convert_mathnumberingname(document):
+    " rename the \\math_number_before tag to \\math_numbering_side "
+    regexp = re.compile(r'(\\math_number_before 1)')
+    i = find_re(document.header, regexp, 0)
+    if i != -1:
+        document.header[i] = "\\math_numbering_side left"
+    regexp = re.compile(r'(\\math_number_before 0)')
+    i = find_re(document.header, regexp, 0)
+    if i != -1:
+        document.header[i] = "\\math_numbering_side default"
+    # check if the document uses the class option "reqno"
+    k = find_token(document.header, "\\math_numbering_side", 0)
+    m = find_token(document.header, "\\options", 0)
+    regexp = re.compile(r'^.*reqno.*')
+    i = find_re(document.header, regexp, 0)
+    if i != -1 and i == m:
+        document.header[k] = "\\math_numbering_side right"
+        # delete the found option
+        document.header[i] = document.header[i].replace(",reqno", "")
+        document.header[i] = document.header[i].replace(", reqno", "")
+        document.header[i] = document.header[i].replace("reqno,", "")
+        j = find_re(document.header, regexp, 0)
+        if i == j:
+            # then we have reqno as the only option
+            del document.header[i]
+
+
+def revert_mathnumberingname(document):
+    " rename the \\math_numbering_side tag back to \\math_number_before "
+    # just rename
+    regexp = re.compile(r'(\\math_numbering_side left)')
+    i = find_re(document.header, regexp, 0)
+    if i != -1:
+        document.header[i] = "\\math_number_before 1"
+    # add the option reqno and delete the tag
+    regexp = re.compile(r'(\\math_numbering_side right)')
+    i = find_re(document.header, regexp, 0)
+    if i != -1:
+        document.header[i] = "\\math_number_before 0"
+        k = find_token(document.header, "\\options", 0)
+        if k != -1:
+           document.header[k] = document.header[k].replace("\\options", "\\options reqno,")
+        else:
+            l = find_token(document.header, "\\use_default_options", 0)
+            document.header.insert(l, "\\options reqno")
+    # add the math_number_before tag   
+    regexp = re.compile(r'(\\math_numbering_side default)')
+    i = find_re(document.header, regexp, 0)
+    if i != -1:
+        document.header[i] = "\\math_number_before 0"
+
+
+def convert_minted(document):
+    " add the \\use_minted tag "
+    document.header.insert(-1, "\\use_minted 0")
+
+
+def revert_minted(document):
+    " remove the \\use_minted tag "
+    i = find_token(document.header, "\\use_minted", 0)
+    if i != -1:
+        document.header.pop(i)
+
+
 ##
 # Conversion hub
 #
@@ -2160,10 +2283,18 @@ convert = [
            [537, []],
            [538, [convert_mathindent]],
            [539, []],
-           [540, []]
+           [540, []],
+           [541, [convert_allowbreak]],
+           [542, [convert_mathnumberpos]],
+           [543, [convert_mathnumberingname]],
+           [544, [convert_minted]]
           ]
 
 revert =  [
+           [543, [revert_minted]],
+           [542, [revert_mathnumberingname]],
+           [541, [revert_mathnumberpos]],
+           [540, [revert_allowbreak]],
            [539, [revert_rotfloat]],
            [538, [revert_baselineskip]],
            [537, [revert_mathindent]],