from parser_tools import find_token, find_end_of, find_tokens, \
find_end_of_inset, find_end_of_layout, find_token_backwards, \
- get_value, get_value_string
+ get_containing_inset, get_value, get_value_string
from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
i += 1
# Make sure it is actually in an inset!
# A normal line could begin with "LatexCommand nameref"!
- # We could just check document.lines[i-1], but that relies
- # upon something that might easily change.
- # So let's see if we're in a ref inset...
- stins = find_token_backwards(document.body, "\\begin_inset CommandInset ref", cmdloc)
+ stins, endins = get_containing_inset(document.body, cmdloc, \
+ "\\begin_inset CommandInset ref")
if stins == -1:
continue
- endins = find_end_of_inset(document.body, stins)
- if endins == -1:
- document.warning("Can't find end of inset at line " + stins + "!!")
- continue
- if endins < cmdloc:
- continue
# ok, so it is in an InsetRef
refline = find_token(document.body, "reference", stins, endins)
i += 1
# Make sure it is actually in an inset!
- # We could just check document.lines[i-1], but that relies
- # upon something that might easily change.
- # We'll look back a few lines.
- stins = cmdloc - 10
- if stins < 0:
- stins = 0
- stins = find_token(document.body, "\\begin_inset CommandInset ref", stins)
- if stins == -1 or stins > cmdloc:
- continue
- endins = find_end_of_inset(document.body, stins)
- if endins == -1:
- document.warning("Can't find end of inset at line " + stins + "!!")
- continue
- if endins < cmdloc:
+ stins, endins = get_containing_inset(document.body, \
+ cmdloc, "\\begin_inset CommandInset ref")
+ if stins == -1:
continue
document.body[cmdloc] = "LatexCommand nameref"
def find_end_of_layout(lines, i):
" Find end of layout, where lines[i] is included."
return find_end_of(lines, i, "\\begin_layout", "\\end_layout")
+
+
+# checks if line i is in the inset e.g., "\\begin_inset CommandInset ref"
+# if so, returns starting and ending lines
+# otherwise, returns (-1, -1)
+def get_containing_inset(lines, i, inset):
+ defval = (-1, -1)
+ stins = find_token_backwards(lines, inset, i)
+ if stins == -1:
+ return defval
+ endins = find_end_of_inset(lines, stins)
+ # note that this includes the notfound case.
+ if endins < i:
+ return defval
+ return (stins, endins)