PageRenderTime 39ms CodeModel.GetById 36ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/macros/Misc/Include_Guard.bsh

#
Unknown | 96 lines | 88 code | 8 blank | 0 comment | 0 complexity | 9cb8377133ab4bd4e13dbb3086624e9c MD5 | raw file
 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
53void includeGuard()
54{
55	if(buffer.isUntitled())
56	{
57		Macros.error(view, "Name the file before inserting an include guard.");
58		return;
59	}
60	// you can change the routine for creating the guard name here
61	guardName = "__" + buffer.getName().toUpperCase().replace('.', '_')
62		+ "__";
63	pos = textArea.getCaretPosition();
64	textArea.setSelectedText("#if !defined(" + guardName
65		+ ")\n#define " + guardName + "\n");
66	textArea.setCaretPosition(buffer.getLength());
67	textArea.setSelectedText("\n#endif        //  #if !defined(" +
68		guardName + ")\n");
69	textArea.setCaretPosition(pos);
70}
71
72includeGuard();
73
74/*
75	Macro index data (in DocBook format)
76
77<listitem>
78    <para><filename>Include_Guard.bsh</filename></para>
79    <abstract><para>
80		Intended for C/C++ header files, this macro inserts a preprocessor
81		directive in the current buffer to ensure that the header is
82		included only once per compilation unit.
83    </para></abstract>
84    <para>
85        To use the macro, first place the caret at the beginning of the
86		header file before any uncommented text.  The macro will return to
87		this position upon completion.  The defined term that triggers the
88		<quote>include guard</quote> is taken from the buffer's name.
89    </para>
90</listitem>
91
92*/
93
94
95// end Include_Guard.bsh
96