# 5.

Editing Source Code

This chapter explains how to use the CodeWarrior text editor to edit your source code.

## Source Code Editor Overview

The CodeWarrior editor is a full-featured text editor specially designed for programmers, with features such as:

- Pop-up menus on every editor window for opening your interface files and navigating your routines quickly
- Colored text highlights for easy identification of comments and keywords in your source files

The topics in this chapter are:
- `Guided Tour of the Editor Window`
- `Editor Window Configuration`
- `Basic Text Editing`
- `Navigating in Text`

You can also change options that affect the way the editor works. See `"Editor"`.

## Guided Tour of the Editor Window

The CodeWarrior editor, shown in Figure 5.1, contains elements you'll find useful when viewing and editing your source files.

#### Figure 5.1 The Editor window

To see an Editor window, create a new text file using the `New Text` command on the File Menu.

The sections that follow describe the elements of the Editor window shown in `Figure 5.1`.

- `Text Editing Area`
- `Interface Pop-up Menu`
- `Routine Pop-up Menu`
- `Options Pop-up Menu`
- `Permissions Pop-Up Menu`
- `Read Only Pop-Up Menu`
- `Line Number Display`

### Text Editing Area

The Text Editing Area of the Editor window is where your text is entered in your new window.

### Interface Pop-up Menu

Use the Interface pop-up menu to open interface or header files referenced by the current file, as shown in `Figure 5.2`. You can also use the Touch command from this pop-up.

#### Figure 5.2 The Interface pop-up menu

To open a file in the list, scroll to the file you want to see and release the mouse button.

For more information on opening files, see `"Opening an Existing File."`

To cause your file to be recompiled the next time the project is built, you choose the Touch command. If you click the Interface pop-up again you can deselect the file for compilation with the Untouch command on the menu.

### Routine Pop-up Menu

Use the Routine pop-up menu shown in `Figure 5.3` to set the current location of the text insertion point in your text files.

The checked routine in the pop-up contains the current location of the text insertion point.

#### Figure 5.3 The Routine pop-up menu

The Routine pop-up menu lists the routines in your source file.

### NOTE:

If the pop-up is empty, the file is not a source file

Note that, by default, the menu lists the routines in the order in which they appear in the file. If you'd like to list routines alphabetically, hold down the Ctrl or Option key as you click on the routine icon.

### Options Pop-up Menu

Use the Options pop-up menu, shown in `Figure 5.4`, to choose syntax styling for the current file, and also to set the format for how to save the file.

The Macintosh, DOS, and Be options indicate the file format of the file currently open in the Editor window. The checkmark indicates the current file format. The next time you save the file, CodeWarrior saves it in the selected format.

#### Figure 5.4 The Options pop-up menu

For more information on how to use the options on this pop-up menu, see `"Saving as a Be, Macintosh, or DOS text file."`

For more information on the Syntax Styling option shown in this menu, see `"Syntax Styling."`

### Permissions Pop-Up Menu

The Permissions pop-up menu, as shown in `Figure 5.5`, indicates the read/write status of the file you are currently editing. The icon indicates whether the file is locked or unlocked.

To lock or unlock a file, click the icon and choose the listed option to change the permisson for the file.

#### Figure 5.5 Permissions pop-up menu

### Read Only Pop-Up Menu

The Read Only pop-up menu, shown below in `Figure 5.6`, appears in the place of the Permissions pop-up menu when you open a file that cannot be modified. For example, if you open a file from a CD-ROM in the IDE, the Read Only icon will appear, since this file cannot be modified.

#### Figure 5.6 Read Only pop-up menu

### Line Number Display

The line number box, as shown in `Figure 5.1`, displays the line number that contains the text insertion point.

## Editor Window Configuration

The Editor allows you to customize your view of the file you're working with. In this section, you'll learn about the following topics:

- `Setting Text Size and Font`
- `Navigating in Text`

