/*
Poor man's replace - 2007 - written by vbr - Vlastimil Brom; vbr@email.cz; comments and feedback welcome
http://www.vbr.wz.cz/pspad/poor-man-s-REplace.html
experimental version - not thoroughly tested! meant as temporary rough "low cost" replacement tool; without any warranty :-)
in particular, the scripts work with ANSI characters only !! - in case, something went wrong,
the suplementary HTML file is displayed consisting of unicode entities, but the text itself IS CORRUPTED !!
dialog fields are remembered only within each run of PSPad - or untill calling recompile scripts
requires some newer version - tested (a bit) on PSPad 4.5.3 (2262, 2265, 2296),
requires a version of PSPad supporting InputText(); blockBeginX() ... , setBlockEnd() ...
DOESN'T work on the stable version 4.5.2 (2240)
all expressions to be used without quotes between slashes / / in the search pattern and between quotes " " in the replace pattern
a slash in the search expression as well as the quote in the replace pattern must be escaped using backslash! i.e.: \/ or \"
replacing a newline with something - mostly using "\r\n"
single "\r" matches after the last character of the line (if the newline is present)
single "\n" matches before the first character of the line (if the newline preceedes)
replacing something with newline - only: "\n" OR "\r" in the replacement pattern
any character including newline: "(?:(?:\r\n)|.)"
support of the lookahead assertions: positive "(?=abc)" and negative "(?!abc)"
"reReplace" - "nahradit RE" - obecné nahrazení textu pomocí regul[éá]rních výrazů
"reReplaceAllEditors" "nahrazení RE ve všech otevřených oknech editoru (celé texty)"
"reReplaceSeq" - "provede postupně řadu re nahrazení podle seznamu reg. výrazů (s nastavitelným oddělovačem)"
"joinLines" - "spojit řádky" - nahrazení "\r\n|\r|\n" zadaným oddělovačem
"splitLines" - "rozdělit řádky" - nahrazení zadaného oddělovače (RE) za "\n"
"RE search" - "hledání RE" ("najít předchozí", "najít další", "počet nálezů", "výpis nálezů", "výpis nevyhovujících")
"character codes" - "kódy znaků (tab.)" - vytvoří zdrojový kód HTML s tabulkou znaků vstupního textu (výběr nebo celý text) - vlastní znak, literál unicode (hex.), decimální znakové entity HTML, hexadecimální znakové entity HTML
"text stat" - "text stat - jednoduché HTML výpisy statistiky textu - počet slov a znaků; řazení: abecedně, frekvenčně; vzestupně, sestupně; s případným převedením na malá písmena; nastavitelný re výraz pro "slovo" a "znak"
"unicode literals (sel.)" - "literály unic. (výb.)" - převede vybraný text na literály unicode - použitelné např. pro nahrazování funkcí "nahradit RE"
"hex entities (sel.)" - "hex entity (výb.)" - převede vybraný text na hexadecimální znakové entity HTML
"dec entities (sel.)" - "dec entity (výb.)" - převede vybraný text na decimální znakové entity HTML
"plain text (sel.)" - "text (výb.)" - zobrazí vybraný text jako prostý jednořádkový řetězec (jen ANSI)
"eval" - "eval" - vyhodnotí pomocí eval(); primárním vstupem je vybraný text; výsledek je vložen na pozici kurzoru a zvýrazněn výběrem (předtím vybraný text zůstává zachován)
"ev. fromCharCode" - "ev. znaky - dec unic." - znak podle hodnoty unicode (decimální)
"ev. charCode seq." - "ev. zn. - dec unic. - řada" - zobrazí řadu znaků v zadaném intervalu hodnot unicode (decimální)
"ev. number seq." - "ev. řada čísel" - vytvoří číselnou řadu
requires at least PSPad version 4.5.3 build 2262 InputText() supporting the storno_value
*/
var module_name = "re_replace_js_c";
// var module_ver = "0.0.1"; // 11. 3. 2007
// var module_ver = "0.0.2"; // 13. 3. 2007 // new functions // evalInput(); convertSelection() unicLit, hexEnt, decEnt, string ; textToCharCodeTab(); fromCharCode(); fromCharCodeSeq(); numberSeq();
// var module_ver = "0.0.3"; // 17. 3. 2007 // new functions // textInfo() reCount() reSearchList() reSearch()
// var module_ver = "0.0.4"; // 22. 3. 2007 // all functions (including joinLines, splitLines) now respect the cancel button (see the hack with stornoValue) reSearch should work better according to the caret position and also a bit faster
// var module_ver = "0.0.5"; // 5. 4. 2007 // new function reWordBoundary setting the caret to the next word boundary - according to the given RE pattern; selection mode supported; reCount now supports selection
// var module_ver = "0.0.6"; // 11. 6. 2007 // new function reReplaceSeq - performs multiple re replacements
// var module_ver = "0.0.7"; // 24. 11. 2007 // new function reReplaceAllEditors - performs re replacements in all open editors (whole texts); fixes for function calls with no editors open; numbering of the SupplementaryResult-files
// var module_ver = "0.0.8"; // 12. 1. 2008 // correction in checkAndSupplResult; added a global variable - array - globParamArr to store values in the replacement function etc.
// var module_ver = "0.0.9"; // 17. 10. 2008 // some additional functions: newTextFile() (without the file-type dialog); lineUpCaretX, lineDownCaretX ... for moving the caret vertically while retaining the previously stored column position
// var module_ver = "0.0.10"; // 1. 12. 2008 // additional function: copyWithLineNrs()
var module_ver = "0.0.11"; // 10. 1. 2009 // additional function: duplTab()
var stornoValue = "\n\n\r";// a random sequence of system symbols - dec. code 1-31, which should not be inserted normally
function textInfoAlphAscImpl(){ textInfo(false, false, "alph", "asc"); }
function textInfoAlphDescImpl(){ textInfo(false, false, "alph", "desc"); }
function textInfoNumAscImpl(){ textInfo(false, false, "num", "asc"); }
function textInfoNumDescImpl(){ textInfo(false, false, "num", "desc"); }
function textInfoAlphAscImplLc(){ textInfo(false, true, "alph", "asc"); }
function textInfoAlphDescImplLc(){ textInfo(false, true, "alph", "desc"); }
function textInfoNumAscImplLc(){ textInfo(false, true, "num", "asc"); }
function textInfoNumDescImplLc(){ textInfo(false, true, "num", "desc"); }
function textInfoAlphAscAsk(){ textInfo(true, false, "alph", "asc"); }
var reCharPatTxtMem = "/[^\\r\\n]/g";
var reWordPatTxtMem = "/[^ \\r\\n\\t,.;:\"\\{\\}\\[\\]()!?+\/*-]+/g";
function textInfo(askCharWordRE, lowerCase, sortType, sortDir){
// generates a table of words and characters of the text
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var inputTxt = actEd.text();
if (lowerCase == true){
inputTxt = inputTxt.toLowerCase();
}
if (askCharWordRE == true){
var reWordPatTxt = InputText("Zadejte re výraz pro SLOVO (statistika textu) - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", reWordPatTxtMem, stornoValue);
var reCharPatTxt = InputText("Zadejte re výraz pro ZNAK (statistika textu) - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti",reCharPatTxtMem, stornoValue);
}else{
reWordPatTxt = reWordPatTxtMem;
reCharPatTxt = reCharPatTxtMem;
}
if((reWordPatTxt == stornoValue)||(reCharPatTxt == stornoValue)){
}else{
reWordPatTxtMem = reWordPatTxt;
reCharPatTxtMem = reCharPatTxt;
// indexOf() // JavaScript 1.6 ... mozilla
// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
// http://4umi.com/web/javascript/array.htm
Array.prototype.indexOf = function( v, b, s ) {
for( var i = +b || 0, l = this.length; i < l; i++ ) {
if( this[i]===v || s && this[i]==v ) { return i; }
}
return -1;
};
var arrChars = new Array();
var arrCharsWithFreq = new Array();
var arrWords = new Array();
var arrWordsWithFreq = new Array();
var reChars = eval('new RegExp('+reCharPatTxt+');');
var charCount = 0;
while ((matchObj = reChars.exec(inputTxt)) != null){
charCount ++;
var arrPos = arrChars.indexOf(matchObj[0]);
if(arrPos < 0){
arrChars[arrChars.length] = matchObj[0];
arrCharsWithFreq[arrCharsWithFreq.length] = new Array(matchObj[0],1);
}else{
arrCharsWithFreq[arrPos][1]++;
}
}
var reWords = eval('new RegExp('+reWordPatTxt+');');
var wordCount = 0;
while ((matchObj = reWords.exec(inputTxt)) != null){
wordCount ++;
var arrPos = arrWords.indexOf(matchObj[0]);
if(arrPos < 0){
arrWords[arrWords.length] = matchObj[0];
arrWordsWithFreq[arrWordsWithFreq.length] = new Array(matchObj[0],1);
}else{
arrWordsWithFreq[arrPos][1]++;
}
}
// helper functions for sorting arrays / second items are compared - num
function numSortFreqAsc(arrNum1, arrNum2){
return arrNum1[1]-arrNum2[1];
}
function numSortFreqDesc(arrNum1, arrNum2){
return arrNum2[1]-arrNum1[1];
}
var sortModeInfoText = "(setříděno ";
arrCharsWithFreq.sort(); //alpha sort in all cases
arrWordsWithFreq.sort();
if(sortType=="alph"){ // first sub array - string, implicit string sorting ??
sortModeInfoText += "abecedně (bez locale), ";
if (sortDir =="desc"){
sortModeInfoText += "sestupně; ";
arrCharsWithFreq.reverse();
arrWordsWithFreq.reverse();
}else{
sortModeInfoText += "vzestupně; ";
}
}else if(sortType=="num"){
sortModeInfoText += "číselně, ";
if (sortDir =="asc"){
sortModeInfoText += "vzestupně; ";
arrCharsWithFreq.sort(numSortFreqAsc);
arrWordsWithFreq.sort(numSortFreqAsc);
}else if (sortDir =="desc"){
sortModeInfoText += "sestupně; ";
arrCharsWithFreq.sort(numSortFreqDesc);
arrWordsWithFreq.sort(numSortFreqDesc);
}
}
if (lowerCase ==false){
sortModeInfoText += "bez změny velikosti písmen)";
}else{
sortModeInfoText += "převedeno na malá písmena)";
}
var htmlHeader = '\n\n
\n\nJednoduchá statistika textu - slova, znaky ...\n\n\nJednoduchá statistika textu - počty slov a znaků ...
\n';
var htmlFooter = '\n';
var titleInfoWords = "\nJednoduchá statistika slov - reg. výraz pro \"slovo\": "+reWordPatTxt+"
\nPočet nalezených slov: "+wordCount+".
\n"+sortModeInfoText+"
\n";
var titleInfoChars = "\nJednoduchá statistika znaků - reg. výraz pro \"znak\": "+reCharPatTxt+"
\nPočet nalezených znaků: "+charCount+".
\n"+sortModeInfoText+"
\n";
var tableContentWords = '
\nslovo | frequence | % slov |
';
for (var i=0; i < arrWordsWithFreq.length; i++){
tableContentWords += '\n'+textToUnicEntities(arrWordsWithFreq[i][0])+' | '+arrWordsWithFreq[i][1]+' | '+(arrWordsWithFreq[i][1]/wordCount*100)+'% |
';
}
tableContentWords += '\n
\n';
var tableContentChars = '
\nznak | frequence | % znaků |
';
for (var i=0; i < arrCharsWithFreq.length; i++){
tableContentChars += '\n'+textToUnicEntities(arrCharsWithFreq[i][0])+' | '+arrCharsWithFreq[i][1]+' | '+(arrCharsWithFreq[i][1]/charCount*100)+'% |
';
}
tableContentChars += '\n
\n';
outputEditor = newEditor();
outputEditor.newFile("TextStatisticsTab.html");
outputEditor.text(htmlHeader+titleInfoWords+tableContentWords+"
"+titleInfoChars+tableContentChars+htmlFooter);
}
}
function textToUnicEntities(textToConv){
// replaces non basic latin characters and some special chars with HTML entities
textToConv = textToConv.replace(/&/g,"&");
textToConv = textToConv.replace(/"); // all chars below 128 - not affected by the following code
var outputStr ="";
for (var i=0; i 0){
var inputTxt = actEd.selText();
var infoSrcTxt = "ve výběru";
}else{
var inputTxt = actEd.text();
var infoSrcTxt = "v celém textu";
}
var rePatText = InputText("Zadejte re výraz (POČET nálezů) - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", rePatSearchTextMem, stornoValue);
if (rePatText == stornoValue){
}else{
rePatSearchTextMem = rePatText;
var reInst = eval('new RegExp('+rePatText+');');
var matchObj;
var arrStartInd = new Array();
while ((matchObj = reInst.exec(inputTxt)) != null){
arrStartInd[arrStartInd.length] = matchObj.index;
}
arrEOLendInd = new Array();
arrEOLendInd[0] = 0; // first index pos
var EOLregExp = new RegExp("\r\n|\r|\n", "g");
while ((matchEOLobj = EOLregExp.exec(inputTxtCompl)) != null){
arrEOLendInd[arrEOLendInd.length] = matchEOLobj.lastIndex; //index;
}
arrEOLendInd[arrEOLendInd.length] = inputTxtCompl.length+1; // dummy last index pos + 1
var origCaretX = actEd.caretX();
var origCaretY = actEd.caretY();
var stringPosCaret = arrEOLendInd[origCaretY-1]+origCaretX-1; // string index position of the caret
var matchesBeforeCaret = 0;
for (var i=0; i <= arrStartInd.length-1; i++){
if (stringPosCaret > arrStartInd[i]){
matchesBeforeCaret ++;
}else{
break;
}
}
var infoResultTxt = "POČET NÁLEZŮ reg. výrazu: \""+rePatText+"\" "+infoSrcTxt+";\n celkový počet: "+arrStartInd.length+".\n ("+matchesBeforeCaret+" před kurzorem; "+(arrStartInd.length-matchesBeforeCaret)+" za kurzorem).\n";
echo(infoResultTxt);
}
}
function reListSearch(){ reSearchList(false); }
function reListSearchExcl(){ reSearchList(true); }
var rePatTextBaseREMem = '/[^\\r\\n]+/g';
function reSearchList(checkNotMatchingSubRE){
// produces the list of re matches - of the whole text - in a new text window
// if checkNotMatchingSubRE is true, the function prompts for a re to find and also for a NEGATIVE RE pattern, that MUST NOT match as a (sub)expression within the base match
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var inputTxt = actEd.text();
if (checkNotMatchingSubRE == false){ // a normal list search without excluding
var rePatText = InputText("Zadejte re výraz (VÝPIS NÁLEZŮ) - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", rePatSearchTextMem, stornoValue);
if (rePatText == stornoValue){
}else{
rePatSearchTextMem = rePatText;
var reInst = eval('new RegExp('+rePatText+');');
var matchObj;
var arrMatches = new Array();
while ((matchObj = reInst.exec(inputTxt)) != null){
arrMatches[arrMatches.length] = matchObj[0];
}
var infoResultTxt = "Výsledky hledání re výrazu: \""+rePatText+";\n počet nálezů: "+arrMatches.length+".\n\n";
if(arrMatches.length < 1){
echo("Nenalezeno!\n\n"+infoResultTxt);
}else{
var outputText = infoResultTxt+arrMatches.join("\n")+"\n";
outputEditorList = newEditor();
outputEditorList.newFile("SearchResultList.txt");
outputEditorList.text(outputText);
checkAndSupplResult(outputEditorList.text(), outputText);
}
}
}else{//list search with an excluding RE
var rePatTextBaseRE = InputText("Zadejte ZÁKLADNÍ RE výraz pro VÝPIS S VÝJIMKOU: nejprve je třeba zadat základní RE pro hledání - výjimka vyřazující nálezy bude zadána v dalším kroku - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", rePatTextBaseREMem, stornoValue);
var rePatTextNeg = InputText("Zadejte VYŘAZUJÍCÍ RE výraz pro VÝPIS S VÝJIMKOU - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", rePatSearchTextMem, stornoValue);
if ((rePatTextBaseRE == stornoValue)||(rePatTextNeg == stornoValue)){
}else{
rePatTextBaseREMem = rePatTextBaseRE;
rePatSearchTextMem = rePatTextNeg;
var reBase = eval('new RegExp('+rePatTextBaseRE+');');
var reNeg = eval('new RegExp('+rePatTextNeg+');');
var matchObj;
var arrMatches = new Array();
while ((matchObj = reBase.exec(inputTxt)) != null){
if(matchObj[0].search(reNeg)<0){// adds the baseRE match to the results if negRE doesn't match
arrMatches[arrMatches.length] = matchObj[0];
}
}
var infoResultTxt = "Výsledky hledání reg. výrazu: \""+rePatTextBaseRE+"\" \n(po vyloučení nálezů obsahujících: \""+rePatTextNeg+"\"); počet vyhovujících nálezů: "+arrMatches.length+".\n\n";
if(arrMatches.length < 1){
echo("Nenalezeno!\n\n"+infoResultTxt);
}else{
var outputText = infoResultTxt+arrMatches.join("\n")+"\n";
outputEditorList = newEditor();
outputEditorList.newFile("SearchResultList.txt");
outputEditorList.text(outputText);
checkAndSupplResult(outputEditorList.text(), outputText);
}
}
}
}
function reSearchDlg(){ reSearch(true, "forward"); }
function reSearchNextNoDlg(){ reSearch(false, "forward"); }
function reSearchPreviousNoDlg(){ reSearch(false, "back"); }
var editorTextCache = "";
var arrEOLendInd = new Array(); // reSearch, reCount, reWordBoundary
arrEOLendInd[0] = 0; // first index pos - test!!
var rePatSearchTextMem = '/ /g';
function reSearch(dlgRE, direction){
// highlights the first match from the cursor position; dlgRE - display promt for the search pattern?, direction (forward or backward search)
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
if (dlgRE == true){
var rePatText = InputText("Zadejte re výraz (HLEDÁNÍ) - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", rePatSearchTextMem, stornoValue);
}else{
var rePatText = rePatSearchTextMem;
}
if (rePatText == stornoValue){
}else{
rePatSearchTextMem = rePatText;
var inputTxt = actEd.text();
if (editorTextCache == inputTxt){//checks if the editor content has changed, if so, it updates the EOL data
}else{
editorTextCache = inputTxt;
arrEOLendInd = new Array();
arrEOLendInd[0] = 0; // first index pos
var EOLregExp = new RegExp("\r\n|\r|\n", "g");
while ((matchEOLobj = EOLregExp.exec(inputTxt)) != null){
arrEOLendInd[arrEOLendInd.length] = matchEOLobj.lastIndex; //index;
}
arrEOLendInd[arrEOLendInd.length] = inputTxt.length+1; // dummy last index pos + 1 to pass < for the last character
}
var origCaretX = actEd.caretX();
var origCaretY = actEd.caretY();
var stringPosCaret = arrEOLendInd[origCaretY-1]+origCaretX-1; // string index position of the caret
var reInst = eval('new RegExp('+rePatText+');');
if (direction == "forward"){
var inputTxtAktCurs = inputTxt.substring(stringPosCaret, inputTxt.length);
}else if (direction == "back"){
var inputTxtAktCurs = inputTxt.substring(0, stringPosCaret);
}
var matchObj;
var matchStartIndInputTxt = -1;
var matchEndIndInputTxt = -1;
if (direction == "forward"){
if ((matchObj = reInst.exec(inputTxtAktCurs)) != null){// first match only
matchStartIndInputTxt = matchObj.index+stringPosCaret;
matchEndIndInputTxt = matchObj.lastIndex+stringPosCaret;
}
}else if (direction == "back"){
while ((matchObj = reInst.exec(inputTxtAktCurs)) != null){//replaces the values untill they hold the last match
matchStartIndInputTxt = matchObj.index;
matchEndIndInputTxt = matchObj.lastIndex;
}
}
if (matchStartIndInputTxt > -1){
var matchStartX = -1;
var matchStartY = -1;
var matchEndX = -1;
var matchEndY = -1;
for (var i=0; i< arrEOLendInd.length-1; i++){
if ((arrEOLendInd[i] <= matchStartIndInputTxt)&&(matchStartIndInputTxt < arrEOLendInd[i+1])){
matchStartY = i+1;
matchStartX = matchStartIndInputTxt - arrEOLendInd[i] +1;
}
if ((arrEOLendInd[i] <= matchEndIndInputTxt)&&(matchEndIndInputTxt < arrEOLendInd[i+1])){
matchEndY = i+1;
matchEndX = matchEndIndInputTxt - arrEOLendInd[i] +1;
break;
}
}
if (direction == "forward"){
actEd.setCaretPos(matchEndX, matchEndY);
} else if (direction == "back"){
actEd.setCaretPos(matchStartX, matchStartY);
}
actEd.setBlockBegin(matchStartX, matchStartY);
actEd.setBlockEnd(matchEndX, matchEndY);
}else{
var totalMatchesCount = 0; // total matches - block not necessary - for the prompt info only
var matchObjCompl;
while ((matchObjCompl = reInst.exec(inputTxt)) != null){
totalMatchesCount++;
}
var continueSearch = InputText("Další výskyt nenalezen! \nCelkový počet nálezů pro \n "+rePatText+" \nv celém souboru: "+totalMatchesCount+"\n\nChcete pokračovat v hledání?", "ano", stornoValue);
// var continueSearch = InputText("Další výskyt \n "+rePatText+" \nnenalezen! \n\nPokračovat v hledání?", "yes", stornoValue);
if (continueSearch == "ano"){
if(direction == "forward"){
actEd.setCaretPos(1, 1);
} else if (direction == "back"){
actEd.setCaretPos(0, actEd.linesCount());
actEd.setCaretPos(actEd.lineText().length+1, actEd.linesCount());//set caret at the EOF ...
}
reSearch(false, direction);
}
}
}
}
function nextWordBoundaryDlg(){ reWordBoundary(true, "forward", false); }
function nextWordBoundary(){ reWordBoundary(false, "forward", false); }
function previousWordBoundary(){ reWordBoundary(false, "back", false); }
function nextWordBoundarySel(){ reWordBoundary(false, "forward", true); }
function previousWordBoundarySel(){ reWordBoundary(false, "back", true); }
// editorTextCache, arrEOLendInd; also reSearch, reCount
var matchWordStartEndIndexArr = new Array();
matchWordStartEndIndexArr[0] = -1; //dummy first index
var reWordBoundaryPatMem = "/[^ ,.;:?!\'\"+*\\/<>()\\[\\]\\r\\n-]+/g";
function reWordBoundary(dlgRE, direction, select){
// sets the caret to the next word boundary - according to the given reWord pattern (begining and end of each match) "word", "non-word" or "word + non-word" etc. can be set to determine the boundaries; dlgRE - display promt for the pattern?, direction (forward or backward search), select - select text to the next word boundary
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
if (dlgRE == true){
var reWordBoundaryPat = InputText("Zadejte re výraz (SLOVO / HRANICE SLOV) - je třeba zachovat formát:\n /abc/g \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", rePatSearchTextMem, stornoValue);
}else{
var reWordBoundaryPat = reWordBoundaryPatMem;
}
if (reWordBoundaryPat == stornoValue){
}else{
reWordBoundaryPatMem = reWordBoundaryPat;
var inputTxt = actEd.text();
if (editorTextCache == inputTxt){//checks if the editor content has changed, if so, it updates the EOL data
}else{
editorTextCache = inputTxt;
arrEOLendInd = new Array();
arrEOLendInd[0] = 0; // first index pos
var EOLregExp = new RegExp("\r\n|\r|\n", "g");
var matchEOLobj;
while ((matchEOLobj = EOLregExp.exec(inputTxt)) != null){
arrEOLendInd[arrEOLendInd.length] = matchEOLobj.lastIndex; //index;
}
arrEOLendInd[arrEOLendInd.length] = inputTxt.length+1; // dummy last index pos + 1 to pass < for the last character
if (dlgRE == false){// otherwise (dialog shown) the data will be updated in all cases (also with unchanged text) - below
var reInst = eval('new RegExp('+reWordBoundaryPat+');');
matchWordStartEndIndexArr = new Array();
matchWordStartEndIndexArr[0] = -1; //dummy first index
var matchObj;
while ((matchObj = reInst.exec(inputTxt)) != null){
if(matchWordStartEndIndexArr[matchWordStartEndIndexArr.length-1] != matchObj.index){
matchWordStartEndIndexArr[matchWordStartEndIndexArr.length] = matchObj.index;
}
if(matchWordStartEndIndexArr[matchWordStartEndIndexArr.length-1] != matchObj.lastIndex){
matchWordStartEndIndexArr[matchWordStartEndIndexArr.length] = matchObj.lastIndex;
}
}
matchWordStartEndIndexArr[matchWordStartEndIndexArr.length] = inputTxt.length+1; // dummy last index pos + 1 to pass < for the last character
}
}
if (dlgRE == true){
var reInst = eval('new RegExp('+reWordBoundaryPat+');');
matchWordStartEndIndexArr = new Array();
matchWordStartEndIndexArr[0] = 0;
var matchObj;
while ((matchObj = reInst.exec(inputTxt)) != null){
if(matchWordStartEndIndexArr[matchWordStartEndIndexArr.length-1] != matchObj.index){//duplicate indices omitted
matchWordStartEndIndexArr[matchWordStartEndIndexArr.length] = matchObj.index;
}
if(matchWordStartEndIndexArr[matchWordStartEndIndexArr.length-1] != matchObj.lastIndex){
matchWordStartEndIndexArr[matchWordStartEndIndexArr.length] = matchObj.lastIndex;
}
}
matchWordStartEndIndexArr[matchWordStartEndIndexArr.length] = inputTxt.length+1; // dummy last index pos + 1 to pass < for the last character
}
var origCaretX = actEd.caretX();
var origCaretY = actEd.caretY();
var stringPosCaret = arrEOLendInd[origCaretY-1]+origCaretX-1; // string index position of the caret
var anotherWordBoundIndInputTxt = -1;
if (direction == "forward"){
for (var i=0; i< matchWordStartEndIndexArr.length-1; i++){
if ((matchWordStartEndIndexArr[i] <= stringPosCaret)&&(stringPosCaret < matchWordStartEndIndexArr[i+1])){
anotherWordBoundIndInputTxt = matchWordStartEndIndexArr[i+1];
break;
}
}
}else if (direction == "back"){
for (var i=0; i< matchWordStartEndIndexArr.length-1; i++){
if ((matchWordStartEndIndexArr[i] < stringPosCaret)&&(stringPosCaret <= matchWordStartEndIndexArr[i+1])){
anotherWordBoundIndInputTxt = matchWordStartEndIndexArr[i];
break;
}
}
}
if(anotherWordBoundIndInputTxt > inputTxt.length){// for the dummy last index
anotherWordBoundIndInputTxt = inputTxt.length
}
if (anotherWordBoundIndInputTxt > -1){
var WBIndX = -1;
var WBIndY = -1;
for (var i=0; i< arrEOLendInd.length-1; i++){
if ((arrEOLendInd[i] <= anotherWordBoundIndInputTxt)&&(anotherWordBoundIndInputTxt < arrEOLendInd[i+1])){
WBIndY = i+1;
WBIndX = anotherWordBoundIndInputTxt - arrEOLendInd[i] +1;
break;
}
}
if (select == false){ // non-selection mode
actEd.setCaretPos(WBIndX, WBIndY);
} else { // selection mode
if(actEd.caretX() == actEd.blockEndX() && actEd.caretY() == actEd.blockEndY()){// also no selection
var fixedXSel = actEd.blockBeginX();//fixed selection coords - those not identical with the caret
var fixedYSel = actEd.blockBeginY();
} else if(actEd.caretX() == actEd.blockBeginX()&&actEd.caretY() == actEd.blockBeginY()){
var fixedXSel = actEd.blockEndX();
var fixedYSel = actEd.blockEndY();
}
actEd.setCaretPos(WBIndX, WBIndY);
actEd.setBlockBegin(compareTextCoords(fixedXSel,fixedYSel,WBIndX,WBIndY)[0], compareTextCoords(fixedXSel,fixedYSel,WBIndX,WBIndY)[1]);//4 function calls?? pass coord and edObj and select directly??
actEd.setBlockEnd(compareTextCoords(fixedXSel,fixedYSel,WBIndX,WBIndY)[2], compareTextCoords(fixedXSel,fixedYSel,WBIndX,WBIndY)[3]);
}
}
}
}
function compareTextCoords(x1,y1,x2,y2){
// compares text coordinates, returns an array with ordered coords - the smaller xy; the greater xy
if (y1y2){return new Array(x2,y2,x1,y1);}
else if (y1==y2){
if (x1x2){return new Array(x2,y2,x1,y1);}
else {return new Array(x1,y1,x2,y2);}// equal coords
}
}
function fromCharCode(){ preSetForEval("String.fromCharCode(104,105,33)"); }
function fromCharCodeSeq(){ preSetForEval('function nrSeq(){var nr = ""; for(var i = 32; i < 128; i++){nr += String.fromCharCode(i);} return nr;} nrSeq();'); }
function numberSeq(){ preSetForEval('function nrSeq(){var nr = ""; for(var i = 0; i <= 100; i++){nr += i+",";} return nr;} nrSeq();'); }
function anotherEvalStr(){ preSetForEval(""); }
var inputCodeStr = "1+1";
function preSetForEval(evalString){
// presets strings to evaluate by evalInput()
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
actEd.setBlockBegin(actEd.caretX(), actEd.caretY());//removes the eventual selection block leaving the text
actEd.setBlockEnd(actEd.caretX(), actEd.caretY());
inputCodeStr = evalString;
evalInput();
}
function evalInput(){
// evaluates the input via eval(); text selection is the default pattern; if not present, the last pattern used
// the result is inserted at the caret position and highlighted/selected (the previously selected text remains intact, the selection block changes however)
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var selTxt = actEd.selText();
if (selTxt.length >0){
var codeInputDef = selTxt;
}else{
var codeInputDef = inputCodeStr;
}
inputCodeStr = InputText("Vložte kód javascriptu k VYHODNOCENÍ (výsledek se vloží na pozici kurzoru a zvýrazní výběrem, případný dříve vybraný text se načte do vstupu kódu; v textu pak zůstane zachován).:", codeInputDef, stornoValue);
if (inputCodeStr == stornoValue){
}else{
var origCaretX = actEd.caretX();
var origCaretY = actEd.caretY();
actEd.setBlockBegin(origCaretX, origCaretY);//removes the eventual selection block leaving the text
actEd.setBlockEnd(origCaretX, origCaretY);
var outputObj = eval(inputCodeStr);
if(outputObj){
var outputText = outputObj.toString();
}else{
var outputText = "";
}
actEd.selText(outputText);
var carXafterIns = actEd.caretX();
var carYafterIns = actEd.caretY();
actEd.setBlockBegin(origCaretX, origCaretY);
actEd.setBlockEnd(carXafterIns, carYafterIns);
checkAndSupplResult(actEd.selText(), outputText);
}
}
function convSelTOunicodeLiteral(){ convertSelection("unicodeLiteral"); }
function convSelTOhexEntity(){ convertSelection("hexEntity"); }
function convSelTOdecEntity(){ convertSelection("decEntity"); }
function convSelTOplainText(){ convertSelection("plainText"); }
// helper functions storing the string parameters for convertSelection()
function convertSelection(convertTo){
// converts the selected text to various formats
// unicodeLiteral - usable eg. as an input for RE replacements
// html character entities dec/hex
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var selTxt = actEd.selText();
var outputStr = "";
var unicCharCode4Txt;
var infoText = "Text výběru po konverzi na ";
if(convertTo == "unicodeLiteral"){
infoText += " literály unicode. ";
for (var i=0; i 0){
var inputTxt = actEd.selText();
}else{
var inputTxt = actEd.text();
}
// function d2h(d) {return d.toString(16);}
// function h2d(h) {return parseInt(h,16);}
var character, unicCharCode, decValue, unicCharCode4Txt;
var tableContent = '\ncharacter | unicode literal | unicode entity (dec.) | unicode entity (hex.) |
';
for (var i=0; i'+unicCharCode+'; | \\u'+unicCharCode4Txt+' | &#'+unicCharCode+'; | &#x'+unicCharCode.toString(16)+'; | ';
}
tableContent += '\n
\n';
outputEditor = newEditor();
outputEditor.newFile("CharacterCodesUnicode.html");
outputEditor.text(htmlHeader+tableContent+htmlFooter);
}
var indexSupplFile=1;
function checkAndSupplResult(reCheckedText, textToDisplay){
// helper function - shows texts with non ANSI characters as HTML page using unicode character codes
var unifiedEOLsTxt = textToDisplay.replace(/\r\n|\r|\n/g,"\n");
if (reCheckedText.replace(/\r\n|\r|\n/g,"\n") != unifiedEOLsTxt){//checks for equality after unifying line endings; displays suppl.
echo("Pozor! \n\nTento skript podporuje pouze ANSI znaky Windows.\n Zdá se, že ve zpracovávaném textu se vyskytovaly znaky mimo ANSI, které byly touto operací ztraceny.\nDoporučuje se tento krok vrátit zpět (Ctrl+Z); k dispozici je náhradní výstup v HTML souboru SupplementaryResult.html. \n(Tento kód je třeba zobrazit jako HTML stánku pomocí náhledu HTML v PSPadu (F10) nebo v běžném prohlížeči a vykopírovat text zpátky do okna editoru - nahrazení výběru nebo celého textu.)");
var htmlHeader = 'Supplementary text file - Poor Man \'s replace...';
var htmlFooter = '';
var supplTxt = "";
unifiedEOLsTxt = unifiedEOLsTxt.replace(/&/g,"&");// replace amp before other entities!!
unifiedEOLsTxt = unifiedEOLsTxt.replace(/");
for (var i=0; i 0){
if(actEd.blockBeginX()){
var selStartX = actEd.blockBeginX();
var selStartY = actEd.blockBeginY();
}
var inputTxt = actEd.selText();
var outputText = eval('inputTxt.replace('+rePatTextRepl+');');
actEd.selText(outputText);
if(actEd.blockBeginX()){
var carXafterRepl = actEd.caretX();
var carYafterRepl = actEd.caretY();
actEd.setBlockBegin(selStartX, selStartY);
actEd.setBlockEnd(carXafterRepl, carYafterRepl);
}
checkAndSupplResult(actEd.selText(), outputText);
}else{
var inputTxt = actEd.text();
var outputText = eval('inputTxt.replace('+rePatTextRepl+');');
actEd.text(outputText);
checkAndSupplResult(actEd.text(), outputText);
}
}
}
function reReplaceAllEditors(){
// performs simple re replacements in all open editors (whole texts)
var rePatTextRepl = InputText("Vložit RE výraz pro NAHRAZENÍ VŠECH OTEVŘENÝCH TEXTŮ- je třeba zachovat formát:\n /abc/g,\"xyz\" \nFlagy - za hledaný výraz uvedený mezi / /g \ng - globální (nahradí všechny nálezy - default)\ni - ignorovat velikost\ngi - globální s ignorováním velikosti", rePatTextReplMem, stornoValue); // ; m - multiline; doesn' work ??
if (rePatTextRepl == stornoValue){
}else{
rePatTextReplMem = rePatTextRepl;
if (editorsCount()>0){
var numberOfEditors = editorsCount();//value at the time of calling - in order not to replace in newly created SupplementaryResult-files
for(var i=0; i 0){
if(actEd.blockBeginX()){
var selStartX = actEd.blockBeginX();
var selStartY = actEd.blockBeginY();
}
var inputTxt = actEd.selText();
var outputText = inputTxt;
for (var i=0; i < arrReplPatterns.length; i++){
outputText = eval('outputText.replace('+arrReplPatterns[i]+');');
}
actEd.selText(outputText);
if(actEd.blockBeginX()){
var carXafterRepl = actEd.caretX();
var carYafterRepl = actEd.caretY();
actEd.setBlockBegin(selStartX, selStartY);
actEd.setBlockEnd(carXafterRepl, carYafterRepl);
}
checkAndSupplResult(actEd.selText(), outputText);
}else{
var inputTxt = actEd.text();
var outputText = inputTxt;
for (var i=0; i < arrReplPatterns.length; i++){
outputText = eval('outputText.replace('+arrReplPatterns[i]+');');
}
actEd.text(outputText);
checkAndSupplResult(actEd.text(), outputText);
}
}
}
var separJoinMem = " ";
function joinLines(){
// joins lines (in selection or in the whole document) using the given separator - default: space;
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var separJoin = InputText("Vložit oddělovač pro SPOJENÍ ŘÁDEK:", separJoinMem, stornoValue);
if (separJoin == stornoValue){
}else{
separJoinMem = separJoin;
var joinRE = new RegExp("\r\n|\r|\n","g");
if (actEd.selText().length > 0){
if(actEd.blockBeginX()){
var selStartX = actEd.blockBeginX();
var selStartY = actEd.blockBeginY();
}
var inputTxt = actEd.selText();
var outputText = inputTxt.replace(joinRE, separJoin);
actEd.selText(outputText);
if(actEd.blockBeginX()){
var carXafterRepl = actEd.caretX();
var carYafterRepl = actEd.caretY();
actEd.setBlockBegin(selStartX, selStartY);
actEd.setBlockEnd(carXafterRepl, carYafterRepl);
}
checkAndSupplResult(actEd.selText(), outputText);
}else{
var inputTxt = actEd.text();
var outputText = inputTxt.replace(joinRE, separJoin);
actEd.text(outputText);
checkAndSupplResult(actEd.text(), outputText);
}
}
}
var separSplitMem = " ";
function splitLines(){
// splits lines (in selection or in the whole document) using the given separator - Regular Expression -
// default: space; setting the separator to an empty string, splits each character to a separate line;
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var separSplit = InputText("Vložit oddělovač (re) pro ROZDĚLENÍ ŘÁDEK:", separSplitMem, stornoValue);
if (separSplit == stornoValue){
}else{
separSplitMem = separSplit;
var SplitRE = new RegExp(separSplit,"g");
if (actEd.selText().length > 0){
if(actEd.blockBeginX()){
var selStartX = actEd.blockBeginX();
var selStartY = actEd.blockBeginY();
}
var inputTxt = actEd.selText();
var outputText = inputTxt.replace(SplitRE, "\n");
actEd.selText(outputText);
if(actEd.blockBeginX()){
var carXafterRepl = actEd.caretX();
var carYafterRepl = actEd.caretY();
actEd.setBlockBegin(selStartX, selStartY);
actEd.setBlockEnd(carXafterRepl, carYafterRepl);
}
checkAndSupplResult(actEd.selText(), outputText);
}else{
var inputTxt = actEd.text();
var outputText = inputTxt.replace(SplitRE, "\n");
actEd.text(outputText);
checkAndSupplResult(actEd.text(), outputText);
}
}
}
var fileNr=1;
function newTextFile(){
myNewEditor = newEditor();
myNewEditor.newFile("NewFile_"+fileNr+".txt");
fileNr += 1;
}
var savedCaretXPos = 0
function storeCaretX(){
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
savedCaretXPos = actEd.caretX()
}
function lineUpCaretX(){ moveCaretVert(-1);}
function lineDownCaretX(){ moveCaretVert(1);}
function moveCaretVert(vertDistance){
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
actEd.setCaretPos(savedCaretXPos, actEd.caretY()+vertDistance);
}
function copyWithLineNrs(){
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var selectedText = actEd.selText();
var firstSelLine = actEd.blockBeginY();
var lastSelLine = actEd.blockEndY();
var lineNr = firstSelLine - 1;
var numberedSelText = selectedText.replace(/([^\n\r]*(?:\r\n)?)/g, function (match, lnTxt){lineNr += 1; if (lineNr <= lastSelLine){ return (lineNr + ": " + lnTxt);} } );
//echo(selectedText + "\n------------\n" + numberedSelText);
setClipboardText(numberedSelText);
}
function duplTab(){
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
var actEdTxt = actEd.text();
var actFileName = actEd.fileName();
var duplEd = newEditor();
duplEd.newFile("kopie_"+actFileName);
duplEd.text(actEdTxt);
if (actEdTxt != duplEd.text()){
echo("Obsah duplicitního tabu NENÍ přesně TOTOŽNÝ se zdrojovým oknem!\nMohly být např. ztraceny unicode znaky mimo nastavenou národní znakovou sadu.");
}
}
function Init(){
addMenuItem("spojit řádky", "Laciné RE", "joinLines","Ctrl+Alt+J");// labels and shortcuts can be modified
addMenuItem("rozdělit řádky", "Laciné RE", "splitLines","Ctrl+Alt+S");
addMenuItem("* nahradit RE *", "Laciné RE", "reReplace","Ctrl+Alt+H");
addMenuItem("nahr. vše okna", "Laciné RE", "reReplaceAllEditors","");
addMenuItem("nahradit sekv.", "Laciné RE", "reReplaceSeqDefault","");
addMenuItem("nahr. sekv. separ.", "Laciné RE", "reReplaceSeqSetSepar","");
addMenuItem("-", "Laciné RE", "","");//menu separator
addMenuItem("* hledání RE *", "Laciné RE", "reSearchDlg","Ctrl+Alt+I");
addMenuItem("najít předchozí", "Laciné RE", "reSearchPreviousNoDlg","Ctrl+Alt+O");
addMenuItem("najít další", "Laciné RE", "reSearchNextNoDlg","Ctrl+Alt+P");
addMenuItem("-", "Laciné RE", "","");
addMenuItem("nast. re hran. slov", "Laciné RE", "nextWordBoundaryDlg","");
addMenuItem("předchozí slovo", "Laciné RE", "nextWordBoundary","Ctrl+Alt+K");
addMenuItem("další slovo", "Laciné RE", "previousWordBoundary","Ctrl+Alt+M");
addMenuItem("předchozí slovo výb.", "Laciné RE", "previousWordBoundarySel","Ctrl+Alt+Shift+M");
addMenuItem("další slovo výb.", "Laciné RE", "nextWordBoundarySel","Ctrl+Alt+Shift+K");
addMenuItem("-", "Laciné RE", "","");
addMenuItem("počet nálezů", "Laciné RE", "reCount","");
addMenuItem("výpis nálezů", "Laciné RE", "reListSearch","");
addMenuItem("výpis nevyhovujících", "Laciné RE", "reListSearchExcl","");
addMenuItem("-", "Laciné RE", "","");
addMenuItem("kódy znaků (tab.)", "Laciné RE", "textToCharCodeTab","");
addMenuItem("text stat (abc. vzest.)", "Laciné RE", "textInfoAlphAscImpl","");
addMenuItem("text stat (abc. sest.)", "Laciné RE", "textInfoAlphDescImpl","");
addMenuItem("text stat (num. vzest.)", "Laciné RE", "textInfoNumAscImpl","");
addMenuItem("text stat (num. sest.)", "Laciné RE", "textInfoNumDescImpl","");
addMenuItem("t.st. malá (abc. vzest.)", "Laciné RE", "textInfoAlphAscImplLc","");
addMenuItem("t.st. malá (abc. sest.)", "Laciné RE", "textInfoAlphDescImplLc","");
addMenuItem("t.st. malá (num. vzest.)", "Laciné RE", "textInfoNumAscImplLc","");
addMenuItem("t.st. malá (num. sest.)", "Laciné RE", "textInfoNumDescImplLc","");
addMenuItem("stat - zadat RE", "Laciné RE", "textInfoAlphAscAsk","");
addMenuItem("-", "Laciné RE", "","");
addMenuItem("literály unic. (výb.)", "Laciné RE", "convSelTOunicodeLiteral","");
addMenuItem("hex entity (výb.)", "Laciné RE", "convSelTOhexEntity","");
addMenuItem("dec entity (výb.)", "Laciné RE", "convSelTOdecEntity","");
addMenuItem("text (výb.)", "Laciné RE", "convSelTOplainText","");
addMenuItem("-", "Laciné RE", "","");
addMenuItem("* eval *", "Laciné RE", "evalInput","");
addMenuItem("ev. znaky - dec unic.", "Laciné RE", "fromCharCode","");
addMenuItem("ev. zn. - dec unic. - řada", "Laciné RE", "fromCharCodeSeq","");
addMenuItem("ev. řada čísel", "Laciné RE", "numberSeq","");
addMenuItem("-", "Laciné RE", "","");
addMenuItem("nový soubor txt", "Laciné RE", "newTextFile","Ctrl+Shift+N");
addMenuItem("uložit kursor (x)", "Laciné RE", "storeCaretX","Ctrl+Shift+C");
addMenuItem("o řádek výše (x)", "Laciné RE", "lineUpCaretX","Alt+Up");
addMenuItem("o řádek níže (x)", "Laciné RE", "lineDownCaretX","Alt+Down");
addMenuItem("kopírovat + č.ř.", "Laciné RE", "copyWithLineNrs","Alt+Shift+C");
addMenuItem("duplikovat tab", "Laciné RE", "duplTab","Ctrl+Shift+D");
}