EDITING All of the standard text selection and editing features supplied by the operating system ’ s text editing framework work in Xcode.. Selecting Text All of the standard text selec
Trang 1EDITING
All of the standard text selection and editing features supplied by the operating system ’ s text editing framework work in Xcode If you need a refresher on the basics, refer to the Mac OS X Help The following sections highlight a few obscure features that are particularly useful when editing source code In addition, Xcode adds its own set of features and commands
Selecting Text
All of the standard text selection gestures work in Xcode Click and drag to select some text Hold down the Shift key to extend a selection Selecting text by dragging is dependent on timing; see the
“ Drag and Drop ” section later for an explanation
The standard Mac OS X text editor is sensitive to word boundaries when extending text selections It ’ s an obscure quirk that most people don ’ t notice, but may impact your coding When you Shift - click to extend a selection, Xcode examines the other end of the selection (the end not being moved) If that end lies on a word boundary, the end being extended snaps to word boundaries
If the stationary end is in the middle of a word or between non - word characters, the extended end can be positioned at any character This makes it very quick and easy to extend a selection to include additional words or symbols, because you don ’ t have to select the edge of the word; dragging anywhere into the word will suffi ce If you start with a selection on a word boundary, and want to extend it to a non - word boundary, extend the selection using the keyboard (Shift + right arrow or Shift + left arrow) instead This might be a good time to review the cursor movement modifi ers back in the “ Cursor Movement ” section
Selecting Columns
One of the more obscure features of the editor is the ability to select columns of text You can select all of the characters between two column positions across multiple lines by holding down the Option key and dragging out a selection Figure 6 - 22 shows a column selection Hold down the Shift and Option keys to extend or contract an existing column selection
Trang 2You can copy the selected characters to the clipboard or delete them If you copy the selection,
the clipboard will contain the selected characters of each line on separate lines Lines that have no
characters selected (lines shorter than the fi rst column) are not included You cannot paste into a
column selection or otherwise replace a column with multi - line content
Selecting Code Blocks
The Edit ➪ Format ➪ Balance command, also on the Right/Control + click contextual pop - up menu,
selects the contents of a C or Java style block of code surrounded by square brackets or curly braces
The command looks outward from the current selection to fi nd the nearest block delimiter, then
selects all of the text between it and its matching delimiter It will correctly account for escaped
quote characters and nested blocks
This command is similar to, and independent of, the Select to Matching Brace option in the Text
Editing pane of the Xcode Preferences, described earlier in the “ Editing Options ” section This
command, however, works only with braces and brackets
Jumping to a Line or Character
The Edit ➪ Go To Line (Command + L) command lets you jump
to a specifi c line or character position in a text fi le Selecting the
command opens the Goto window, as shown in Figure 6 - 23
Enter the position within the fi le that you want to select The
radio buttons determine whether the position will be interpreted
as a character offset or a line number
Press the Return key and that line or character is selected in the fi le and the Goto window closes
Alternatively, you can click the Select button This also selects your chosen position, but leaves
the Goto window open If you check the Auto - Update option, the position in the Goto window
continuously updates to refl ect your current position within the fi le
FIGURE 6-22
FIGURE 6-23
Trang 3Deleting Text
The text editor provides several useful variations of the standard delete key for deleting text adjacent
to the insertion point, as listed in the following table
KEY COMBINATION DELETES
Delete, Control + H One character to the left of the cursor Control + Delete From the cursor to the beginning of the previous subword Option + Delete From the cursor to the beginning of the previous word Command + Delete From the cursor to the beginning of the line
Delete - right, Control + D One character to the right of the cursor Control + delete - right From the cursor to the end of the next subword Option + delete - right From the cursor to the end of the next word Control + W From the cursor to the current mark
The Delete key, just above the Return key (sometimes labeled ⌫), deletes to the left The delete -right key (⌦), typically found on extended keyboards near the Home and End keys, deletes to the right Either can delete a single character, subword, word, or line
The delete keys follow the same pattern that the right and left arrow keys do The amount they delete increases depending on the modifi er used: either none, Control, Option, or Command
The one exception is the combination of Command + right - delete, which doesn ’ t work; use Shift + Command + right - arrow to select to the end of the line, and then press the Delete key
Control + H and Control + D are Emacs synonyms for Delete and delete - right Use these if you prefer
or if you don ’ t have a delete - right key on your keyboard These control key equivalents cannot be combined with the Option or the Control key modifi ers The Emacs - centric Control + W command deletes from the current cursor location to the location of the mark, previously set with Control -spacebar or Control + X Control + X
Drag and Drop
A text selection can be dragged around within a document, between documents, and between Xcode and other applications
A text selection and a text drag use the same gesture: click, move, and release The difference is in the timing To drag out a new text selection, press down the mouse button and move the mouse immediately The cursor remains a text cursor To start a drag, press down the mouse button and wait After a very short delay (less than a second) the cursor turns into an arrow You are now dragging the text selection You can customize this delay to suit your mousing speed See the discussion of the NSDragAndDropTextDelay expert setting in Chapter 23
Trang 4Press down the Option key before dropping the text to make a copy Dragging between documents
always performs a copy Dragging from other applications inserts the text equivalent of the object
being dragged Dragging fi le icons from the Finder or the project window inserts the URL of the fi le,
not the contents of the fi le itself To insert the contents of a fi le, open that fi le in Xcode, choose Edit
➪ Select All, and drag the text into your document
Most lists, displays, and fi elds in Xcode can be the source of a drag even when they are not
editable How useful this is will vary, but keep it in mind before you reach for a yellow pad to write
something down You might simply be able to drag it somewhere to keep it Drag any text selection
to the desktop or an open Finder window to create a clipping fi le containing that text This is a
handy way of saving or “ setting aside ” a chunk of code while you experiment
Font and Text Styles
Program source fi les are plaintext fi les that do not include any font, style, color, or other
typographic information Logically then, the commands and tools in the Edit ➪ Format ➪ Font and
Edit ➪ Format ➪ Text menus are inapplicable — even if they ’ re enabled The font and style used for
the text is set globally, as explained earlier in the “ Display Options ” section
When you use Xcode ’ s RTF (styled text) editor, the commands in these menus perform as expected
Saving Files
All editor panes, except for ones just created with the File ➪ New Empty File command, are
associated with a physical fi le Editing only changes the copy of the text held in memory A fi le is not
actually altered until the editor pane is saved, which writes those changes back to the fi le Several
variations of the Save command exist in Xcode, as listed in the following table
COMMAND ACTION
Save (Command + S) Writes the contents of the editor pane to its fi le
Save As … (Shift+Command+S) Enables you to create a new fi le and writes the text to that new
fi le The new fi le becomes the fi le associated with that pane If the original fi le was referred from a project, the path and name of that source item is also updated to refer to the new fi le The original
fi le is not altered, and the project no longer has a reference to it
Use this command to rename a fi le while preserving a copy of the original
Save a Copy As
(Shift+Option+Command+S)
Like Save As, this command enables you to choose a new fi le, write the contents of the editor pane to it, and leave the contents of the original fi le untouched But that ’ s where it stops The fi le associated with the editor pane is not changed, and it still refers to the original
fi le A subsequent Save command writes any changes to the original fi le Nothing in the project is altered This command is useful for making a snapshot of a fi le, or for quickly creating a new
fi le that is a copy of an existing one without altering the project
Trang 5COMMAND ACTION
Save All (Option+Command+S) This command presents a dialog box, shown in Figure 6 - 8, that
lists all modifi ed fi le buff ers being held in memory You can choose to save all, or only some, of them in a single command
The Save All dialog box was described earlier in this chapter in the “ Closing and Saving Editor Panes ” section
The Revert to Saved (Command + U) command is the opposite of the various Save commands
Instead of writing the changes to the fi le, it discards any changes made since the fi le was opened
or saved and re - reads the text from the fi le Xcode presents a warning beforehand, shown in Figure 6 - 24, ensuring that you really do want to abandon all the changes you ’ ve recently made
You cannot recover these changes once the Revert to Saved command has executed
FIGURE 6-24
Undo
Xcode ’ s undo features work pretty much the way they do in most modern applications, but a couple of differences exist Undo history is maintained on a per - fi le basis Until the fi le is saved and all editing panes that contain it are closed, Xcode maintains the undo history for the fi le, and all editing panes showing the same fi le share the same undo history
Each atomic editing action is recorded as a step in the undo history An “ atomic ” action would
be typing some sequence of regular text characters, navigation, auto - completion, using the Tab key, deleting text, cutting, pasting, or any other menu command that changes the contents of the text buffer
The Edit ➪ Undo command (Command + Z) reverses the effect of the most recent action recorded in the history The original action is then remembered on the redo list As you repeatedly use the Undo command, each change made to the fi le is undone, depleting the undo history and adding to the redo list
Trang 6Undoing a Cut or Copy command does not restore the previous contents of the system ’ s clipboard It only restores the state of the editor pane prior to the Cut command
The Redo command performs the complement of the Undo command It takes the most recent
action moved to the redo list, performs it again, and adds it to the undo history Together, the Undo
and Redo commands are like a time machine, allowing you to step forward and backward through
the history of the fi le ’ s changes until you fi nd the state of the fi le you want
Any new change made to a fi le erases the redo list If you go back in time and make a different
choice, you can no longer visit the future that contained the original choice The Revert to Saved
command empties both the undo and redo information for a fi le
The Save command does not alter the undo or redo information for a fi le This means it is possible
to undo changes made to a fi le after it has been saved Whenever you try to undo the last action that
occurred before a fi le was saved, Xcode presents a dialog box like the one shown in Figure 6 - 25
FIGURE 6-25
Some developers fi nd this dialog box annoying Chapter 19 explains how to set the
XCShowUndoPastSaveWarning advanced setting to disable that warning
SHELL SCRIPTS
If you ’ ve been around the Macintosh platform long enough to remember MPW (the Macintosh
Programmer ’ s Workshop), you may be missing one of its more intriguing features Every text
window in MPW was a “ live ” worksheet, capable of executing any MPW command In fact,
MPW made no real distinction between a text document and a shell window This made for some
interesting possibilities