def run(self): # Retrieve root file and dirname. view = self.window.active_view() root_file = getTeXRoot.get_tex_root(view) if root_file is None: msg = \ 'Could not find TEX root. Please ensure that either you ' + \ 'have configured a TEX root in your project settings or ' + \ 'have a LaTeX document open.' sublime.status_message(msg) print(msg) return if not os.path.isfile(root_file): message = "Could not find TEX root {0}.".format(root_file) sublime.status_message(message) print(message) return # clear the cache try: cache.LocalCache(root_file).invalidate() except: print('Error while trying to delete local cache') traceback.print_exc() aux_directory, aux_directory_setting = get_aux_directory( view, return_setting=True) output_directory, output_directory_setting = get_output_directory( view, return_setting=True) if aux_directory is not None: # we cannot delete the output directory on Windows in case # Sumatra is holding a reference to it if (sublime.platform() != 'windows' or aux_directory != output_directory): if aux_directory_setting.startswith('<<'): self._rmtree(aux_directory) else: self.delete_temp_files(aux_directory) if output_directory is not None: if output_directory_setting.startswith('<<'): # we cannot delete the output directory on Windows in case # Sumatra is holding a reference to it if sublime.platform() == 'windows': self._clear_dir(output_directory) else: self._rmtree(output_directory) else: self.delete_temp_files(output_directory) else: # if there is no output directory, we may need to clean files # in the main directory, even if aux_directory is used self.delete_temp_files(os.path.dirname(root_file)) sublime.status_message("Deleted temp files")
def find_bib_files(root): def _find_bib_files(): # the final list of bib files result = [] # a list of candidates bib files to check resources = [] # load the analysis doc = analysis.get_analysis(root) # we use ALL_COMMANDS here as any flag will filter some command # we want to support flags = analysis.ALL_COMMANDS | analysis.ONLY_COMMANDS_WITH_ARGS for c in doc.filter_commands(_bibfile_filter, flags=flags): # process the matching commands # \begin{refsection} / \newrefsection # resource is specified as an optional argument argument if (c.command == 'begin' or c.command == 'newrefsection'): # NB if the resource doesn't end with .bib, assume its a label # for a bibliography defined elsewhere or a non-.bib file # which we don't handle resources.extend([ s.strip() for s in (c.optargs or '').split(',') if s.endswith('.bib') ]) # \bibliography / \nobibliography elif c.command in MULTI_BIBCOMMANDS: for s in c.args.split(','): s = s.strip() if not s: continue if not s.endswith('.bib'): s += '.bib' resources.append(s) # standard biblatex ocmmands else: # bib file must be followed by .bib if c.args.endswith('.bib'): resources.append(c.args) # extract absolute filepath for each bib file rootdir = os.path.dirname(root) for res in resources: # We join with rootdir, the dir of the master file candidate_file = os.path.normpath(os.path.join(rootdir, res)) # if the file doesn't exist, search the default tex paths if not os.path.exists(candidate_file): candidate_file = kpsewhich(res, 'mlbib') if candidate_file is not None and os.path.exists(candidate_file): result.append(candidate_file) # remove duplicates return list(set(result)) # since the processing can be a bit intensive, cache the results return cache.LocalCache(root).cache('bib_files', _find_bib_files)
def get_own_env_completion(view): tex_root = get_tex_root(view) if not tex_root: return [] def make_completions(): ana = analysis.get_analysis(tex_root) return _make_own_env_completion(ana) return list( cache.LocalCache(tex_root).cache("own_env_completion", make_completions) or [])
def run(self): view = self.window.active_view() if view is None or not bool(view.score_selector(0, "text.tex.latex")): return tex_root = getTeXRoot.get_tex_root(view) if tex_root: # clear the cache try: cache.LocalCache(tex_root).invalidate() except: print('Error while trying to delete local cache') traceback.print_exc()
def get_own_command_completion(view): tex_root = get_tex_root(view) if not tex_root: return [] # use special handling (additional completions) for math mode math_selector = ("string.other.math.tex, " "string.other.math.latex, " "string.other.math.block.environment.latex") is_math = bool(view.score_selector(view.sel()[0].b, math_selector)) def make_completions(): ana = analysis.get_analysis(tex_root) return _make_own_command_completion(ana, is_math) cache_name = "own_command_completion" if is_math: cache_name += "_math" return list( cache.LocalCache(tex_root).cache(cache_name, make_completions) or [])
def find_bib_files(root): def _find_bib_files(): # the final list of bib files result = [] # a list of candidates bib files to check resources = [] # load the analysis doc = analysis.get_analysis(root) # we use ALL_COMMANDS here as any flag will filter some command # we want to support flags = analysis.ALL_COMMANDS | analysis.ONLY_COMMANDS_WITH_ARGS for c in doc.filter_commands(_bibfile_filter, flags=flags): # process the matching commands # \begin{refsection} / \newrefsection # resource is specified as an optional argument argument if (c.command == 'begin' or c.command == 'newrefsection'): # NB if the resource doesn't end with .bib, assume its a label # for a bibliography defined elsewhere or a non-.bib file # which we don't handle resources.extend([ s.strip() for s in (c.optargs or '').split(',') if s.endswith('.bib') ]) # \bibliography / \nobibliography elif c.command in MULTI_BIBCOMMANDS: for s in c.args.split(','): s = s.strip() if not s: continue if not s.endswith('.bib'): s += '.bib' resources.append(s) # standard biblatex commands else: # bib file must be followed by .bib if c.args.endswith('.bib'): resources.append(c.args) # extract absolute filepath for each bib file rootdir = os.path.dirname(root) for res in resources: # We join with rootdir, the dir of the master file candidate_file = os.path.normpath(os.path.join(rootdir, res)) # if the file doesn't exist, search the default tex paths if not os.path.exists(candidate_file): candidate_file = kpsewhich(res, 'mlbib') if candidate_file is not None and os.path.exists(candidate_file): result.append(candidate_file) # remove duplicates return list(set(result)) # since the processing can be a bit intensive, cache the results result = cache.LocalCache(root).cache('bib_files', _find_bib_files) # TODO temporary workaround to ensure the result is a sequence if not hasattr(type(result), '__iter__'): result = _find_bib_files() try: cache.LocalCache(root).set('bib_files', result) except: pass # if a an additional_file is set append it to the result additional_file = get_setting("additional_bibliography_file") if additional_file: def _make_abs_path(file_path): if not file_path.endswith(".bib"): file_path += ".bib" if not os.path.isabs(file_path): root_folder, _ = os.path.split(root) file_path = os.path.join(root_folder, file_path) return os.path.normpath(file_path) if isinstance(additional_file, str): additional_file = [additional_file] additional_file = map(_make_abs_path, additional_file) additional_file = filter(os.path.isfile, additional_file) result = list(additional_file) + list(result) return result