#!/usr/bin/env ruby # # This script will expand the current word either to # \word{} (if it's matched by the commands pattern), # to one of the shortcuts in the shortcut dictionary, # otherwise to \begin{word} … \end{word} with the caret # inbetween. # # With no selection, it will insert \begin{environment} # … \end{environment} and allow you to overwrite the # environment name. testing = ENV['TM_LATEX_COMMANDS'].to_s commands = /^cite|footcite|footnote|label|ref|text(it|bf|tt|sf|sc)|#{testing}$/ customShortcuts = Hash.new ENV['TM_LATEX_SHORTCUTS'].to_s.split(',').each { |x| temp = x.split('=>') customShortcuts["#{temp[0]}"]="#{temp[1]}" } shortcuts = { "it" => "textit", "bf" => "textbf", "tt" => "texttt", "sf" => "textsf", "sc" => "textsc", "em" => "emph", "fc" => "footcite", "fn" => "footnote", "eqn" => "equation", "item" => "itemize", "enum" => "enumerate" }.update(customShortcuts) name = STDIN.read if(!shortcuts[name].nil?) name = shortcuts[name] end if(name == '') print("\\begin{${1:environment}}\n\t$2\n\\end{$1}") elsif(commands.match(name)) print("\\#{name}{$1}") else lab = ENV['TM_LATEX_INSERT_LABEL'].to_i if(lab == 1) labPrefix = name.slice(0,3) print("\\begin{#{name}}\n\\label{#{labPrefix}:$1}\n\t$2\n\\end{#{name}}") else print("\\begin{#{name}}\n\t$1\n\\end{#{name}}") end end