### Setting Text Size and Font

You set the size or font used to display text in an Editor window in the Fonts & Tabs preference panel. For more information, see `"Font."`

## Basic Text Editing

CodeWarrior gives you lots of help in editing source files, all of it described in the sections that follow.

The topics in this section are:

- `Basic Editor Window Navigation`
- `Adding Text`
- `Deleting Text`
- `Selecting Text`
- `Drag & Drop Editing`
- `Using Cut, Copy, Paste, and Clear`
- `Balancing Punctuation`
- `Shifting Text Left and Right`
- `Undoing Changes`
- `Controlling Color`

### Basic Editor Window Navigation

CodeWarrior gives you several ways to move the text insertion point in a file. It would be useful to revisit this section after you become more familiar with the CodeWarrior Editor's features.

#### Scroll bar navigation

The CodeWarrior editor provides dynamic scrolling, which means that as you drag the Scroll Bar at the far right of the Editor window, the CodeWarrior Editor updates the contents. See Figure 5.7 to see what the Scroll Bars look like.

You can see the text scroll by in the Editor window as you click and drag the Scroll Bar thumb. Use the Scroll Bar thumb at the bottom of the editor window to scroll left and right.

#### Figure 5.7 Scroll Bars in an Editor Window

#### Keyboard navigation

Table 5.1 describes how to move the insertion point around a file with function keys.

#### Table 5.1 Text navigation with the keyboard

To move insertion point to | Press
--- | ---

Previous word

Ctrl-Left Arrow

Next word

Ctrl-Right Arrow

Beginning of the line

Alt-Left Arrow

End of the line

Alt-Right Arrow

Previous page

Ctrl-Up Arrow

Next page

Ctrl-Down Arrow

Beginning of the file

Alt-Up Arrow

End of the file

Alt-Down Arrow

Table 5.2 describes how to scroll to different locations in a file, without moving the insertion point. Note that some of the keys listed in the table may not be on your keyboard, depending on what kind of keyboard you have.

#### Table 5.2 Scroll navigation keys

To scroll to the | Press this
--- | ---

Previous page

Page Up

Next page

Page Down

Beginning of the file

Home

End of the file

End

Insertion point

Left Arrow, and Right Arrow

### Adding Text

To add text to a file you've opened, click once in the `Text Editing Area` of the window to set the new location of the text insertion point. After you see the insertion point at the new location, you may begin typing on the keyboard to enter text.

To read about different ways to move the insertion point in an Editor window, see `"Basic Editor Window Navigation."`

### Deleting Text

There are several different methods for deleting text.

To delete text that you just typed, hit the Backspace key.

To delete text that is in front of the text insertion point, use the Delete key.

To delete more than one contiguous character at a time, select the text you want to delete and hit the Backspace or Delete key.

If you don't know how to select text, see `"Selecting Text."`

### Selecting Text

There are several different ways to select text in the Editor window.

To select a word, double-click on the word.

To select a line, triple click anywhere in the line.

You can also select text by holding down the Shift key while pressing any of the shortcuts listed in Table 5.1.

To select a range of text, click and drag the mouse in a portion of your window where there is text. Another way to select a range is to set your insertion point somewhere in your window. Then, press the Shift key and click at the place in your text where you want the range to end.

To list and display an entire routine in the Editor window, press the Shift key while selecting a routine in the `Routine Pop-up Menu`. This is particularly useful for copy and paste operations in your file.

### Using Cut, Copy, Paste, and Clear

There are standard menu commands available on most computer applications, called Cut, Copy, Paste, and Clear. In CodeWarrior, these commands appear on the Edit Menu.

You use these commands to remove text, or to copy and paste in a window, between windows, or between applications.

For more information about these commands, refer to `"Edit Menu."`

### Drag & Drop Editing

