PageRenderTime 71ms CodeModel.GetById 64ms app.highlight 3ms RepoModel.GetById 1ms 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
 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   
 8    A jedit beanshell macro that toggles your current buffer
 9    between the header file (.h) and the source file (.c(pp|xx)?).
10
11    Enables you to switch the current text
12    buffer between C/C++ header and sourcecode
13    file. If the file does not already exist, it opens a buffer
14    of that name for you (!).
15
16   This program is free software; you can redistribute it and/or
17   modify it under the terms of the GNU General Public License
18   as published by the Free Software Foundation; either version 2
19   of the License, or any later version.
20
21   This program is distributed in the hope that it will be useful,
22   but WITHOUT ANY WARRANTY; without even the implied warranty of
23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24   GNU General Public License for more details.
25
26   You should have received a copy of the GNU General Public License
27   along with the jEdit program; if not, write to the Free Software
28   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29
30 */
31
32String defaultSourceExtension = "cpp";  
33
34String[] sourceExtensions = new String[]{"cpp", "c", "cxx" };
35
36String getSourceFile(String baseName)
37{
38    int numExt = sourceExtensions.length;
39    for (int i=numExt-1; i>=0; --i) 
40    {
41        String ext = sourceExtensions[i];
42        String tryFile = baseName + "." + ext;
43        File f = new File(tryFile);
44        if (f.canRead()) return f.getPath();
45    }              
46    return baseName + "." + defaultSourceExtension;
47}
48
49boolean isSourceFile(String extension) 
50{
51    for (int i=0; i<sourceExtensions.length; ++i) {
52        if (extension.equals(sourceExtensions[i])) return true;
53    }
54    return false;
55}
56
57void toggleHeaderSource()
58{
59
60    String currentFile = buffer.getPath();
61    int pos = currentFile.lastIndexOf('.');
62    String extension = currentFile.substring(pos+1);
63    String baseName = currentFile.substring(0, pos);
64    if (isSourceFile(extension)) 
65    {
66        jEdit.openFile(view, baseName + ".h"); 
67    }
68    else if (extension.equals("h")) {
69        String sourceFile = getSourceFile(baseName);
70        jEdit.openFile(view, sourceFile);         
71    }
72}
73
74toggleHeaderSource();
75
76