.
.   
NEdit.org
   .

Home
 - News - Download - Documentation - Search - FTP     
.
.
  

Open include files

[Back]

Here are some macros which will search for include files in C/C++ (i.e. starting with a #include) using a colon-separated list of paths in an environment variable, $NEDIT_INCLUDE_PATH. It's probably pretty fragile. To get this to work, add a macro menu item which calls open_include(). Note that you might have to fiddle with some of the shell commands if you're not using csh.

The macros are slightly broken for relative paths. If you set your environment variable to .:../include:/usr/include, edit a file in your source directory (for example), use the open_include() macro to open a file in ../include, the current directory is set to ../include in shell commands fired from that window, but remains in your source directory when the attempt is made to actually open the file.

What that means is, within the header file you just opened, if you attempt to use open_include() to open another header file which is also in the include directory (header2.h, say), it will fail. The shell_command("test -r "...) command in the open_with_test() macro will indicate that the file ./header2.h exists (because the shell process did a chdir() to ../include), but the open() command will fail because source/./header2.h won't exist.

One (crude) way to fix that would be to assign your current directory to a global macro variable at startup and always prepend that to relative paths before opening files in open_with_test(). A better solution might be to remove the chdir() in shell.c, but I have no idea what else that might break.


#
#  $g_include_path -- this bit depends on your shell.
#
$g_include_path = ".:/usr/include:/usr/include/CC"  #  useful default
include_path = shell_command("echo $NEDIT_INCLUDE_PATH", "")

if($shell_cmd_status == 0)
{
    #  Strip trailing newline.  You might want to do other checks here
    #  for 0-length path elements, etc.
    $g_include_path = substring(include_path, 0, length(include_path) -1)
}

#  open_with_test
#
#  If the given file is readable, this opens it & returns "yay".
#  Otherwise it just returns "".
#
define open_with_test
{
    filename = $1
    shell_command("test -r " filename, "")
    if($shell_cmd_status == 0)
    {
        open(filename)
        return "yay"
    }
    return ""
}


#  open_include
#
#  This attempts to find an #include file name in the current line and
#  search for it in the global include path.
#
define open_include
{
    #
    #  Try to get the filename from the current line.  All this does
    #  is look for a string delimited by <> or "".
    #
    #  There may be a better/built-in way to get the text of the current line.
    bpos = search("^", $cursor, "regex", "backward")
    epos = search("$", $cursor, "regex", "forward")
    line = get_range(bpos, epos)

    #  See if we can find a filename delimited by <>.  This is pretty fragile,
    #  & doesn't permit "< file.h >", etc.
    filename = replace_in_string(line, "^.+\\<(.+)\\>.*$", "\\1", "regex")

    if(filename == "")
    {
        #  OK, see if we can find a filename delimited by ""
        filename = replace_in_string(line, "^.+\"(.+)\".*$", "\\1", "regex")
    }
    if(filename == "")
    {
        dialog("No filename found in line " $line "!\n" \
               "I'm looking for <filename> or \"filename\".", "Oops.")
        return ""
    }

    #
    #  We have a filename; now run through our list of paths & see
    #  if we can open the file.
    #
    include_path = $g_include_path
    path_separator = ":"
    bpos = 0
    epos = search_string(include_path, path_separator, bpos)
    while(epos > bpos)
    {
        path_bit = substring(include_path, bpos, epos)
        if(open_with_test(path_bit "/" filename) != "") return ""
        bpos = epos + length(path_separator)
        epos = search_string(include_path, path_separator, bpos)
    }
    #  Do the last element in the path.
    path_bit = substring(include_path, bpos, length(include_path))
    if(open_with_test(path_bit "/" filename) != "") return ""

    #
    #  Bummer!  Might as well open a file-open dialog...
    #
    open_dialog(filename)
}
[Back]

. Released on  Wed, 21 Nov 2001  by C. Denat  

  
Visit our sponsor
Check Metrolink

© NEdit.org
1999-2001
All rights reserved


.