To use `Drag & Drop Editing`, select text in a source file using the techniques described in `"Selecting Text"`, then placing the cursor over the selected text, press the left-mouse button to grab the selected text. While holding the right-mouse button down, drag the text outline until the insertion cursor is precisely where the text should be moved, then release the mouse button.

### Balancing Punctuation

When you're editing source code, you must make sure that every parenthesis (`()`), bracket (`[]`), and brace (`{}`) has a mate, or the compiler could misinterpret your code or give you an error.

CodeWarrior provides several checks that help you balance these elements correctly.

To check for balanced parentheses, brackets, or braces, place the insertion point in the text you want to test. Then, choose `Balance` from the Edit Menu.

The CodeWarrior Editor searches starting from the text insertion point until it finds a parenthesis, bracket, or brace, then it searches in the opposite direction until it finds the matching half. When it finds the match, it selects the text between them. If the insertion point isn't enclosed or if the punctuation is unbalanced, the computer beeps.

### NOTE:

Use the Balance command as described in the step above to select blocks of code quickly.

#### Using automatic balancing

You can have the CodeWarrior Editor check for balanced punctuation automatically. If you would like to learn more about checking the balance of code automatically as you type, refer to `"Balance While Typing."`

### Shifting Text Left and Right

Use the `Shift Left` and `Shift Right` commands on the Edit Menu to shift a block of text to the left or right.

To shift blocks of text, select a block of text. If you don't know how to do this, see `"Selecting Text."` Then, choose `Shift Right` or `Shift Left` from the Edit Menu.

The CodeWarrior editor shifts the selected text one tab size to the right or left by inserting or deleting a tab at the beginning of every line in the selection.

To learn more about controlling the number of spaces the text is indented, refer to `"Font."`

### Undoing Changes

The CodeWarrior editor supplies ways to `Undo` mistakes as you edit a file.

#### Undoing the last edit

The `Undo` command reverses the effect of your last action. The name of the Undo command on the Edit Menu varies depending on what you last did. For example, if you just typed in some text, the command changes to Undo Typing.

#### Reverting to the last saved version of a file

The `Revert` command on the Edit Menu returns a file to its last saved version. For more information about this command, refer to `"Revert."`

### Controlling Color

You can use color to highlight many elements in your source code, such as comments, keywords, and quoted character strings. Highlighting these elements helps you identify them in the text, so you can check your spelling and syntax as you type by recognizing color patterns. For information on configuring color syntax options, see `"Syntax Styling."`

## Navigating in Text

The CodeWarrior Editor provides several methods for navigating in a file that you are editing.

This section covers these methods:

- `Finding a Routine`
- `Opening a Related File`
- `Going to a Particular Line`

### Finding a Routine

Click the Routine icon to display the Routine pop-up menu, discussed in`"The Routine pop-up menu,"` then select the routine you want to go to.

### NOTE:

If the pop-up is empty, the file is not a source code file.

### Opening a Related File

There are a few ways to open files related to the active Editor window. For example, if you are looking at a C++ .cpp source code file and want to view a .h header file that is used by the .cpp file, there are several ways to do this.

Use the `Interface Pop-up Menu` shown in `Figure 5.2` to open interface or header files referenced by the current file.

To open a file in the list, scroll to the file you'd like to see and release the mouse button.

There is another method for opening an interface or header file that your source code file uses. To open the related file, you type a special key combination after selecting the file name in the active window. To learn more about this method for opening files, refer to `"Opening an Existing File."`

### Going to a Particular Line

You can go to specific line in an Editor window if you know its number. Lines are numbered consecutively, with the first line designated as line 1.

Choose `Go To Line` from the Search Menu to display the Go To Line dialog box in Figure 5.8. Then enter the number of the line you want to go to and select OK.

#### Figure 5.8 Go to Line Number Dialog

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: support@metrowerks.com
Copyright © 1998, Metrowerks Corp. All rights reserved.
Last updated: February 15, 1998
* Chris Magnuson * John Roseborough