// WikiWord V1.7 jEdit macro. peter@TurtleCoveTech.com, 2007-2009 // when called, it grabs the next [BracketedWord] and opens a file by that // name eg: BracketWord.txt // You can add an anchor to jump to a position within the file: // [document#anchor] which will open document and find [#anchor] // V1.1 2007 Open [text file] // V1.2 Added anchors [file#anchor] // V1.21 Fixed anchor jumping bug // V1.3 Added anchor jumping within a document [#justanchor] .. [#justanchor] // V1.4 Added [path:xyz] to specify a search directory if file is not in current directory // Added [rpath:xyz] to search recursively starting with a directory // Note: you can have multiple [path:] and [rpath:] options in a file // V1.5 Added [filepattern:] for default filenames, // ie: [finance] with [filepattern:notes-$.txt] will look for 'finance' and 'notes-finance.txt' // V1.6 Added support for [file: anchor] as an alternate to [file#anchor] // V1.7 2009-10-19 Added support for space or tab delimited WikiWords: ie: [this is ok] and ThisIsOk and This-is-ok // ToDo: add support for [collect:] // pseudocode: get all [collect:] instructions. remove all lines in current buffer matching [collect:]s // open all [path:][rpath:][collectFiles:] files and extract all matching lines // insert into current buffer import java.io.File; import java.util.regex.Pattern; import java.util.regex.Matcher; // define function to gather config from current file and [config:] files java.util.List paths=new ArrayList(); java.util.List rpaths=new ArrayList(); String filePattern; void getConfig() { // search current buffer for config options // if one of them is [config:xxx] parse the config file first String text = textArea.getText(); // [path:directory] cwd=buffer.getDirectory(); paths.add( cwd); Matcher pathMatcher=Pattern.compile("\\[path:(.*?)\\]").matcher(text); while (pathMatcher.find()) { // Note we may be adding relative paths in here path=pathMatcher.group(1); if (! new File(path).isAbsolute()) { path=new File(cwd,path).getCanonicalPath(); } paths.add(path); } // [rpath:directory] (recursive path) Matcher rpathMatcher=Pattern.compile("\\[rpath:(.*?)\\]").matcher(text); while (rpathMatcher.find()) { rpath=rpathMatcher.group(1); if (! new File(rpath).isAbsolute()) { rpath=new File(cwd,rpath).getCanonicalPath(); } rpaths.add(rpath); } // eg: [filepattern:file-$.txt] (if WikiWord is not found, tries file-.txt Matcher filePatternMatcher=Pattern.compile("\\[filepattern:(.*?)\\]").matcher(text); if (filePatternMatcher.find()) { filePattern=filePatternMatcher.group(1); } else { filePattern="$"; } } String findfile( String filename) { // find file in path or rpath Matcher cookedfilenameMatcher=Pattern.compile("\\$").matcher(filePattern); String cookedFilename= cookedfilenameMatcher.replaceAll( filename); for( path : paths) { //Macros.message(view,"findfile "+new File(path, filename)+" and "+new File(path,cookedFilename)); if (new File( path, filename).isFile()) { return( new File( path, filename).getCanonicalPath()); } else if (new File( path, cookedFilename).isFile()) { return( new File( path, cookedFilename).getCanonicalPath()); } else if (new File( path, filename+".txt").isFile()) { return( new File( path, filename+".txt").getCanonicalPath()); } } for( rpath : rpaths) { foundpath=findFileInPath( filename, rpath); if (foundpath!=null) { return( foundpath); } } return null; } String findFileInPath( String filename, String path) { // find the file recursively in a subdirectory Matcher cookedfilenameMatcher=Pattern.compile("\\$").matcher(filePattern); String cookedFilename= cookedfilenameMatcher.replaceAll( filename); // Macros.message(view,"rtesting "+new File(path, filename)+" and "+new File(path,cookedFilename)); if (new File( path, filename).isFile()) { return new File( path, filename).getCanonicalPath(); } else if (new File( path, cookedFilename).isFile()) { return new File( path, cookedFilename).getCanonicalPath(); } else { for( ifile : new File(path).listFiles()) { if (ifile.isDirectory()) { found=findFileInPath( filename, ifile.getPath()); if (found!=null) { return found; } } } } return null; } // get text of whole current line: linetext=textArea.getLineText(textArea.getLineOfOffset(textArea.getCaretPosition())); // get current position within line: linestart=textArea.getLineStartOffset(textArea.getLineOfOffset(textArea.getCaretPosition())); linepos=textArea.getCaretPosition() - linestart; // Find the requested file name: can be delimeted by [words with space] or WikiStyle delimeted by whitespace closepos=linetext.indexOf( ']', linepos); // First try brackets if (closepos != -1) { // back up to the matching opening bracket openpos=linetext.lastIndexOf( '[', linepos); } else { // no brackets, try space or tab closespace=linetext.indexOf( ' ', linepos); openspace=linetext.lastIndexOf( ' ', linepos); closetab=linetext.indexOf("\t", linepos); opentab=linetext.lastIndexOf("\t", linepos); if (closetab==-1 || closespace < closetab) { closepos=closespace; } else { closepos=closetab; } if (closepos==-1) { closepos=linetext.length-1; } if (opentab==-1 || openspace > opentab) { openpos=openspace; } else { openpos=opentab; } // openpos == -1 is ok, will be start of line } //Macros.message(view, "debug "+ // "cursor="+linepos+ // ", openspace="+openspace+ // ", opentab="+opentab+ // ", openpos="+openpos+ // ", closespace="+closespace+ // ", closetab="+closetab+ // ", closepos="+closepos+ // ", name="+linetext.substring(openpos+1, closepos)); if (closepos == -1 || openpos>=closepos) { textArea.getToolkit().beep(); } else { // grab the BracketWord WikiWord=linetext.substring(openpos+1, closepos); anchorpos=WikiWord.indexOf( '#'); if (anchorpos == -1) { anchorpos=WikiWord.indexOf( ':'); } if (anchorpos != -1) { anchor="[" + WikiWord.substring( anchorpos) + "]"; freeanchor=WikiWord.substring( anchorpos+1); WikiWord=WikiWord.substring( 0, anchorpos); } if (WikiWord.length() != 0) { getConfig(); targetpath=findfile(WikiWord); // Macros.message(view,"findfile= "+targetpath); if (targetpath != null) { //if (new File( targetpath).isDirectory()) { // Runtime.getRuntime().exec("explorer.exe "+targetpath); // open folder //} else { newbuffer=jEdit.openFile( view, targetpath); // open found file //} } else { newbuffer=jEdit.openFile( view, WikiWord); // open new file with given name } } else { // if not found, open a new blank document newbuffer=buffer; } if (anchorpos != -1) { // If there is an anchor text, find it and put the cursor on it VFSManager.waitForRequests(); // wait for file load String text = textArea.getText(); Matcher anchorMatcher=Pattern.compile("\\Q"+anchor+"\\E").matcher(text); if (anchorMatcher.find()) { textArea.setCaretPosition(anchorMatcher.start()+1); } else { Matcher anchorMatcher=Pattern.compile("\\Q"+freeanchor+"\\E").matcher(text); if (anchorMatcher.find()) { textArea.setCaretPosition(anchorMatcher.start()+1); } } if (WikiWord.length() == 0 && (textArea.getCaretPosition() == linestart+openpos+1)) { // when jumping within a document, jump over the link itself to the anchor if (anchorMatcher.find()) { textArea.setCaretPosition(anchorMatcher.start()+1); } } } }