PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/macros/Misc/Include_Guard.bsh

#
Unknown | 96 lines | 88 code | 8 blank | 0 comment | 0 complexity | 9cb8377133ab4bd4e13dbb3086624e9c 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. * Include_Guard.bsh - a BeanShell macro script for the
  3. * jEdit text editor - for C/C++ header files: inserts preprocessor
  4. * directive in current buffer to ensure that header is included only
  5. * once per compilation unit
  6. * Copyright (C) 2001 John Gellene
  7. * jgellene@nyc.rr.com
  8. * http://community.jedit.org
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with the jEdit program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. * $Id: Include_Guard.bsh 3873 2001-11-06 17:57:35Z jgellene $
  25. *
  26. * Notes on use:
  27. *
  28. * An "include guard" is a conventional mechanism for reducing
  29. * compilation time for C/C++ source files by ensuring that the
  30. * substance of a header file is read by the preprocessor only once
  31. * for each compilation unit. The usual reason this is necessary is
  32. * the presence of nested include files, sometimes an unavoidable
  33. * circumstance when creating a hierarchy of classes.
  34. *
  35. * To use the macro, first place the caret at the beginning of the header
  36. * file before any uncommented text. The macro returns to this position
  37. * upon completion.
  38. *
  39. * The macro will complain if you have not yet named the buffer, but it
  40. * does not check to see if the buffer is actually a header file.
  41. *
  42. * The defined term that triggers the guard is taken from the buffer's
  43. * name. This is one conventional approach and should not cause a conflict
  44. * unless a compilation unit includes header files from different
  45. * directories with the same name. In that case, change the conflicting
  46. * guard names manually.
  47. *
  48. *
  49. * Checked for jEdit 4.0 API
  50. *
  51. */
  52. void includeGuard()
  53. {
  54. if(buffer.isUntitled())
  55. {
  56. Macros.error(view, "Name the file before inserting an include guard.");
  57. return;
  58. }
  59. // you can change the routine for creating the guard name here
  60. guardName = "__" + buffer.getName().toUpperCase().replace('.', '_')
  61. + "__";
  62. pos = textArea.getCaretPosition();
  63. textArea.setSelectedText("#if !defined(" + guardName
  64. + ")\n#define " + guardName + "\n");
  65. textArea.setCaretPosition(buffer.getLength());
  66. textArea.setSelectedText("\n#endif // #if !defined(" +
  67. guardName + ")\n");
  68. textArea.setCaretPosition(pos);
  69. }
  70. includeGuard();
  71. /*
  72. Macro index data (in DocBook format)
  73. <listitem>
  74. <para><filename>Include_Guard.bsh</filename></para>
  75. <abstract><para>
  76. Intended for C/C++ header files, this macro inserts a preprocessor
  77. directive in the current buffer to ensure that the header is
  78. included only once per compilation unit.
  79. </para></abstract>
  80. <para>
  81. To use the macro, first place the caret at the beginning of the
  82. header file before any uncommented text. The macro will return to
  83. this position upon completion. The defined term that triggers the
  84. <quote>include guard</quote> is taken from the buffer's name.
  85. </para>
  86. </listitem>
  87. */
  88. // end Include_Guard.bsh