PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms 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
 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
53void includeGuard()
54{
55    if(buffer.isUntitled())
56    {
57        Macros.error(view, "Name the file before inserting an include guard.");
58        return;
59    }
60
61    guardName = buffer.getName().toUpperCase().replace('.', '_') ;
62    pos = textArea.getCaretPosition();
63    textArea.setSelectedText("#ifndef " + guardName + "\n#define " + guardName + "\n");
64    textArea.setCaretPosition(buffer.getLength());
65    textArea.setSelectedText("\n#endif        //  #ifndef " + guardName + "\n");
66    textArea.setCaretPosition(pos);
67}
68
69if( buffer.isReadOnly() ) 
70    Macros.error(view, "Buffer is read-only.");
71else 
72    includeGuard();