HOWTO: Extract Tag Name
Niko Dittmann wrote a command to lookup the current HTML tag on http://de.selfhtml.org/ (hint: by default, pressing ⌃H on a tag name will show the W3C documentation for that tag, this also works for CSS selectors, PHP/Ruby functions, Cocoa stuff, and outside of HTML, things for which there is a manual page, etc.)
TextMate exports the current word as the TM_CURRENT_WORD
environment variable (which can be used in your script), but as it would be nice to have the command find whatever tag name is to the left of the caret, the following solution was proposed (using Ruby) instead of settling on just the current word:
line = ENV["TM_CURRENT_LINE"].to_s
col = ENV["TM_LINE_INDEX"].to_i
if line =~ /\A.{0,#{col}}
This grabs the entire line and the carets position (both exported as environment variables) and then uses a regular expression which skips at most all the characters to the left of the caret, ensuring that it finds a tag name after having skipped these (it’s greedy, so it will skip as many as possible, still allowing us to match a tag.)
This approach is useful with other commands as well, when these need to grab a “unit” from the current line. For example a command which opens the current URL, inserts width/height attributes into the current `` tag or similar. The command can be set to replace the line it works on, optionally as a snippet, making it easy to write such functionality.
If you adopt Niko’s command, or something similar, you can give it a scope selector of meta.tag
, that way, only inside tags will the key equivalent be checked, making it possible to re-use the key equivalent in other contexts (as done e.g. by the ⌃H convention.)