Last active
December 15, 2015 07:18
-
-
Save bjmorgan/5221948 to your computer and use it in GitHub Desktop.
Sublime Text 2 plugin for inserting \ref{<reference string>} code into LaTeX documents. The plugin scans the file for \label{<reference string>} commands (ignoring those in commented out lines), and presents a drop-down list of <reference string> options, alphabetically sorted.
The keymappings given here bind the command to ⌘-l, ⌘-r.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
// Run LaTeX plugin to insert reference code | |
{ | |
"keys": ["super+l", "super+r"], | |
"command": "insert_latex_reference", | |
"context": [ | |
{ "key": "selector", "operator": "equal", "operand": "text.tex.latex" } | |
] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime, sublime_plugin | |
import re | |
class InsertLatexReferenceCommand( sublime_plugin.TextCommand ): | |
def run(self, edit): | |
labels = self.get_labels() | |
self.view.window().show_quick_panel( self.labels, self.complete ) | |
def complete(self, choice): | |
if choice == -1: | |
return | |
label = self.labels[ choice ] | |
output_string = ( "\\ref{" + label + "}" ) | |
edit = self.view.begin_edit() | |
startloc = self.view.sel()[-1].end() | |
self.view.insert( edit, startloc, output_string ) | |
def get_labels(self): | |
pattern = '^[^%]*label\{+([^\}]+)\}' | |
fromPosition = 1 | |
self.labels = [] | |
self.view.find_all( pattern, 0, "$1", self.labels ) | |
if self.labels: | |
return self.labels.sort | |
else: | |
return '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
[ | |
{ "caption": "LaTeX: Select reference label", "command": "insert_latex_reference" } | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment