+ 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