+ return default
+ if len(lines[i].split()) > 1:
+ for k in range (0, len(lines[i])):
+ if lines[i][k] == ' ':
+ if trim ==False:
+ return lines[i][k+1:len(lines[i])]
+ else:
+ return lines[i][k+2:len(lines[i])-1]
+ else:
+ return default
+
+
+def del_token(lines, token, start, end):
+ """ del_token(lines, token, start, end) -> int
+
+ Find the lower line in lines where token is the first element and
+ delete that line.
+
+ Returns the number of lines remaining."""
+
+ k = find_token_exact(lines, token, start, end)
+ if k == -1:
+ return end
+ else:
+ del lines[k]
+ return end - 1
+
+
+def find_beginning_of(lines, i, start_token, end_token):
+ count = 1
+ while i > 0:
+ i = find_tokens_backwards(lines, [start_token, end_token], i-1)
+ if i == -1:
+ return -1
+ if check_token(lines[i], end_token):
+ count = count+1
+ else:
+ count = count-1
+ if count == 0:
+ return i
+ return -1
+
+
+def find_end_of(lines, i, start_token, end_token):
+ count = 1
+ n = len(lines)
+ while i < n:
+ i = find_tokens(lines, [end_token, start_token], i+1)
+ if i == -1:
+ return -1
+ if check_token(lines[i], start_token):
+ count = count+1
+ else:
+ count = count-1
+ if count == 0:
+ return i
+ return -1
+
+
+def find_nonempty_line(lines, start, end = 0):
+ if end == 0:
+ end = len(lines)
+ for i in xrange(start, end):
+ if is_nonempty_line(lines[i]):
+ return i
+ return -1
+
+
+def find_end_of_inset(lines, i):
+ " Find end of inset, where lines[i] is included."
+ return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
+
+
+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)