1 21 22 package org.armedbear.j; 23 24 import gnu.regexp.RE; 25 import gnu.regexp.REMatch; 26 import gnu.regexp.UncheckedRE; 27 import java.awt.AWTEvent ; 28 import java.awt.event.MouseEvent ; 29 30 public final class GotoFile implements Constants 31 { 32 public static void gotoFile() 33 { 34 final Editor editor = Editor.currentEditor(); 35 36 AWTEvent event = editor.getDispatcher().getLastEvent(); 39 if (event instanceof MouseEvent ) 40 editor.mouseMoveDotToPoint((MouseEvent )event); 41 42 String filename = gotoFileGetFileName(editor); 43 if (filename == null) 44 return; 45 int lineNumber = -1; 46 47 int index = filename.lastIndexOf(" line "); 49 if (index >= 0) { 50 try { 52 lineNumber = Integer.parseInt(filename.substring(index+6)) - 1; 53 filename = filename.substring(0, index); 55 } 56 catch (NumberFormatException e) { 57 } 59 } else { 60 index = filename.lastIndexOf(':'); 61 if (index >= 0) { 62 try { 64 lineNumber = 65 Integer.parseInt(filename.substring(index+1)) - 1; 66 filename = filename.substring(0, index); 68 } 69 catch (NumberFormatException e) { 70 } 72 } 73 } 74 75 boolean tryCurrentDirectory = true; 76 77 if (filename.length() >= 2 && 78 filename.charAt(0) == '<' && 79 filename.charAt(filename.length()-1) == '>') { 80 filename = filename.substring(1, filename.length()-1); 84 tryCurrentDirectory = false; 85 } 86 87 File file = null; 88 89 if (Utilities.isFilenameAbsolute(filename)) { 90 file = File.getInstance(editor.getCurrentDirectory(), filename); 91 } else { 92 if (tryCurrentDirectory) 94 file = File.getInstance(editor.getCurrentDirectory(), 96 filename); 97 98 if (file == null || (file.isLocal() && !file.exists())) 100 file = Utilities.findFile(editor, filename); 101 } 102 103 if (file != null) { 104 Buffer buf = editor.getBuffer(file); 105 if (buf != null) { 106 final Frame frame = editor.getFrame(); 107 editor.makeNext(buf); 108 editor.switchToBuffer(buf); 109 Editor ed = 111 frame.contains(editor) ? editor : frame.getCurrentEditor(); 112 if (ed.getBuffer() == buf) { 113 if (lineNumber >= 0) { 114 if (ed.getDot() != null) { 115 if (ed.getDotLineNumber() != lineNumber) { 116 ed.addUndo(SimpleEdit.MOVE); 117 ed.unmark(); 118 ed.gotoline(lineNumber); 119 ed.moveCaretToDotCol(); 120 } 121 } 122 } 123 ed.updateDisplay(); 124 } 125 } 126 } 127 } 128 129 private static String gotoFileGetFileName(Editor editor) 130 { 131 if (editor.getDot() == null) 132 return null; 133 final Line dotLine = editor.getDotLine(); 134 final int dotOffset = editor.getDotOffset(); 135 if (editor.getMark() != null && editor.getMarkLine() == dotLine) { 136 return new Region(editor).toString(); 138 } 139 final int modeId = editor.getModeId(); 140 if (modeId == HTML_MODE) { 141 RE re = new UncheckedRE("(href|src)=\"([^\"]+)\"", RE.REG_ICASE); 142 REMatch match = null; 143 final String text = dotLine.getText(); 144 int index = 0; 145 REMatch m; 146 while ((m = re.getMatch(text, index)) != null) { 147 match = m; 148 if (match.getEndIndex() > dotOffset) 149 break; index = match.getEndIndex(); 151 } 152 if (match != null) 153 return match.toString(2); 154 } else if (modeId == JAVA_MODE) { 155 String fileName = 156 getFileNameFromImport(editor.getBuffer(), dotLine.getText()); 157 if (fileName != null) 158 return fileName; 159 } else if ((modeId == C_MODE || modeId == CPP_MODE)) { 160 String fileName = getFileNameFromInclude(dotLine.getText()); 161 if (fileName != null) 162 return fileName; 163 } else if (editor.getBuffer().getType() == Buffer.TYPE_SHELL) { 164 String s = dotLine.getText().trim(); 165 REMatch match = Directory.getNativeMoveToFilenameRegExp().getMatch(s); 166 if (match != null) 167 return s.substring(match.getEndIndex()); 168 } 169 return editor.getFilenameAtDot(); 170 } 171 172 private static String getFileNameFromImport(Buffer buffer, String s) 173 { 174 if (s.indexOf('*') >= 0) 175 return null; 176 s = s.trim(); 177 if (!s.startsWith("import")) 178 return null; 179 s = s.substring(6); 180 if (s.length() == 0) 181 return null; 182 if (s.charAt(0) != ' ' && s.charAt(0) != '\t') 183 return null; 184 s = s.trim(); 185 FastStringBuffer sb = new FastStringBuffer(); 186 for (int i = 0; i < s.length(); i++) { 187 char c = s.charAt(i); 188 if (c == ' ' || c == '\t' || c == ';') 189 break; 190 sb.append(c); 191 } 192 File file = JavaSource.findSource(buffer, sb.toString(), true); 193 return file != null ? file.canonicalPath() : null; 194 } 195 196 private static final RE includeRE = 197 new UncheckedRE("[ \t]*#[ \t]*include[ \t]"); 198 199 private static final String getFileNameFromInclude(String s) 200 { 201 REMatch match = includeRE.getMatch(s); 202 if (match == null) 203 return null; 204 s = s.substring(match.getEndIndex()).trim(); 205 if (s.length() < 3) 207 return null; 208 char c = s.charAt(0); 209 if (c == '"') { 210 int index = s.indexOf('"', 1); 211 if (index >= 0) 212 return s.substring(1, index); 213 return null; 214 } 215 if (c == '<') { 216 int index = s.indexOf('>', 1); 217 if (index >= 0) 218 return s.substring(0, index+1); } 220 return null; 221 } 222 } 223 | Popular Tags |