How to Run C/Ch/C++ Programs
from TextWrangler in Mac OS X

TextWrangler for editing

Ch is a free C/C++ interpreter for cross-platform scripting, shell programming, 2D/3D plotting, numerical computing, and embedded scripting.

TextWrangler is a freely available text editor for the Mac aimed at programmers and others. It can be downloaded from the BareBones website. It has lots of nice features and is free. Since it's not a true IDE, programs can not be run directly from the text editing window. There is a way around this because TextWrangler allows you to run scripts, and Ch programs can be run as scripts.

Setting up TextWrangler

First you can set up TextWrangler with Ch specific keywords for syntax coloring. Provided here is a Codeless Language Module for Ch. Control-click on the file and Save to .... Make sure to delete the ".xml" extention if added when you download the file so that it has an extension of ".plist". Move this file to the following folder:
"Your Home folder" → LibraryApplication SupportTextWranglerLanguage Modules
You may have to add the last folder manually if it's not already there.

Now TextWrangler will color all keywords in a Ch program. You can still use coloring for the C language, it just won't highlight all the additional keywords that Ch has. The following screen-shot shows the Language Menu in a TextWrangler window.

showing how to pick a language in TextWrangler

One thing that doesn't work with the codeless language modules right away is the Un/Comment Selection item in the Text menu. To turn this on, open the Preferences and click on Languages selection on the left. In the top right window, select the Codeless Ch Language Module and click the Options button. This will let you set beginning and ending characters for comments. Show below is a screen shot of this menu. Here, I've set comments to be standard C style comments. You can also use C++ style comments in Ch, beginning with "// " and leaving the ending blank.

showing how to set comment preferences for a language in TextWrangler

The other change you can make here is to add the ".ch" as a Suffix Mapping. Click the Add... button, put ".ch" in the resulting dialog box, and choose what language you want. This way, files with this suffix will automatically use the specified laguage syntax coloring.

Modifying a Ch program to run as a script

To make a Ch program run from "within" TextWrangler, you need to add one line to the very beginning of the program. The first line should be exactly as follows:
For those unfamiliar with scripting, this is refered to as the "she-bang" line because of the #! symbols. It tells the computer what type of shell to use when executing the following code. In this case, it's a Ch shell. Make sure that X11 is already running if the output of the program is a plot or an animation, unless you're simply writing the graphical output to a file because X11 is not necessary to write these files. TextWrangler will launch the Terminal Application in order to run the script, but won't launch X11 on its own.

To run the program click the "#!" menu and choose Run in Terminal. A new Terminal window will open with the output of your program. If the output is just text, clicking Run in the same menu will send the output to a new TextWrangler document. This screen-shot shows a simple program in TextWrangler, the "#!" menu, and the output in the resulting Terminal window.

showing menu to run script in TextWrangler

How to Plot from Ch Programs
Using AquaTerm in Mac OS X

AquaTerm for plotting

AquaTerm is an open source application for Mac OS X that provides a GUI interface for plotting programs. It can be used to display the plots created by Ch. AquaTerm is a standalone application that can be downloaded through the download link on its homepage. There are a few setup steps involved, that are detailed below. You need to use Ch version 5.5 or later for Aquaterm compatibility.

Plots are displayed using X11 by default. Installation instructions for X11 can be found by searching for "x11 install" on Apple's webpage.

Setting Ch to use AquaTerm

One variable needs to be set to make Ch push its plots to Aquaterm. There are two steps involved to make this work once you've installed Aquaterm.

1. Create a ".chrc" file. This is a start-up file that will set certain variable everytime Ch starts. To create this file, open the Terminal application and start Ch by typing with the following command.
ch -d
The -d option tells Ch to copy the default ".chrc" file to your home directory. Shown below is a Terminal window where the above command has been run.

Creating the chrc file

2. Edit the ".chrc" file. One line needs to be added to this new file. Since this is a hidden file, you need to use the "Open Hidden..." command in TextWrangler's "File" menu. To allow you to select the ".chrc" file you need to select "All Files" in the "Enable" drop down menu at the very top of this window. The ".chrc" file will in the top level of your home directory. Once open, add the following line just as it appears below.
This tells Ch to use Aquaterm as the output for plotting. This line may already exist and just be commented out. If this is the case, just uncomment the line by getting rid of the "//" at the beginning of the line. The highlighted line in the picture below shows the line added to the ".chrc" file. This file is read like a standard C file, so if you choose "ANSI C" from the "Language" drop down as shown in the first picture on the page, the lines that are commented out will show as a different color.

Editing the chrc file

Now when you create a plot in Ch, the output will be sent to AquaTerm, which will launch automatically if it's not already running. From there the plot can be saved as a PDF file for future use.

A simple plotting program and the output