1 22 23 package org.gjt.sp.jedit; 24 25 import java.io.*; 27 import java.util.*; 28 import org.gjt.sp.jedit.gui.AddAbbrevDialog; 29 import org.gjt.sp.jedit.textarea.*; 30 import org.gjt.sp.util.Log; 31 33 38 public class Abbrevs 39 { 40 public static final String ENCODING = "UTF8"; 41 42 47 public static boolean getExpandOnInput() 48 { 49 return expandOnInput; 50 } 52 59 public static void setExpandOnInput(boolean expandOnInput) 60 { 61 Abbrevs.expandOnInput = expandOnInput; 62 } 64 73 public static boolean expandAbbrev(View view, boolean add) 74 { 75 Buffer buffer = view.getBuffer(); 77 JEditTextArea textArea = view.getTextArea(); 78 if(!buffer.isEditable()) 79 { 80 view.getToolkit().beep(); 81 return false; 82 } 83 84 int line = textArea.getCaretLine(); 85 int lineStart = buffer.getLineStartOffset(line); 86 int caret = textArea.getCaretPosition(); 87 88 String lineText = buffer.getLineText(line); 89 if(lineText.length() == 0) 90 { 91 if(add) 92 view.getToolkit().beep(); 93 return false; 94 } 95 96 int pos = caret - lineStart; 97 if(pos == 0) 98 { 99 if(add) 100 view.getToolkit().beep(); 101 return false; 102 } 104 m_pp.removeAllElements(); 106 107 int wordStart; 108 String abbrev; 109 110 if(lineText.charAt(pos-1) == '#') 112 { 113 wordStart = lineText.indexOf('#'); 114 wordStart = TextUtilities.findWordStart(lineText,wordStart, 115 buffer.getStringProperty("noWordSep") + '#'); 116 117 abbrev = lineText.substring(wordStart,pos - 1); 118 119 122 int lastIndex = 0; 123 for(int i = 0; i < abbrev.length(); i++) 124 { 125 if(abbrev.charAt(i) == '#') 126 { 127 m_pp.addElement(abbrev.substring(lastIndex,i)); 128 lastIndex = i + 1; 129 } 130 } 131 132 m_pp.addElement(abbrev.substring(lastIndex)); 133 134 abbrev = (String )m_pp.elementAt(0); 136 m_pp.removeElementAt(0); 137 } else 140 { 141 wordStart = TextUtilities.findWordStart(lineText,pos - 1, 142 buffer.getStringProperty("noWordSep")); 143 144 abbrev = lineText.substring(wordStart,pos); 145 } 147 Expansion expand = expandAbbrev(buffer.getMode().getName(), 148 abbrev,(buffer.getBooleanProperty("noTabs") ? 149 buffer.getTabSize() : 0),m_pp); 150 151 if(expand == null) 153 { 154 if(add) 155 new AddAbbrevDialog(view,abbrev); 156 157 return false; 158 } else 161 { 162 buffer.remove(lineStart + wordStart, 163 pos - wordStart); 164 165 int whitespace = buffer.insertIndented( 166 lineStart + wordStart, 167 expand.text); 168 169 int newlines = countNewlines(expand.text, 170 expand.caretPosition); 171 172 if(expand.caretPosition != -1) 173 { 174 textArea.setCaretPosition(lineStart + wordStart 175 + expand.caretPosition 176 + newlines * whitespace); 177 } 178 if(expand.posParamCount != m_pp.size()) 179 { 180 view.getStatus().setMessageAndClear( 181 jEdit.getProperty( 182 "view.status.incomplete-abbrev", 183 new Integer [] { new Integer (m_pp.size()), 184 new Integer (expand.posParamCount) })); 185 } 186 187 return true; 188 } } 191 196 public static Hashtable getGlobalAbbrevs() 197 { 198 if(!loaded) 199 load(); 200 201 return globalAbbrevs; 202 } 204 210 public static void setGlobalAbbrevs(Hashtable globalAbbrevs) 211 { 212 abbrevsChanged = true; 213 Abbrevs.globalAbbrevs = globalAbbrevs; 214 } 216 221 public static Hashtable getModeAbbrevs() 222 { 223 if(!loaded) 224 load(); 225 226 return modes; 227 } 229 235 public static void setModeAbbrevs(Hashtable modes) 236 { 237 abbrevsChanged = true; 238 Abbrevs.modes = modes; 239 } 241 248 public static void addGlobalAbbrev(String abbrev, String expansion) 249 { 250 if(!loaded) 251 load(); 252 253 globalAbbrevs.put(abbrev,expansion); 254 abbrevsChanged = true; 255 } 257 265 public static void addModeAbbrev(String mode, String abbrev, String expansion) 266 { 267 if(!loaded) 268 load(); 269 270 Hashtable modeAbbrevs = (Hashtable)modes.get(mode); 271 if(modeAbbrevs == null) 272 { 273 modeAbbrevs = new Hashtable(); 274 modes.put(mode,modeAbbrevs); 275 } 276 modeAbbrevs.put(abbrev,expansion); 277 abbrevsChanged = true; 278 } 280 static void save() 282 { 283 jEdit.setBooleanProperty("view.expandOnInput",expandOnInput); 284 285 String settings = jEdit.getSettingsDirectory(); 286 if(abbrevsChanged && settings != null) 287 { 288 File file1 = new File(MiscUtilities.constructPath(settings,"#abbrevs#save#")); 289 File file2 = new File(MiscUtilities.constructPath(settings,"abbrevs")); 290 if(file2.exists() && file2.lastModified() != abbrevsModTime) 291 { 292 Log.log(Log.WARNING,Abbrevs.class,file2 + " changed on disk;" 293 + " will not save abbrevs"); 294 } 295 else 296 { 297 jEdit.backupSettingsFile(file2); 298 299 try 300 { 301 saveAbbrevs(new OutputStreamWriter( 302 new FileOutputStream(file1), 303 ENCODING)); 304 file2.delete(); 305 file1.renameTo(file2); 306 } 307 catch(Exception e) 308 { 309 Log.log(Log.ERROR,Abbrevs.class,"Error while saving " + file1); 310 Log.log(Log.ERROR,Abbrevs.class,e); 311 } 312 abbrevsModTime = file2.lastModified(); 313 } 314 } 315 } 317 319 private static boolean loaded; 321 private static boolean abbrevsChanged; 322 private static long abbrevsModTime; 323 private static boolean expandOnInput; 324 private static Hashtable globalAbbrevs; 325 private static Hashtable modes; 326 327 328 private static Vector m_pp = new Vector(); 329 331 private Abbrevs() {} 332 333 static 334 { 335 expandOnInput = jEdit.getBooleanProperty("view.expandOnInput"); 336 } 337 338 private static void load() 340 { 341 globalAbbrevs = new Hashtable(); 342 modes = new Hashtable(); 343 344 String settings = jEdit.getSettingsDirectory(); 345 if(settings != null) 346 { 347 File file = new File(MiscUtilities.constructPath(settings,"abbrevs")); 348 abbrevsModTime = file.lastModified(); 349 350 try 351 { 352 loadAbbrevs(new InputStreamReader( 353 new FileInputStream(file),ENCODING)); 354 loaded = true; 355 } 356 catch(FileNotFoundException fnf) 357 { 358 } 359 catch(Exception e) 360 { 361 Log.log(Log.ERROR,Abbrevs.class,"Error while loading " + file); 362 Log.log(Log.ERROR,Abbrevs.class,e); 363 } 364 } 365 366 if(!loaded) 368 { 369 try 370 { 371 loadAbbrevs(new InputStreamReader(Abbrevs.class 372 .getResourceAsStream("default.abbrevs"), 373 ENCODING)); 374 } 375 catch(Exception e) 376 { 377 Log.log(Log.ERROR,Abbrevs.class,"Error while loading default.abbrevs"); 378 Log.log(Log.ERROR,Abbrevs.class,e); 379 } 380 loaded = true; 381 } 382 } 384 private static int countNewlines(String s, int end) 386 { 387 int counter = 0; 388 389 for(int i = 0; i < end; i++) 390 { 391 if(s.charAt(i) == '\n') 392 counter++; 393 } 394 395 return counter; 396 } 398 private static Expansion expandAbbrev(String mode, String abbrev, 400 int softTabSize, Vector pp) 401 { 402 m_pp = pp; 403 if(!loaded) 404 load(); 405 406 String expand = null; 408 Hashtable modeAbbrevs = (Hashtable)modes.get(mode); 409 if(modeAbbrevs != null) 410 expand = (String )modeAbbrevs.get(abbrev); 411 412 if(expand == null) 413 expand = (String )globalAbbrevs.get(abbrev); 414 415 if(expand == null) 416 return null; 417 else 418 return new Expansion(expand,softTabSize,m_pp); 419 } 421 private static void loadAbbrevs(Reader _in) throws Exception 423 { 424 BufferedReader in = new BufferedReader(_in); 425 426 try 427 { 428 Hashtable currentAbbrevs = globalAbbrevs; 429 430 String line; 431 while((line = in.readLine()) != null) 432 { 433 int index = line.indexOf('|'); 434 435 if(line.length() == 0) 436 continue; 437 else if(line.startsWith("[") && index == -1) 438 { 439 if(line.equals("[global]")) 440 currentAbbrevs = globalAbbrevs; 441 else 442 { 443 String mode = line.substring(1, 444 line.length() - 1); 445 currentAbbrevs = (Hashtable)modes.get(mode); 446 if(currentAbbrevs == null) 447 { 448 currentAbbrevs = new Hashtable(); 449 modes.put(mode,currentAbbrevs); 450 } 451 } 452 } 453 else if(index != -1) 454 { 455 currentAbbrevs.put(line.substring(0,index), 456 line.substring(index + 1)); 457 } 458 } 459 } 460 finally 461 { 462 in.close(); 463 } 464 } 466 private static void saveAbbrevs(Writer _out) throws Exception 468 { 469 BufferedWriter out = new BufferedWriter(_out); 470 String lineSep = System.getProperty("line.separator"); 471 472 out.write("[global]"); 474 out.write(lineSep); 475 476 saveAbbrevs(out,globalAbbrevs); 477 478 Enumeration keys = modes.keys(); 480 Enumeration values = modes.elements(); 481 while(keys.hasMoreElements()) 482 { 483 out.write('['); 484 out.write((String )keys.nextElement()); 485 out.write(']'); 486 out.write(lineSep); 487 saveAbbrevs(out,(Hashtable)values.nextElement()); 488 } 489 490 out.close(); 491 } 493 private static void saveAbbrevs(Writer out, Hashtable abbrevs) 495 throws Exception 496 { 497 String lineSep = System.getProperty("line.separator"); 498 499 Enumeration keys = abbrevs.keys(); 500 Enumeration values = abbrevs.elements(); 501 while(keys.hasMoreElements()) 502 { 503 String abbrev = (String )keys.nextElement(); 504 out.write(abbrev); 505 out.write('|'); 506 out.write(values.nextElement().toString()); 507 out.write(lineSep); 508 } 509 } 511 513 static class Expansion 515 { 516 String text; 517 int caretPosition = -1; 518 int lineCount; 519 520 int posParamCount; 522 523 Expansion(String text, int softTabSize, Vector pp) 525 { 526 StringBuffer buf = new StringBuffer (); 527 boolean backslash = false; 528 529 for(int i = 0; i < text.length(); i++) 530 { 531 char ch = text.charAt(i); 532 if(backslash) 534 { 535 backslash = false; 536 537 if(ch == '|') 538 caretPosition = buf.length(); 539 else if(ch == 'n') 540 { 541 buf.append('\n'); 542 lineCount++; 543 } 544 else if(ch == 't') 545 { 546 if(softTabSize == 0) 547 buf.append('\t'); 548 else 549 { 550 for(int j = 0; j < softTabSize; j++) 551 buf.append(' '); 552 } 553 } 554 else 555 buf.append(ch); 556 } 557 else if(ch == '\\') 558 backslash = true; 559 else if(ch == '$') 562 { 563 if(i != text.length() - 1) 564 { 565 ch = text.charAt(i + 1); 566 if(Character.isDigit(ch) && ch != '0') 567 { 568 i++; 569 570 int pos = ch - '0'; 571 posParamCount = Math.max(pos,posParamCount); 572 if(pos <= pp.size()) 575 buf.append(pp.elementAt(pos - 1)); 576 } 577 else 578 { 579 buf.append('$'); 582 } 583 } 584 else 585 buf.append('$'); } else 588 buf.append(ch); 589 } 590 591 this.text = buf.toString(); 592 } } } 595 | Popular Tags |