PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Unknown | 72 lines | 69 code | 3 blank | 0 comment | 0 complexity | fb8b00fedea2818dd0af6427421fca4e 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 5378 2006-04-27 08:58:11Z ezust $
  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. guardName = buffer.getName().toUpperCase().replace('.', '_') ;
  60. pos = textArea.getCaretPosition();
  61. textArea.setSelectedText("#ifndef " + guardName + "\n#define " + guardName + "\n");
  62. textArea.setCaretPosition(buffer.getLength());
  63. textArea.setSelectedText("\n#endif // #ifndef " + guardName + "\n");
  64. textArea.setCaretPosition(pos);
  65. }
  66. if( buffer.isReadOnly() )
  67. Macros.error(view, "Buffer is read-only.");
  68. else
  69. includeGuard();