SublimeText / LaTeXTools

LaTeX plugin for Sublime Text
https://latextools.readthedocs.io/
2.01k stars 365 forks source link

Feature Request: Highlight certain words #1052

Open diego898 opened 7 years ago

diego898 commented 7 years ago

I frequently leave myself notes in my latex documents - sometimes directly in the rendered PDFs like this:

\newcommand{\needref}{\bt{[insert references]}}
\newcommand\todo{\bt{ TODO }}

other times in comments - I think it would be useful to add a tag/word highlighter to latextools so that if in comments I type @todo @author_name, etc. etc. Latextools can highlight them, similar to how https://github.com/aziz/PlainTasks highlights tags like @due, @today, @critical, etc.

I know this is probably too specific/not worth implementing - just figured I'd make a note of it here incase someone else wants it/searches for it. You can feel free to close.

Thanks for all your continued hard work!

r-stein commented 7 years ago

This is quite simple and actually I already have this for my personal use (just changed the regex for this example). Just select Tools >> Developer >> New Plugin... and paste:

import sublime
import sublime_plugin

class HighlightTodoListener(sublime_plugin.EventListener):
    def highlight_todo(self, view):
        # only highlight tex files
        if not view.score_selector(0, "text.tex"):
            return
        # adapt this regex as you wish
        regions = view.find_all(
            r"\\todo\b|" # todo command
            r"@(?:todo|author_name)\b" # annotations
        )

        view.erase_regions("td_highlighter")
        view.add_regions("td_highlighter", regions, "keyword"
                         # , flags=sublime.DRAW_OUTLINED
                         )

    on_load = highlight_todo
    on_post_save = highlight_todo

I think this is to specific to add it to LaTeXTools, although many users may have a similar command. However if there was some kind of standardization, I would be happy to add it.

diego898 commented 7 years ago

thanks @r-stein! - In theory I can see this reading from a project file or user settings for a list of words to highlight. I will begin messing around with this

r-stein commented 7 years ago

In practice you can easily use a list of words like this (I don't know how much you know about regex):

        commands = ["todo"]
        annotations = ["todo", "author_name"]
        regions = view.find_all(
            r"\\(?:{com})\b|"
            r"@(?:{anon})\b"
            .format(com="|".join(commands), anon="|".join(annotations))
        )
ig0774 commented 7 years ago

Three packages that it may be worth supporting for this feature:

There are several others on CTAN, but these look to be (relatively) actively maintained. They're mostly focused on providing in-text comments on drafts, but we could consider integrating them, perhaps also taking advantage of

diego898 commented 7 years ago

I have used the excellent todonotes package in the past, and would also support integration if possible!

ig0774 commented 7 years ago

I meant to add SublimeLinter to my last comment.

I'm voting to leave this issue open as it's a viable feature request that belongs somewhere in the queue.