Pipe: Uses Automation to Filter or Pipe Output from the Text Editor

Click to open or copy the Pipe project files.

Pipe (or Filter as it is called in the Customize dialog box) lets you process text by selecting it in the Visual C++ Text editor, pressing a button, choosing or setting a command, and applying the command to your selection. It is useful in sorting.

Pipe was created with the ATL COM AppWizard. Support MFC was checked in the wizard, and then an Add-in object was added to the project with the ATL Object Wizard. Most of the code added is in MarkIt.cpp.

If you modify the sample, precede each function that could be called from an external interface with the following code:

AFX_MANAGE_STATE(AfxGetStaticModuleState());

This allows you to access your resources and is necessary because of the MFC support.

The CMRUStrings class is used to do the persistence for a drop-down combo box. You can use this class and other classes from the add-ins in other projects. See Reusing Code Topics and Adding Classes to the Gallery for information on how to reuse code.

To build the sample

  1. Click the link at the beginning of this topic to download the sample files.

  2. In Visual C++, open the project file Pipe.dsp.

  3. Choose an appropriate configuration (Win32 Release is best if you plan to use this add-in).

  4. Click Rebuild All from the Build menu.

To run the add-in

  1. From the Tools menu, click Customize.

  2. Click the Add-ins and Macro Files tab.

  3. Select the Pipe.dll file or browse for it. See Tips for Using Add-ins.

    Visual C++ will load the add-in. When you close the Customize dialog box, Pipe’s toolbar will appear.

  4. Run the Pipe add-in by clicking the toolbar button, which will display a dialog box where you can set the command line. For example, you can sort the selection by setting the command to c:\winnt\system32\sort.exe. Or you could issue a dir command with the output inserted into your current file.

Pipe will work on a selection size of 0, on a column selection (SHIFT+ALT and select), whole lines, or partial lines. It will pipe the selection into your command, or allow you to use the selection as a file passed as an argument. You can write or append to a specified file instead of replacing the text in your editor. The buttons in the box named Add To Command are helpers that put strings that will be replaced with file names or a number. These are: the current text editor’s file name; the selection from that file written out to a temporary file as a file name; the starting line of the selection; the ending line of the selection. If you put a file in the Output To edit box, you will turn off the filter feature, and all output from the command will go into this file. Setting the Append to this File check box prevents this file from being destroyed before being written to.

Consider writing your own filters by creating a simple console application. For instance, you could write a filter to remove extra carriage returns and line feeds. Another useful filter would convert hexadecimal numbers to decimal, or the opposite. As a last suggestion, a column organizer is a useful tool for formatting lines by field.

This sample demonstrates the following keywords:

CTimeSpan; CTime::GetCurrentTime; CTime::Format; CRegKey::QueryValue; CRegKey::Create; CRegKey::SetValue, ITextSelection, IApplication, CRegKey; ITextSelection::get_TopLine; CComPtr, CComQIPtr, ITextDocument::get_FullName; CComBSTR, IApplication::get_ActiveDocument; ITextSelection::MoveTo; ITextSelection::put_Text; ITextSelection::get_Text; ITextSelection::EndOfLine; ITextSelection::get_CurrentColumn; ITextSelection::get_TopLine; ITextSelection::get_BottomLine; ITextDocument::get_Selection; ITextDocument::get_Name; CString::Remove; CString::Replace; pipe; filter; addin; customize; CComboBox