orglink
Create orgmode link

Script key: Create orgmode link

Posted by deuvisfaecibusque

Install

Details

Label orglink
Voice Over Create orgmode link
Script
/* 
### Create orgmode link

#### if text in clipboard is NOT a URL:

- If no text is selected, insert [[][]] in cursor position, leaving cursor ready to type link text.
- If text is selected, insert [[][selectedText]] leaving cursor in parens to type URL.

#### If URL in Clipboard:

- If no text is selected, insert [[urlFromClipboard][]] in cursor position, leaving cursor ready to type link text
- If text is selected, insert [[urlFromClipboard][selectedText]] leaving cursor after close parens.
*/

// helper to test for URL
function isUrl(s) {
   var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
   return regexp.test(s);
}

// get clipboard and test if it's a URL
var clip = getClipboard();
var link = "";
if (isUrl(clip)) {
  link = clip;
}

var sel = getSelectedText();
var selRange = getSelectedRange();

if (!sel || sel.length == 0) {
  setSelectedText("[[" + link + "][]]");
  setSelectedRange(selRange[0]+link.length+4,0);
}
else {
  setSelectedText("[[" + link + "]["+sel+"]]");
  setSelectedRange(selRange[0]+selRange[1]+link.length-2,0);
}

Advanced

Keyboard shortcut None
Hide key Yes

Discussion

comments powered by Disqus