PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/C/Toggle_Header_Source.bsh

#
Unknown | 76 lines | 61 code | 15 blank | 0 comment | 0 complexity | 354392c6ee12cc04ff2cb4d1d311b440 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /**
  2. ToggleHeaderSource.bsh
  3. by Alan Ezust
  4. $Id: Toggle_Header_Source.bsh 5450 2006-06-20 09:08:13Z vampire0 $
  5. alan dot ezust at gmail dot com
  6. 2005-11-09
  7. A jedit beanshell macro that toggles your current buffer
  8. between the header file (.h) and the source file (.c(pp|xx)?).
  9. Enables you to switch the current text
  10. buffer between C/C++ header and sourcecode
  11. file. If the file does not already exist, it opens a buffer
  12. of that name for you (!).
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with the jEdit program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. String defaultSourceExtension = "cpp";
  26. String[] sourceExtensions = new String[]{"cpp", "c", "cxx" };
  27. String getSourceFile(String baseName)
  28. {
  29. int numExt = sourceExtensions.length;
  30. for (int i=numExt-1; i>=0; --i)
  31. {
  32. String ext = sourceExtensions[i];
  33. String tryFile = baseName + "." + ext;
  34. File f = new File(tryFile);
  35. if (f.canRead()) return f.getPath();
  36. }
  37. return baseName + "." + defaultSourceExtension;
  38. }
  39. boolean isSourceFile(String extension)
  40. {
  41. for (int i=0; i<sourceExtensions.length; ++i) {
  42. if (extension.equals(sourceExtensions[i])) return true;
  43. }
  44. return false;
  45. }
  46. void toggleHeaderSource()
  47. {
  48. String currentFile = buffer.getPath();
  49. int pos = currentFile.lastIndexOf('.');
  50. String extension = currentFile.substring(pos+1);
  51. String baseName = currentFile.substring(0, pos);
  52. if (isSourceFile(extension))
  53. {
  54. jEdit.openFile(view, baseName + ".h");
  55. }
  56. else if (extension.equals("h")) {
  57. String sourceFile = getSourceFile(baseName);
  58. jEdit.openFile(view, sourceFile);
  59. }
  60. }
  61. toggleHeaderSource();