+def revert_mbox_fbox(document):
+ 'Convert revert mbox/fbox boxes to TeX-code'
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Box", i)
+ if i == -1:
+ return
+ j = find_token(document.body, "width", i)
+ if j != i + 7:
+ document.warning("Malformed LyX document: Can't find box width")
+ return
+ width = get_value(document.body, "width", j)
+ k = find_end_of_inset(document.body, j)
+ if k == -1:
+ document.warning("Malformed LyX document: Can't find end of box inset")
+ i += 1
+ continue
+ BeginLayout = find_token(document.body, "\\begin_layout Plain Layout", j)
+ EndLayout = find_token(document.body, "\\end_layout", BeginLayout)
+ # replace if width is "-999col%"
+ if (width == '"-999col%"'):
+ document.body[EndLayout:k + 1] = put_cmd_in_ert("}")
+ if document.body[i] == "\\begin_inset Box Frameless":
+ document.body[i:BeginLayout + 1] = put_cmd_in_ert("\\mbox{")
+ if document.body[i] == "\\begin_inset Box Boxed":
+ document.body[i:BeginLayout + 1] = put_cmd_in_ert("\\fbox{")
+ i = i + 1
+
+
+def revert_starred_caption(document):
+ " Reverts unnumbered longtable caption insets "
+
+ i = 0
+ while True:
+ i = find_token(document.body, "\\begin_inset Caption LongTableNoNumber", i)
+ if i == -1:
+ return
+ # This is not equivalent, but since the caption inset is a full blown
+ # text inset a true conversion to ERT is too difficult.
+ document.body[i] = "\\begin_inset Caption Standard"
+ i = i + 1
+
+
+def revert_forced_local_layout(document):
+ i = 0
+ while True:
+ i = find_token(document.header, "\\begin_forced_local_layout", i)
+ if i == -1:
+ return
+ j = find_end_of(document.header, i, "\\begin_forced_local_layout", "\\end_forced_local_layout")
+ if j == -1:
+ # this should not happen
+ break
+ regexp = re.compile(r'\s*forcelocal', re.IGNORECASE)
+ k = find_re(document.header, regexp, i, j)
+ while k != -1:
+ del document.header[k]
+ j = j - 1
+ k = find_re(document.header, regexp, i, j)
+ k = find_token(document.header, "\\begin_local_layout", 0)
+ if k == -1:
+ document.header[i] = "\\begin_local_layout"
+ document.header[j] = "\\end_local_layout"
+ else:
+ l = find_end_of(document.header, k, "\\begin_local_layout", "\\end_local_layout")
+ if j == -1:
+ # this should not happen
+ break
+ lines = document.header[i+1 : j]
+ if k > i:
+ document.header[k+1 : k+1] = lines
+ document.header[i : j ] = []
+ else:
+ document.header[i : j ] = []
+ document.header[k+1 : k+1] = lines
+
+