The following snippet creates a hyperlink by combining the selected text in a TeX file with (what is hopefully a URL in) the clipboard,
{
"key": "cmd+l",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection && resourceExtname == .tex",
"args": {
"snippet": "\\href{${CLIPBOARD:url}}{${TM_SELECTED_TEXT}}$0"
}
}
Is there a way to make the inserted clipboard content selected when executing the snippet? Moreover, is there a way to check whether the clipboard is a valid web or email address and only then insert it?
Update
Here's a non-working attempt at using a regex to only paste the clipboard content, when it's a url.
{
"key": "cmd+l",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection && resourceExtname == .tex",
"args": {
"snippet": "\\href{${1:${CLIPBOARD/(\\^\\(\\(https\\?\\)://\\)\\?\\(\\[w\\|W\\]\\{3\\}\\.\\)\\+\\[a-zA-Z0-9-\\.\\]\\{3,\\}\\.\\[a-zA-Z\\]\\{2,\\}\\(\\.\\[a-zA-Z\\]\\{2,\\}\\)\\?\\$)/$1/}}}{${TM_SELECTED_TEXT}}$0"
}
}
"snippet": "\\href{${1:${CLIPBOARD}}}{${TM_SELECTED_TEXT}}$0
will print and select the CLIPBOARD
content when you trigger the snippet. You will lose the url
hint though, I don't think there is a way to save that.
Your second question: "is there a way to check whether the clipboard is a valid web or email address and only then insert it?"
I believe so:
${1:${CLIPBOARD/(..your regex test for email validity here..)/$1/}}
EDIT (to check for valid emails) - tested:
"snippet": "\\href{${1:${CLIPBOARD/([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,})?(.*)/$1/i}}}{${TM_SELECTED_TEXT}}$0"
I got the email regex from www.regular-expressions.info.
This seems to work. The key is that with snippet transforms if you don't match, in this case the CLIPBOARD variable content, the unmatched content will pass through and be printed. In other words, if you don't capture it, it will pass through so the only way to prevent a bad email or url from passing through and being printed is to capture both a good email/url and the bad version and only print the good capture group.
So this is the basic form: (regex email check here)?(.*)
So group1 has the valid email, if any, and group2 has the ClipBoard content if it is not a valid email. And only group1 is printed. Neat. Seems to work.
EDIT 2 (to check for valid urls) - pseudocode:
"snippet": "\\href{${1:${CLIPBOARD/(...url regex checker here...)?(.*)/$1/}}}{${TM_SELECTED_TEXT}}$0"
so I just added ?(.*)
to the end of an url regex check.
EDIT 2.5 Here is an url checker based on the one used in your Update snippet:
"snippet": "\\href{${1:${CLIPBOARD/^((https?:\\/\\/)?([w]{3}\\.)+[a-z0-9-]{3,}\\.[a-z]{2,}(\\.[a-z]{2,})?)?(.*)$/$1${5:+bad url}/i}}}{${TM_SELECTED_TEXT}}$0"
Note: regex's to validate urls are extremely tricky - if not impossible - I used the one from the question and make no warranties about its completeness. At least you can see how to escape
the regex.
EDIT 3 email/url (if there is some text you want when a bad email/url is in the variable, in this case, $CLIPBOARD, but could be any variable):
Add this conditional $1${2:+bad email}
to the replacement part of the snippet transform, so for the email version:
"snippet": "\\href{${1:${CLIPBOARD/([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,})?(.*)/$1${2:+bad email}/i}}}{${TM_SELECTED_TEXT}}$0" // email validater
whatever text you want in the bad email
place. Note, that checks if there is non-null content in group 2, if so, print "bad email". Your regex may be using a different group to capture "bad emails/urls" - use that group in the conditional ${2:+bad email}
.