-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
I had a specific need for a snippet, that would have current word (with fall back to selected text) to generate either a container tag or a single tag, depending on whether the ending forward slash was removed in the tab stop variable or not. I use this for the TXP bundle to generate a plugin tag, since those tags
would not get matched by the insert open/close tag command, (ctrl shift <) since it is impossible to know about all of them.
It uses the following snippet(with capture register 1 to decide weather the closing container tag should be inserted or not :
tag="${TM_CURRENT_WORD:-TM_SELECTED_TEXT}" tags="<txp:${1:$tag}${2: />}$3${2/ />|(.+)/(?1:</txp:$tag> )/}" echo "$tags"
The macro then selects the current word, triggers the command, which has output set as "Insert as Snippet" and tabs over the second tab stop variable.
Not knowing right now where to put it, I assigned it to :( ctrl shift > ) and named it simply: Tag/s
Here is how to use it:
write the tag name, trigger the macro, which will insert a single tag. If that is intended, then pressing one more time tab will leave the caret at the end of the single tag. When the tag is no single tag, the you just type : > and it will mirror itself inserting the corresponding closing tag,leaving a tab stop variable in between.
This is perfect, since I can then just press return and it will do the special HTML return behavior.
You can easily adapt this to your needs, for HTML or anything else. (I forgot to encounter for attributes though)
regards, marios
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
marios wrote:
I had a specific need for a snippet, that would have current word (with fall back to selected text) to generate either a container tag or a single tag, depending on whether the ending forward slash was removed in the tab stop variable or not. I use this for the TXP bundle to generate a plugin tag, since those tags
would not get matched by the insert open/close tag command, (ctrl shift <) since it is impossible to know about all of them.
It uses the following snippet(with capture register 1 to decide weather the closing container tag should be inserted or not :
tag="${TM_CURRENT_WORD:-TM_SELECTED_TEXT}" tags="<txp:${1:$tag}${2: />}$3${2/ />|(.+)/(?1:</txp:$tag> )/}" echo "$tags"
Ok, wrong thinking, since there isn't such and example in HTML. This version has attributes, and ommitts the first tab stop which isn't necessary anyway. So it looks like this now:
dtag="permlink" tag="${TM_CURRENT_WORD:-$TM_SELECTED_TEXT}" ctag="${tag:-$dtag}" tags="<txp:$ctag${1: insert="${2:0}"}${3: />}$4${3/ />|(.+)/(?1:</txp:$ctag> )/}" ptags="<txp:$ctag${1: />}$2${1/ />|(.+)/(?1:</txp:$ctag> )/}" if [[ $ctag = "permlink" ]]; then echo "$ptags" else echo "$tags" fi
Even better though, it now handles three different initial cases:
1)The tag name was typed, but there was no selection. 2)The tag name was typed and selected 3)There was no selection and nothing was typed,
Then in the latter case, this will insert a TXP Permlink Tag and once again it is possible to decide, whether one wants to insert a wrapper or a single tag. The Macro on top of the command was necessary in this case, to ged rid of the Current word, that was typed.
regards, marios
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
marios wrote:
marios wrote:
I had a specific need for a snippet, that would have current word (with fall back to selected text) to generate either a container tag or a single tag, depending on whether the ending forward slash was removed in the tab stop variable or not. I use this for the TXP bundle to generate a plugin tag, since those tags
would not get matched by the insert open/close tag command, (ctrl shift <) since it is impossible to know about all of them.
It uses the following snippet(with capture register 1 to decide weather the closing container tag should be inserted or not :
tag="${TM_CURRENT_WORD:-TM_SELECTED_TEXT}" tags="<txp:${1:$tag}${2: />}$3${2/ />|(.+)/(?1:</txp:$tag> )/}" echo "$tags"
Ok, wrong thinking, since there isn't such and example in HTML. This version has attributes, and ommitts the first tab stop which isn't necessary anyway. So it looks like this now:
dtag="permlink" tag="${TM_CURRENT_WORD:-$TM_SELECTED_TEXT}" ctag="${tag:-$dtag}" tags="<txp:$ctag${1: insert="${2:0}"}${3: />}$4${3/ />|(.+)/(?1:</txp:$ctag> )/}" ptags="<txp:$ctag${1: />}$2${1/ />|(.+)/(?1:</txp:$ctag> )/}" if [[ $ctag = "permlink" ]]; then echo "$ptags" else echo "$tags" fi
Even better though, it now handles three different initial cases:
1)The tag name was typed, but there was no selection. 2)The tag name was typed and selected 3)There was no selection and nothing was typed,
Then in the latter case, this will insert a TXP Permlink Tag and once again it is possible to decide, whether one wants to insert a wrapper or a single tag. The Macro on top of the command was necessary in this case, to ged rid of the Current word, that was typed.
Unfortunately, that hits on a certain limitation. It will fail in certain circumstances, that is. That is, because TM will match whitespace characters and the starting brackets of HTML tags as Current word as well.
I did some ctrl W on such arbitrary strings, to verify this, and this occurs to happen even on lines, that only have white-space characters.
I wonder, if this is expected behavior, however I managed to get around this with some replacement patterns on the variables. This works, but not if I trigger the macro in front of another Tag. Then in this case, it will pull in the starting bracket of the following tag and take that as Current word and thus spoil the empty Current word variable.
regards, marios