- return
- if document.body[i].startswith('\\lang %s' % lyxname):
- if use_polyglossia and polyglossianame != "":
- add_to_preamble(document, ["\\AtBeginDocument{\setotherlanguage{%s}}" % polyglossianame])
- if (use_polyglossia and polyglossianame != "") or (use_polyglossia == False and babelname != ""):
- parent = get_containing_layout(document.body, i)
- document.body[parent[2] : parent[2]] = ["\\begin_layout Standard",
- "\\begin_inset ERT", "status open", "",
- "\\begin_layout Plain Layout", "", "",
- "\\backslash",
- "end{%s}" % envname,
- "\\end_layout", "", "\\end_inset", "", "",
- "\\end_layout", ""]
- del document.body[i]
- if (use_polyglossia and polyglossianame != "") or (use_polyglossia == False and babelname != ""):
- document.body[i : i] = ["\\begin_inset ERT", "status open", "",
- "\\begin_layout Plain Layout", "", "",
- "\\backslash",
- "begin{%s}" % envname,
- "\\end_layout", "", "\\end_inset", "", "",
- ""]
+ break
+ if document.body[i].startswith("\\lang %s" % lyxname):
+ secondary = True
+ texname = use_polyglossia and polyglossianame or babelname
+ elif primary and document.body[i].startswith("\\lang english"):
+ # Since we switched the main language manually, English parts need to be marked
+ texname = "english"
+ else:
+ continue
+
+ parent = get_containing_layout(document.body, i)
+ i_e = parent[2] # end line no,
+ # print(i, texname, parent, document.body[i+1], file=sys.stderr)
+
+ # Move leading space to the previous line:
+ if document.body[i+1].startswith(" "):
+ document.body[i+1] = document.body[i+1][1:]
+ document.body.insert(i, " ")
+ continue
+
+ # TODO: handle nesting issues with font attributes, e.g.
+ # \begin_layout Standard
+ #
+ # \emph on
+ # \lang macedonian
+ # Македонски јазик
+ # \emph default
+ # — јужнословенски јазик, дел од групата на словенски јазици од јазичното
+ # семејство на индоевропски јазици.
+ # Македонскиот е службен и национален јазик во Македонија.
+ # \end_layout
+
+ # Ensure correct handling of list labels
+ if (parent[0] in ["Labeling", "Description"]
+ and not " " in "\n".join(document.body[parent[3]:i])):
+ # line `i+1` is first line of a list item,
+ # part before a space character is the label
+ # TODO: insets or language change before first space character
+ labelline = document.body[i+1].split(' ', 1)
+ if len(labelline) > 1:
+ # Insert a space in the (original) document language
+ # between label and remainder.
+ # print(" Label:", labelline, file=sys.stderr)
+ lines = [labelline[0],
+ "\\lang %s" % orig_doc_language,
+ " ",
+ "\\lang %s" % (primary and "english" or lyxname),
+ labelline[1]]
+ document.body[i+1:i+2] = lines
+ i_e += 4
+
+ # Find out where to end the language change.
+ langswitch = i
+ while True:
+ langswitch = find_token(document.body, "\\lang", langswitch+1, i_e)
+ if langswitch == -1:
+ break
+ # print(" ", langswitch, document.body[langswitch], file=sys.stderr)
+ # skip insets
+ i_a = parent[3] # paragraph start line
+ container = get_containing_inset(document.body[i_a:i_e], langswitch-i_a)
+ if container and container[1] < langswitch-i_a and container[2] > langswitch-i_a:
+ # print(" inset", container, file=sys.stderr)
+ continue
+ i_e = langswitch
+ break
+
+ # use function or environment?
+ singlepar = i_e - i < 3
+ if not singlepar and parent[0] == "Plain Layout":
+ # environment not allowed in some insets
+ container = get_containing_inset(document.body, i)
+ singlepar = container[0] in singlepar_insets
+
+ # Delete empty language switches:
+ if not "".join(document.body[i+1:i_e]):
+ del document.body[i:i_e]
+ i -= 1
+ continue
+
+ if singlepar:
+ if with_polyglossia:
+ begin_cmd = "\\text%s{"%texname
+ elif with_babel:
+ begin_cmd = "\\foreignlanguage{%s}{" % texname
+ end_cmd = "}"
+ else:
+ if with_polyglossia:
+ begin_cmd = "\\begin{%s}"%texname
+ end_cmd = "\\end{%s}"%texname
+ elif with_babel:
+ begin_cmd = "\\begin{otherlanguage}{%s}" % texname
+ end_cmd = "\\end{otherlanguage}"
+
+ if (not primary or texname == "english"):
+ try:
+ document.body[i_e:i_e] = put_cmd_in_ert(end_cmd)
+ document.body[i+1:i+1] = put_cmd_in_ert(begin_cmd)
+ except UnboundLocalError:
+ pass
+ del document.body[i]
+
+ if not (primary or secondary):
+ return