+ # Does the document use polyglossia?
+ use_polyglossia = False
+ if get_bool_value(document.header, "\\use_non_tex_fonts"):
+ i = find_token(document.header, "\\language_package")
+ if i == -1:
+ document.warning("Malformed document! Missing \\language_package")
+ else:
+ pack = get_value(document.header, "\\language_package", i)
+ if pack in ("default", "auto"):
+ use_polyglossia = True
+
+ # Do we use this language with polyglossia?
+ with_polyglossia = use_polyglossia and polyglossianame != ""
+ # Do we use this language with babel?
+ with_babel = with_polyglossia == False and babelname != ""
+
+ # Are we dealing with a primary or secondary language?
+ primary = document.language == lyxname
+ secondary = False
+
+ # Main language first
+ orig_doc_language = document.language
+ if primary:
+ # Change LyX document language to English (we will tell LaTeX
+ # to use the original language at the end of this function):
+ document.language = "english"
+ i = find_token(document.header, "\\language %s" % lyxname, 0)
+ if i != -1:
+ document.header[i] = "\\language english"
+
+ # Now look for occurences in the body
+ i = 0
+ while True:
+ i = find_token(document.body, "\\lang", i+1)
+ if i == -1:
+ 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