+def revert_Argument_to_TeX_brace(document, line, endline, n, nmax, environment, opt):
+ '''
+ Reverts an InsetArgument to TeX-code
+ usage:
+ revert_Argument_to_TeX_brace(document, LineOfBegin, LineOfEnd, StartArgument, EndArgument, isEnvironment, isOpt)
+ LineOfBegin is the line of the \\begin_layout or \\begin_inset statement
+ LineOfEnd is the line of the \end_layout or \end_inset statement, if "0" is given, the end of the file is used instead
+ StartArgument is the number of the first argument that needs to be converted
+ EndArgument is the number of the last argument that needs to be converted or the last defined one
+ isEnvironment must be true, if the layout is for a LaTeX environment
+ isOpt must be true, if the argument is an optional one
+ '''
+ lineArg = 0
+ wasOpt = False
+ while lineArg != -1 and n < nmax + 1:
+ lineArg = find_token(document.body, "\\begin_inset Argument " + str(n), line)
+ if lineArg > endline and endline != 0:
+ return wasOpt
+ if lineArg != -1:
+ beginPlain = find_token(document.body, "\\begin_layout Plain Layout", lineArg)
+ # we have to assure that no other inset is in the Argument
+ beginInset = find_token(document.body, "\\begin_inset", beginPlain)
+ endInset = find_token(document.body, "\\end_inset", beginPlain)
+ k = beginPlain + 1
+ l = k
+ while beginInset < endInset and beginInset != -1:
+ beginInset = find_token(document.body, "\\begin_inset", k)
+ endInset = find_token(document.body, "\\end_inset", l)
+ k = beginInset + 1
+ l = endInset + 1
+ if environment == False:
+ if opt == False:
+ document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("}{")
+ del(document.body[lineArg : beginPlain + 1])
+ wasOpt = False
+ else:
+ document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("]")
+ document.body[lineArg : beginPlain + 1] = put_cmd_in_ert("[")
+ wasOpt = True
+ else:
+ document.body[endInset - 2 : endInset + 1] = put_cmd_in_ert("}")
+ document.body[lineArg : beginPlain + 1] = put_cmd_in_ert("{")
+ wasOpt = False
+ n += 1
+ return wasOpt
+
+
+def convert_TeX_brace_to_Argument(document, line, n, nmax, inset, environment, opt):
+ '''
+ Converts TeX code for mandatory arguments to an InsetArgument
+ The conversion of TeX code for optional arguments must be done with another routine
+ !!! Be careful if the braces are different in your case as expected here:
+ - "}{" separates mandatory arguments of commands
+ - "}" + "{" separates mandatory arguments of commands
+ - "}" + " " + "{" separates mandatory arguments of commands
+ - { and } surround a mandatory argument of an environment
+ usage:
+ convert_TeX_brace_to_Argument(document, LineOfBeginLayout/Inset, StartArgument, EndArgument, isInset, isEnvironment, isOpt)
+ LineOfBeginLayout/Inset is the line of the \\begin_layout or \\begin_inset statement
+ StartArgument is the number of the first ERT that needs to be converted
+ EndArgument is the number of the last ERT that needs to be converted
+ isInset must be true, if braces inside an InsetLayout needs to be converted
+ isEnvironment must be true, if the layout is for a LaTeX environment
+ isOpt must be true, if the argument is an optional one
+
+ Todo: this routine can currently handle only one mandatory argument of environments
+ '''
+
+ end_layout = find_end_of_layout(document.body, line)
+ lineERT = line
+ endn = line
+ loop = 1
+ while n < nmax + 1:
+ lineERT = find_token(document.body, "\\begin_inset ERT", lineERT, end_layout)
+ if lineERT == -1:
+ break
+ if environment == False:
+ end_ERT = find_end_of_inset(document.body, lineERT)
+ if end_ERT == -1:
+ document.warning("Can't find end of ERT!!")
+ break
+ # Note that this only checks for ][ or }{ at the beginning of a line
+ if opt:
+ bracePair = find_token(document.body, "][", lineERT, end_ERT)
+ else:
+ bracePair = find_token(document.body, "}{", lineERT, end_ERT)
+ if bracePair != -1:
+ end = find_token(document.body, "\\end_inset", bracePair)
+ document.body[lineERT : end_ERT + 1] = ["\\end_layout", "", "\\end_inset"]
+ if loop == 1:
+ # in the case that n > 1 we have optional arguments before
+ # therefore detect them if any
+ if n > 1:
+ # first check if there is an argument
+ lineArg = find_token(document.body, "\\begin_inset Argument", line)
+ if lineArg < lineERT and lineArg != -1:
+ # we have an argument, so now search backwards for its end
+ # we must now assure that we don't find other insets like e.g. a newline
+ endInsetArg = lineERT
+ endLayoutArg = endInsetArg
+ while endInsetArg != endLayoutArg + 2 and endInsetArg != -1:
+ endInsetArg = endInsetArg - 1
+ endLayoutArg = endInsetArg
+ endInsetArg = find_token_backwards(document.body, "\\end_inset", endInsetArg)
+ endLayoutArg = find_token_backwards(document.body, "\\end_layout", endLayoutArg)
+ line = endInsetArg + 1
+ if inset == False:
+ document.body[line + 1 : line + 1] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+ else:
+ document.body[line + 4 : line + 4] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+ else: # if loop != 1
+ document.body[endn : endn] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+ n += 1
+ endn = end
+ loop += 1
+ else:
+ # no brace pair found
+ # now check the case that we have "}" + "{" in two ERTs
+ if opt:
+ endBrace = find_token(document.body, "]", lineERT, end_layout)
+ else:
+ endBrace = find_token(document.body, "}", lineERT, end_layout)
+ if endBrace == lineERT + 5:
+ if opt:
+ beginBrace = find_token(document.body, "[", endBrace, end_layout)
+ else:
+ beginBrace = find_token(document.body, "{", endBrace, end_layout)
+ # assure that the ERTs are consecutive (11 or 12 depending if there is a space between the ERTs or not)
+ if beginBrance != -1 and (beginBrace == endBrace + 11 or beginBrace == endBrace + 12):
+ end = find_token(document.body, "\\end_inset", beginBrace)
+ document.body[lineERT : end + 1] = ["\\end_layout", "", "\\end_inset"]
+ if loop == 1:
+ # in the case that n > 1 we have optional arguments before
+ # therefore detect them if any
+ if n > 1:
+ # first check if there is an argument
+ lineArg = find_token(document.body, "\\begin_inset Argument", line)
+ if lineArg < lineERT and lineArg != -1:
+ # we have an argument, so now search backwards for its end
+ # we must now assure that we don't find other insets like e.g. a newline
+ endInsetArg = lineERT
+ endLayoutArg = endInsetArg
+ while endInsetArg != endLayoutArg + 2 and endInsetArg != -1:
+ endInsetArg = endInsetArg - 1
+ endLayoutArg = endInsetArg
+ endInsetArg = find_token_backwards(document.body, "\\end_inset", endInsetArg)
+ endLayoutArg = find_token_backwards(document.body, "\\end_layout", endLayoutArg)
+ line = endInsetArg + 1
+ if inset == False:
+ document.body[line + 1 : line + 1] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+ else:
+ document.body[line + 4 : line + 4] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+ else:
+ document.body[endn : endn] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+ n += 1
+ loop += 1
+ # set the line where the next argument will be inserted
+ if beginBrace == endBrace + 11:
+ endn = end - 11
+ else:
+ endn = end - 12
+ else:
+ lineERT += 1
+ else:
+ lineERT += 1
+ if environment == True:
+ # FIXME This version of the routine does not check for and pass over
+ # arguments before n. So it attempts to process the argument in the
+ # document, no matter what has been specified.
+ #
+ # The other branch does do that, but probably that code would be better
+ # in a single location: Skip all those arguments, then process the ones
+ # we want.
+ end_ERT = find_end_of_inset(document.body, lineERT)
+ if end_ERT == -1:
+ document.warning("Can't find end of ERT!!")
+ break
+ # Note that this only checks for [ or { at the beginning of a line
+ if opt:
+ opening = find_token(document.body, "[", lineERT, end_ERT)
+ else:
+ opening = find_token(document.body, "{", lineERT, end_ERT)
+ if opening != -1:
+ lineERT2 = find_token(document.body, "\\begin_inset ERT", end_ERT, end_layout)
+ if lineERT2 == -1:
+ # argument in a single ERT
+ # strip off the opening bracket
+ document.body[opening] = document.body[opening][1:]
+ ertcontlastline = end_ERT - 3
+ if (opt and document.body[ertcontlastline].endswith("]")) or document.body[ertcontlastline].endswith("}"):
+ # strip off the closing bracket
+ document.body[ertcontlastline] = document.body[ertcontlastline][:-1]
+ end2 = find_token(document.body, "\\end_inset", ertcontlastline)
+ document.body[lineERT : lineERT + 1] = ["\\begin_inset Argument " + str(n)]
+ else:
+ end_ERT2 = find_end_of_inset(document.body, lineERT2)
+ if end_ERT2 == -1:
+ document.warning("Can't find end of second ERT!!")
+ break
+ if opt:
+ closing = find_token(document.body, "]", lineERT2, end_ERT2)
+ else:
+ closing = find_token(document.body, "}", lineERT2, end_ERT2)
+ if closing != -1: # assure that the "}" is in this ERT
+ end2 = find_token(document.body, "\\end_inset", closing)
+ document.body[lineERT2 : end2 + 1] = ["\\end_layout", "", "\\end_inset"]
+ document.body[lineERT : end_ERT + 1] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+ n += 1
+ else:
+ document.warning("Unable to process argument!")
+ n += 1
+
+