I'm trying to do the following:
LaTeX -> (text selected, keyboard shortcut pressed) -> \hyperref[LaTeX]{LaTeX}
I can't use user tags for this because it can't use selection twice, only once. I made this script by editing one of the simple, provided scripts for TeXWorks, which turns normal text into bolded text.
toggleHyperref.js
// TeXworksScript
// Title: Toggle Hyperlink
// Shortcut: Ctrl+Shift+H
// Description: Converts the current selection to be a hyperlink.
// Author: Robo
// Version: 0.1
// Date: 2018-01-06
// Script-Type: standalone
// Context: TeXDocument
function addOrRemove(prefix, midfix, suffix) {
var txt = TW.target.selection;
var len = txt.length;
var wrapped = prefix + txt + midfix + txt + suffix;
var pos = TW.target.selectionStart;
if (pos >= prefix.length) {
TW.target.selectRange(pos - prefix.length, wrapped.length);
if (TW.target.selection == wrapped) {
TW.target.insertText(txt);
TW.target.selectRange(pos - prefix.length, len);
return;
}
TW.target.selectRange(pos, len);
}
TW.target.insertText(wrapped);
TW.target.selectRange(pos + prefix.length, len);
return;
};
addOrRemove("\\hyperref[", "]{", "}");
For reference, this is what a .tms script (provided by TeXMaker) looks like:
hardwordwrap_selection_80col.tms
/***************************************************************************
* copyright : (C) 2003-2014 by Pascal Brachet *
* http://www.xm1math.net/texmaker/ *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
function wordwrap( str, width, brk, cut ) {
brk = brk || '\n';
width = width || 75;
cut = cut || false;
if (!str) { return str; }
var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
return str.match( RegExp(regex, 'g') ).join( brk );
}
var textwidth=80;
var txt = TM.selection;
if (txt != "") {
var pos = TM.selectionStart;
var wraptext="";
var lines = txt.split('\n');
for (i = 0; i < lines.length; ++i) {
var line = lines[i];
wraptext+=wordwrap(line, textwidth, '\n')+"\n";
}
TM.insertText(wraptext);
TM.selectRange(pos, wraptext.length);
}
undefined;
Thanks!