def _jumpto_ref(view, com_reg, pos): label_id = _get_selected_arg(view, com_reg, pos) if not label_id: return sublime.status_message( "Scanning document for label '{0}'...".format(label_id)) tex_root = get_tex_root(view) ana = analysis.analyze_document(tex_root) if ana is None: return def is_correct_label(c): return c.command == "label" and c.args == label_id labels = ana.filter_commands(is_correct_label) try: label = labels[0] except: message = "No matching label found for '{0}'.".format(label_id) print(message) sublime.status_message(message) return label_region = label.region message = "Jumping to label '{0}'.".format(label_id) print(message) sublime.status_message(message) utils.open_and_select_region(view, label.file_name, label_region)
def _jumpto_cite(view, com_reg, pos): tex_root = get_tex_root(view) bib_key = _get_selected_arg(view, com_reg, pos) if tex_root is None or not bib_key: return message = "Scanning bibliography files for key '{0}'...".format(bib_key) print(message) sublime.status_message(message) base_dir = os.path.dirname(tex_root) ana = analysis.get_analysis(tex_root) bib_commands = ana.filter_commands( ["bibliography", "nobibliography", "addbibresource"]) for bib_command in bib_commands: for bib_file in jumpto_tex_file._split_bib_args(bib_command.args): if not os.path.splitext(bib_file)[1]: bib_file += ".bib" bib_file = os.path.join(base_dir, bib_file) try: file_content = utils.read_file_unix_endings(bib_file) start = file_content.find(bib_key) end = start + len(bib_key) # check that we found the entry and we are not inside a word if (start == -1 or file_content[start - 1:start].isalnum() or file_content[end:end + 1].isalnum()): continue region = sublime.Region(start, end) message = "Jumping to bibliography key '{0}'.".format(bib_key) print(message) sublime.status_message(message) utils.open_and_select_region(view, bib_file, region) return except Exception as e: print("Error occurred opening file {0}".format(bib_file)) print(e) continue message = "Entry '{0}' not found in bibliography.".format(bib_key) print(message) sublime.status_message(message)
def _opt_jumpto_self_def_command(view, com_reg): tex_root = get_tex_root(view) if tex_root is None: return False # check in the cache whether we should jump (is command self defined) newcommand_keywords = ["newcommand", "renewcommand"] command = "\\" + com_reg.group("command") cana = analysis.get_analysis(tex_root) new_commands = cana.filter_commands(newcommand_keywords) if command not in [c.args for c in new_commands]: message = "Command not defined (cached) '{0}'".format(command) print(message) return False message =\ "Scanning document for command definition of '{0}'".format(command) print(message) sublime.status_message(message) # analyze the document to retrieve the correct position of the # command definition ana = analysis.analyze_document(tex_root) new_commands = ana.filter_commands(newcommand_keywords) try: new_com_def = next(ifilter(lambda c: c.args == command, new_commands)) except: message = "Command not self defined '{0}'".format(command) print(message) return False file_name = new_com_def.file_name region = new_com_def.args_region message = "Jumping to definition of '{0}'".format(command) print(message) sublime.status_message(message) utils.open_and_select_region(view, file_name, region) return True
def _show_usage_label(view, args): tex_root = get_tex_root(view) if tex_root is None: return False ana = analysis.analyze_document(tex_root) def is_correct_ref(c): command = ("\\" + c.command + "{")[::-1] return NEW_STYLE_REF_REGEX.match(command) and c.args == args refs = ana.filter_commands(is_correct_ref) if len(refs) == 0: sublime.error_message("No references for '{0}' found.".format(args)) return elif len(refs) == 1: ref = refs[0] utils.open_and_select_region(view, ref.file_name, ref.region) return captions = [ana_utils.create_rel_file_str(ana, r) for r in refs] quickpanel.show_quickpanel(captions, refs)
def _jumpto_glo(view, com_reg, pos, acr=False): tex_root = get_tex_root(view) if not tex_root: return ana = analysis.analyze_document(tex_root) if not acr: commands = ana.filter_commands( ["newglossaryentry", "longnewglossaryentry", "newacronym"]) else: commands = ana.filter_commands("newacronym") iden = com_reg.group("args") try: entry = next(c for c in commands if c.args == iden) except: message = "Glossary definition not found for '{0}'".format(iden) print(message) sublime.status_message(message) return message = "Jumping to Glossary '{0}'.".format(iden) print(message) sublime.status_message(message) utils.open_and_select_region(view, entry.file_name, entry.args_region)
def _jumpto_glo(view, com_reg, pos, acr=False): tex_root = get_tex_root(view) if not tex_root: return ana = analysis.analyze_document(tex_root) if not acr: commands = ana.filter_commands( ["newglossaryentry", "longnewglossaryentry"]) else: commands = ana.filter_commands("newacronym") iden = com_reg.group("args") try: entry = next(c for c in commands if c.args == iden) except: message = "Glossary definition not found for '{0}'".format(iden) print(message) sublime.status_message(message) return message = "Jumping to Glossary '{0}'.".format(iden) print(message) sublime.status_message(message) utils.open_and_select_region(view, entry.file_name, entry.args_region)
def _jumpto_tex_file(view, window, tex_root, file_name, auto_create_missing_folders, auto_insert_root): base_path, base_name = os.path.split(tex_root) _, ext = os.path.splitext(file_name) if not ext: file_name += '.tex' # clean-up any directory manipulating components file_name = os.path.normpath(file_name) containing_folder, file_name = os.path.split(file_name) # allow absolute paths on \include or \input isabs = os.path.isabs(containing_folder) if not isabs: containing_folder = os.path.normpath( os.path.join(base_path, containing_folder)) # create the missing folder if auto_create_missing_folders and\ not os.path.exists(containing_folder): try: os.makedirs(containing_folder) except OSError: # most likely a permissions error print('Error occurred while creating path "{0}"' .format(containing_folder)) traceback.print_last() else: print('Created folder: "{0}"'.format(containing_folder)) if not os.path.exists(containing_folder): sublime.status_message( "Cannot open tex file as folders are missing") return is_root_inserted = False full_new_path = os.path.join(containing_folder, file_name) if auto_insert_root and not os.path.exists(full_new_path): if isabs: root_path = tex_root else: root_path = os.path.join( os.path.relpath(base_path, containing_folder), base_name) # Use slashes consistent with TeX's usage if sublime.platform() == 'windows' and not isabs: root_path = root_path.replace('\\', '/') root_string = '%!TEX root = {0}\n'.format(root_path) try: with codecs.open(full_new_path, "w", "utf8")\ as new_file: new_file.write(root_string) is_root_inserted = True except OSError: print('An error occurred while creating file "{0}"' .format(file_name)) traceback.print_last() # open the file print("Open the file '{0}'".format(full_new_path)) # await opening and move cursor to end of the new view # (does not work on st2) if _ST3 and auto_insert_root and is_root_inserted: cursor_pos = len(root_string) new_region = sublime.Region(cursor_pos, cursor_pos) utils.open_and_select_region(view, full_new_path, new_region) else: window.open_file(full_new_path)
def _jumpto_tex_file(view, window, tex_root, file_name, auto_create_missing_folders, auto_insert_root): base_path, base_name = os.path.split(tex_root) _, ext = os.path.splitext(file_name) if not ext: file_name += '.tex' # clean-up any directory manipulating components file_name = os.path.normpath(file_name) containing_folder, file_name = os.path.split(file_name) # allow absolute paths on \include or \input isabs = os.path.isabs(containing_folder) if not isabs: containing_folder = os.path.normpath( os.path.join(base_path, containing_folder)) # create the missing folder if auto_create_missing_folders and\ not os.path.exists(containing_folder): try: os.makedirs(containing_folder) except OSError: # most likely a permissions error print('Error occurred while creating path "{0}"'.format( containing_folder)) traceback.print_last() else: print('Created folder: "{0}"'.format(containing_folder)) if not os.path.exists(containing_folder): sublime.status_message("Cannot open tex file as folders are missing") return is_root_inserted = False full_new_path = os.path.join(containing_folder, file_name) if auto_insert_root and not os.path.exists(full_new_path): if isabs: root_path = tex_root else: root_path = os.path.join( os.path.relpath(base_path, containing_folder), base_name) # Use slashes consistent with TeX's usage if sublime.platform() == 'windows' and not isabs: root_path = root_path.replace('\\', '/') root_string = '%!TEX root = {0}\n'.format(root_path) try: with codecs.open(full_new_path, "w", "utf8")\ as new_file: new_file.write(root_string) is_root_inserted = True except OSError: print('An error occurred while creating file "{0}"'.format( file_name)) traceback.print_last() # open the file print("Open the file '{0}'".format(full_new_path)) # await opening and move cursor to end of the new view # (does not work on st2) if _ST3 and auto_insert_root and is_root_inserted: cursor_pos = len(root_string) new_region = sublime.Region(cursor_pos, cursor_pos) utils.open_and_select_region(view, full_new_path, new_region) else: window.open_file(full_new_path)