#
Interapplication Communication

This chapter addresses communication between Source-Navigator and external applications, and provides examples of how to control Source-Navigator from other applications.
These examples use the Source-Navigator Tcl interpreter called hyper. To use the standard Tcl/Tk interpreter, replace the `.../snavigator/bin/hyper` string with the appropriate name; for example `/usr/local/bin/wish`.
After an external application connects to a Source-Navigator project, it may be controlled remotely using the Tk `send` command.

## The Tk Send Command

The communication between external applications and Source-Navigator can be achieved using the Tk `send` command. Communicating with Source-Navigator from another application requires that the application know the name of the interpreter running Source-Navigator. The Tk command `winfo interps` is useful for determining the name of all the interpreters active on a host.
Further information on the Tk `send` command can be found in one of the Tcl/Tk texts mentioned in the introduction.

### Multiple Source-Navigator Interpreters

An application may need to query every Source-Navigator interpreter name in the network. The example below queries every Source-Navigator interpreter name and fetches the name of the Source-Navigator projects. Then if the string `c++_demo` is found, the project name reloads files specified in the command line.

    #!/bin/sh
    # REPLACE $HOME/snavigator/bin/hyper with the
    # Source-Navigator installation directory! \
    exec $HOME/snavigator/bin/hyper "$0" "$@"
    #
    # Don't forget the backslash!
    #
    # This program instructs all Source-Navigator applications
    # running on this host to reload the files specified on the
    # command line. The project currently opened by each
    # Source-Navigator application must contain the string
    # "c++_demo".
    # The C++ demo project, c++_demo.proj, is optionally generated
    # during the Source-Navigator installation process.
    # We don't need the top-level window for this program.
    wm withdraw .
    # Scan through all the Tcl-based interpreters on this system
    # and try and find running Source-Navigators.
    foreach interpreter [winfo interps] {
      if {![string match "*navigato*" $interpreter]} {
        continue ;# not S-N, keep looking.
      } else {
        set sn $interpreter ;# found one.
      }
      set msg ""
      # Set the command that we're going to send to the
      # application (using "set" on a temporary variable to
      # examine the name of the currently opened project).
      set cmd {set tmp sn_options(ProjectName)}
      # Send the command to the other application and capture the
      # results.
      catch {set result [send $sn $cmd]} msg
      if {$msg != ""} {
        puts $msg
      }
      # See if the project name contains the substring "c++_demo".
      # If so, then issue the command "paf_parse_uptodate" within
      # the remote interpreter.
      if {[string match "*c++_demo*" $result]} {
        send $sn_parse_uptodate [list $argv]
      }
    }

    # Force our way out of this event-driven shell.
    exit

If the script above is executable and the path to the interpreter is correct, the script will reparse and reload all files specified on the command line. If a listed file is being edited in a Source-Navigator editor window, the contents of the editor window will be updated.

Note: | File names have to be used relative to the project root directory if the file is in a subdirectory of the project; otherwise, the file names must be specified with absolute path names ( `./` and `../` cannot be used in file names).